| Recommend this page to a friend! | 
| Info | Documentation | Reputation | Support forum | Blog | Links | 
| Ratings | Unique User Downloads | Download Rankings | ||||
| Not yet rated by the users | Total: 46 | All time:  10,798 This week: 206 | ||||
| Version | License | PHP version | Categories | |||
| mezon-functional 1.0 | MIT/X Consortium ... | 5 | PHP 5, Language | 
| Description | Author  | |
This package can manipulate objects with functional programming. Innovation Award 
 | 
This class provides various members and tools for functional programming. It will help you to work with arrays in a very simple way.
Here we can fetch specified field from all objects of array:
$obj1 = new stdClass();
$obj1->foo = 1;
$obj2 = new stdClass();
$obj2->foo = 2;
$obj3 = new stdClass();
$obj3->foo = 3;
$Data = array( $obj1 , $obj2 , $obj3 );
// will display array( 1 , 2 ,3 )
var_dump( \Mezon\Functional\Fetcher::getFields( $Data , 'foo' ) );
We can also set fields with multyple values:
$Values = array( 1 , 2 , 3 );
$obj1 = new stdClass();
$obj2 = new stdClass();
$Data = array( $obj1 , $obj2 );
Functional::setFieldsInObjects( $Data , 'foo' , $Values );
// will display 3 objects
var_dump( $Data );
And fianlly we can sum specified fields:
$obj1 = new stdClass();
$obj1->foo = 1;
$obj2 = new stdClass();
$obj2->foo = 2;
$obj3 = new stdClass();
$obj3->foo = 3;
$Data = array( $obj1 , $obj2 , $obj3 );
// will display value 6
var_dump( Functional::sumFields( $Data , 'foo' ) );
Note that you can recursively walk along the nested arrays:
$obj1 = new stdClass();
$obj1->foo = 1;
$obj2 = new stdClass();
$obj2->foo = 2;
$obj3 = new stdClass();
$obj3->foo = 3;
$Data = array( $obj1 , array( $obj2 , $obj3 ) );
// will display value 6
var_dump(Functional::sumFields( $Data , 'foo' ));
And this code will also work:
// will display value 3
var_dump(Functional::sumFields( [
    ['foo'=>1],
    ['foo'=>2]
] , 'foo' ));
We can also transform objects in arrays like this (the most basic and simple way):
/
*   Transformation function multiplies 'foo' field.
*/
function  transform2x( $Object )
{
    $Object->foo *= 2;
    return( $Object );
}
$obj1 = new stdClass();
$obj1->foo = 1;
$obj2 = new stdClass();
$obj2->foo = 2;
$obj3 = new stdClass();
$obj3->foo = 3;
$Data = array( $obj1 , $obj2 , $obj3 );
Functional::transform( $Data , 'transform2x' );
// will display 3 objects
// with 2, 4 and 6 values in their 'foo' fields
var_dump( $Data );
But if you need more complex transformations, you can use class Transform. It will allow you to build entirely new array.
$data = [
	1 , 2
];
Transform::convert($data,function($item){return [10$item, 100$item];});
var_dump($data);
// will output
// [10=>100 , 20=>200]
And if you want to transform only elements of the array, then use Transform::convertElements
$data = [
	1 , 2
];
Transform::convertElements($data,function($item){return 10 * $item;});
var_dump($data);
// will output
// [0=>10 , 1=>20]
| File | Role | Description | ||
|---|---|---|---|---|
| Data | Auxiliary data | |||
| Class | Class source | |||
| Data | Auxiliary data | |||
| Class | Class source | |||
| Class | Class source | |||
| Data | Auxiliary data | |||
| Doc. | Documentation | |||
| Class | Class source | |||
| / | Tests | 
| File | Role | Description | 
|---|---|---|
|    | 
Class | Class source | 
|    | 
Class | Class source | 
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. | 
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% | 
  | 
  | 
| Applications that use this package | 
 If you know an application of this package, send a message to the author to add a link here.