Non official WPML Functional Programming Library manual
Repository url: https://git.onthegosystems.com/wpml-packages/fp
composer require wpml/fp
Instead of writing:
$someArray = [ 1, 2, 3 ];
foreach ( $someArray as &$val ) {
$val++;
}
write:
$someArray = [ 1, 2, 3 ];
$someArray = Fns::each( function( $val ) {
return $val++;
}, $someArray );
Instead of:
$data = [
'foo' => 'bar'
];
$data['foo'];
write:
$data = [
'foo' => 'bar'
];
Obj::prop( 'foo', $data );
The advantage is, you don't need to know if $data
is array or object. It will work with any data structure:
$data = new stdClass();
$data->foo = 'bar';
Obj::prop( 'foo', $data ); // the same as above
If you have an array like this:
$data = [
'foo' => [
'bar' => 'baz'
]
];
and you want to obtain baz
you can use:
$value = Obj::pathOr( 'not found', [ 'foo', 'bar' ], $data ); // returns 'baz'
if the element on path does not exist, the first argument will be returned:
$value = Obj::pathOr( 'not found', [ 'foo', 'bal' ], $data ); // returns 'not found'
Instead of:
$data = [
'foo' => 'bar'
];
array_keys( $data );
write:
$data = [
'foo' => 'bar'
];
Obj::keys( $data ); // returns [ 'foo' ]
You can obtain public object properties in the same way:
$data = new stdClass();
$data->foo = 'bar';
Obj::keys( $data ); // returns [ 'foo' ]
You can chain functions to be executed from left to right with pipe()
.
Instead of writing:
$data = [
'foo' => [
'bar' => 'baz'
]
];
Fns::each( 'strtoupper', Fns::keys( Fns::prop( 'foo', $data ) ) );
write:
$data = [
'foo' => [
'bar' => 'baz'
]
];
$screamBar = pipe( Fns::prop( 'foo' ), Fns::keys() );
Fns::each( 'strtoupper', $screamBar( $data ) );
Notice this time function in pipe does not take processed data as a second argument. Data comes from:
- first from function call with $data as argument (
$screamBar( $data )
) - then data is passed from left to the right throught the pipe. So
Fns::prop( 'foo' )
gets data from function call above, then it runs actions on it and what it returns, goes to the next function (Fns::keys()
gets the result ofFns::prop( 'foo' )
which is[ 'bar' => 'baz' ]
)