ultimix
functional_programming.php File Reference

Go to the source code of this file.

Functions

 _return_or_throw ($DefaultValue, $Field)
 get_field (&$Entity, $Field, $DefaultValue= '_throw_exception')
 get_fields (&$Entity, $Fields, $DefaultValue= '_throw_exception')
 is_field_set (&$Entity, $Field)
 get_field_ex (&$ArrayOfEntities, $Field, $DefaultValue= '_throw_exception')
 array_filter_ex (&$ArrayOfEntities, $Condition= '1==1')
 get_field_cond (&$ArrayOfEntities, $Field, $Condition= '1==1', $DefaultValue= '_throw_exception')
 array_sum_cond (&$Array, $Field=false, $Condition= '1==1')
 set_field (&$Entity, $Field, $Value)
 append_to_field (&$Entity, $Field, $Value)
 implode_ex ($Str, &$Arr, $Field)
 extend (&$Destination, $Source)
 array_sum_ex (&$Array, $Field=false)
 get_dummie (&$Entity)
 remove_fields (&$Entity, $Fields)
 make_array (&$Scalar)
 make_hash_by_field (&$Array, $Field)
 vectorize_by_field (&$Array, $Field)
 get_record_by_field (&$Array, $Field, $Value, $DefaultValue= '_throw_exception')

Function Documentation

_return_or_throw (   $DefaultValue,
  $Field 
)

Function returns value or throws exception.

Parameters
$DefaultValue- Default value.
$Field- Field/key.
Returns
Default value.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 42 of file functional_programming.php.

{
if( $DefaultValue === '_throw_exception' )
{
throw( new Exception( "Key '$Field' does not exists" ) );
}
else
{
return( $DefaultValue );
}
}
append_to_field ( $Entity,
  $Field,
  $Value 
)

Function appends value.

Parameters
$Entity- Object or array.
$Field- Field/key.
$Value- Value to be set.
Returns
Object/array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 513 of file functional_programming.php.

