Mezon PHP Functional Programming Library: Manipulate objects with functional programming

Recommend this page to a friend!
  Info   View files Documentation   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-09-11 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 20 This week: 1All time: 10,062 This week: 232Up
Version License PHP version Categories
mezon-functional 1.0MIT/X Consortium ...5PHP 5, Language
Description Author

This package can manipulate objects with functional programming.

It provides functions that can access objects to perform several types of actions on them. Currently it can:

- Get, set, and sum the values of a specific variable of a given array of objects
- Apply a transformation to the values of a specific variable of a given array of objects by invoking a give callback function

Innovation Award
PHP Programming Innovation award nominee
September 2020
Nominee
Vote
Sometimes you need to perform the same operation on a set of distinct objects.

This package makes it easier to perform a given operation on multiple objects by invoking a given function those objects.

Manuel Lemos
Picture of Alexey Dodonov
  Performance   Level  
Name: Alexey Dodonov <contact>
Classes: 22 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 297180 in Russian Federation Russian Federation
Week rank: 3 Up1 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 2x

Details

Functional programming # Base application class Build Status codecov

Intro

This class provides various members and tools for functional programming. It will help you to work with arrays in a very simple way.

Modes

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' ));

Transformations

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]
  Files folder image Files  
File Role Description
Files folder imageTests (2 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Plain text file Compare.php Class Class source
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file Fetcher.php Class Class source
Plain text file Functional.php Class Class source
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Transform.php Class Class source

  Files folder image Files  /  Tests  
File Role Description
  Plain text file FunctionalUnitTest.php Class Class source
  Plain text file TransformUnitTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:20
This week:1
All time:10,062
This week:232Up

For more information send a message to info at phpclasses dot org.