{
try
{
if( is_field_set( $Entity , $Field ) === false || get_field( $Entity , $Field , false ) === false )
{
set_field( $Entity , $Field , array() );
}
if( is_object( $Entity ) )
{
array_push( $Entity->$Field , $Value );
}
if( is_array( $Entity ) )
{
array_push( $Entity[ $Field ] , $Value );
}
return( $Entity );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
array_filter_ex ( $ArrayOfEntities,
  $Condition = '1 == 1' 
)

Function filters array.

Parameters
$ArrayOfEntities- Array of objects or arrays.
$Condition- Record filtration condition.
Returns
Array of values.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 295 of file functional_programming.php.

{
try
{
$FilterFunction = create_function( '$Element' , "return( $Condition );" );
$FilteredArrayOfEntities = array_filter( $ArrayOfEntities , $FilterFunction );
return( $FilteredArrayOfEntities );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
array_sum_cond ( $Array,
  $Field = false,
  $Condition = '1 == 1' 
)

Function sum all array's elements.

Parameters
$Array- Array with elements.
$Field- Field to sum.
$Condition- Record filtration condition.
Returns
Sum.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 397 of file functional_programming.php.

{
try
{
if( count( $Array ) )
{
$Keys = array_keys( $Array );
if( is_array( $Array[ $Keys[ 0 ] ] ) )
{
$Sum = 0;
foreach( $Array as $i => $Element )
{
$Sum += array_sum_cond( $Element , $Field , $Condition );
}
return( $Sum );
}
}
if( $Field !== false )
{
$Array = get_field_cond( $Array , $Field , $Condition );
}
return( array_sum( $Array ) );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
array_sum_ex ( $Array,
  $Field = false 
)

Function sum all array's elements.

Parameters
$Array- Array with elements.
$Field- Field to sum.
Returns
- Sum.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 656 of file functional_programming.php.

{
try
{
if( $Field !== false )
{
$Array2 = get_field_ex( $Array , $Field );
}
else
{
$Array2 = $Array;
}
return( array_sum( $Array2 ) );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
extend ( $Destination,
  $Source 
)

Function adds fields/values in the object/array.

Parameters
$Destination- Changing object.
$Source- Data to add.
Returns
- Object/array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 608 of file functional_programming.php.

{
try
{
if( $Source === false )
{
return( $Destination );
}
foreach( $Source as $k => $v )
{
$Destination = set_field( $Destination , $k , $v );
}
return( $Destination );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
get_dummie ( $Entity)

Function creates dummie object or empty array.

Parameters
$Array- Array or object.
Returns
- Array or object.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 699 of file functional_programming.php.

{
try
{
if( is_array( $Entity ) )
{
return( array() );
}
elseif( is_object( $Entity ) )
{
return( new stdClass() );
}
else
{
throw( new Exception( "Illegal data type : ".gettype( $Entity ) ) );
}
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
get_field ( $Entity,
  $Field,
  $DefaultValue = '_throw_exception' 
)

Function returns value from array/object.

Parameters
$Entity- Object or array.
$Field- Field/key.
$DefaultValue- Default value.
Returns
Value.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 84 of file functional_programming.php.

{
try
{
if( is_object( $Entity ) )
{
if( property_exists( $Entity , $Field ) )
{
return( $Entity->$Field );
}
return( _return_or_throw( $DefaultValue , $Field ) );
}
if( is_array( $Entity ) )
{
if( array_key_exists( $Field , $Entity ) )
{
return( $Entity[ $Field ] );
}
return( _return_or_throw( $DefaultValue , $Field ) );
}
throw( new Exception( "Illegal value was passed" ) );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
get_field_cond ( $ArrayOfEntities,
  $Field,
  $Condition = '1 == 1',
  $DefaultValue = '_throw_exception' 
)

Function returns values from array of arrays/objects.

Parameters
$ArrayOfEntities- Array of objects or arrays.
$Field- Field/key.
$Condition- Record filtration condition.
$DefaultValue- Default value.
Returns
Array of values.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 345 of file functional_programming.php.

{
try
{
$RetValues = array();
$FilteredArrayOfEntities = array_filter_ex( $ArrayOfEntities , $Condition );
foreach( $FilteredArrayOfEntities as $k => $v )
{
$RetValues [] = get_field( $v , $Field , $DefaultValue );
}
return( $RetValues );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
get_field_ex ( $ArrayOfEntities,
  $Field,
  $DefaultValue = '_throw_exception' 
)

Function returns value from array of arrays/objects.

Parameters
$ArrayOfEntities- Array of objects or arrays.
$Field- Field/key.
$DefaultValue- Default value.
Returns
Array of values.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 250 of file functional_programming.php.

{
try
{
$RetValues = array();
foreach( $ArrayOfEntities as $k => $v )
{
$RetValues [] = get_field( $v , $Field , $DefaultValue );
}
return( $RetValues );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
get_fields ( $Entity,
  $Fields,
  $DefaultValue = '_throw_exception' 
)

Function returns values from array/object.

Parameters
$Entity- Object or array.
$Fields- Fields/keys.
$DefaultValue- Default value.
Returns
Value.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 146 of file functional_programming.php.

{
try
{
$Values = array();
$Fields = explode( ',' , $Fields );
foreach( $Fields as $i => $Field )
{
$Values [] = get_field( $Entity , $Field , $DefaultValue );
}
return( $Values );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
get_record_by_field ( $Array,
  $Field,
  $Value,
  $DefaultValue = '_throw_exception' 
)

Function returns record.

Parameters
$Array- Array of entities.
$Field- Field name.
$Value- Value.
$DefaultValue- Default value.
Returns
Record.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 947 of file functional_programming.php.

{
try
{
foreach( $Array as $i => $Element )
{
if( get_field( $Element , $Field , $DefaultValue ) == $Value )
{
return( $Element );
}
}
return( false );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
implode_ex (   $Str,
$Arr,
  $Field 
)

Function joins object's/array's fields.

Parameters
$Str- Joining string.
$Arr- Array of objects/arrays.
$Field- Joining field.
Returns
- Object/array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 570 of file functional_programming.php.

{
try
{
return( implode( $Str , get_field_ex( $Arr , $Field ) ) );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
is_field_set ( $Entity,
  $Field 
)

Function validate field's existance.

Parameters
$Entity- Object or array.
$Field- Field/key.
Returns
true if exists, false otherwise.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 192 of file functional_programming.php.

{
try
{
if( is_object( $Entity ) )
{
if( property_exists( $Entity , $Field ) )
{
return( true );
}
}
if( is_array( $Entity ) )
{
if( isset( $Entity[ $Field ] ) )
{
return( true );
}
}
return( false );
}
catch( Exception $e )
{
$Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
}
}
make_array ( $Scalar)

Function creates array using scalar.

Parameters
$Scalar- Scalar.
Returns
- Array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 797 of file functional_programming.php.

{
try
{
if( is_array( $Scalar ) || is_object( $Scalar ) )
{
return( $Scalar );
}
else
{
return( array( $Scalar ) );
}
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
make_hash_by_field ( $Array,
  $Field 
)

Function creates hash.

Parameters
$Array- Array of entities.
$Field- Field name.
Returns
- Hash.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 842 of file functional_programming.php.

{
try
{
$Return = array();
foreach( $Array as $k => $v )
{
$Return[ get_field( $v , $Field ) ] = $v;
}
return( $Return );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
remove_fields ( $Entity,
  $Fields 
)

Function removes specified array's elements.

Parameters
$Entity- Object or array.
$Fields- Array of fields.
Returns
- Changed object or array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 748 of file functional_programming.php.

{
try
{
if( is_array( $Fields ) === false )
{
$Fields = array( $Fields );
}
$ChangedEntity = get_dummie( $Entity );
foreach( $Entity as $Field => $Value )
{
if( array_search( $Field , $Fields ) === false )
{
set_field( $ChangedEntity , $Field , $Value );
}
}
return( $Entity = $ChangedEntity );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
set_field ( $Entity,
  $Field,
  $Value 
)

Function sets value from array/object.

Parameters
$Entity- Object or array.
$Field- Field/key.
$Value- Value to be set.
Returns
Object/array.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 459 of file functional_programming.php.

{
try
{
if( is_object( $Entity ) )
{
$Entity->$Field = $Value;
return( $Entity );
}
if( is_array( $Entity ) )
{
$Entity[ $Field ] = $Value;
return( $Entity );
}
return( $Entity );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}
vectorize_by_field ( $Array,
  $Field 
)

Function vectorises array.

Parameters
$Array- Array of entities.
$Field- Field name.
Returns
- Hash.
Exceptions
ExceptionAn exception of this type is thrown.
Author
Dodonov A.A.

Definition at line 887 of file functional_programming.php.

{
try
{
$Return = array();
foreach( $Array as $k => $v )
{
$Key = get_field( $v , $Field );
if( isset( $Return[ $Key ] ) === false )
{
$Return[ $Key ] = array();
}
$Return[ $Key ][] = $v;
}
return( $Return );
}
catch( Exception $e )
{
$a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
}
}