1. Go to this page and download the library: Download ps/fluent-traversable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
FluentTraversable::from(array())
->filter(...)//intermediate operation, so I can chain
->map(...)//intermediate operation, so I can chain
->size()//terminate operation, I cannot chain - it returns integer
FluentTraversable::from($books)
->firstMatch(is::eq('author.name', 'Stephen King'))
//there is Option instance, you can transform value (thanks to map) if this value exists
->map(function($book){
return 'Found book: '.$book->getTitle();
})
//provide default value if book wasn't found
->orElse(Option::fromValue('Not found any book...'))
//print result to stdout thanks to Option::map method
->map('printf')
//or you can call "->get()" and assign to variable, it is safe because you provided default value by "orElse"
;
> Option::fromValue($patientId)
> ->map([$patientRepo,'find'])
> //there could be `Some(null)` value!
> ->map(get::value('doctor.phone'))
> //there could be also `Some(null)` value, so `null` might be passed to `$this::callToDoctor`
> ->each([$this,'callToDoctor']);
>
> Option::fromArrayValue($patientId)
> ->flatMap(get::option([$patientRepo,'find']))
> //when `$this::callToDoctor` return `null` there will be `None`
> ->flatMap(get::option('doctor.phone'))
> //when doctor has not phone set, there will be `None` value
> ->each([$this,'callToDoctor']);
>
$maxEvenPrinter = FluentComposer::forArray();
//very important is, to not chain directly from `forArray()` method, first you should assign created object
//to variable, and then using reference to object you can compose your function
$maxEvenPrinter
->filter(function($number){
//only even numbers
return $number % 2 === 0;
})
->max()
//"max" (as same as firstMatch) returns Option, because there is possibility given array is empty
->map(function($value){
return 'max even number: '.$value;
})
->orElse(Option::fromValue('max even number not found'))
->map('printf');
$maxEvenPrinter(array(1, 3, 5, 2, 4));
//output will be: "max even number: 4"
$maxEvenPrinter(array(1, 3, 5));
//output will be: "max even number not found"
$func = FluentComposer::forValue();
$func-> /* some chaining methods */;
$func('value1', 'this value will be ignored')
$patients = array(...);
$info = FluentTraversable::from($patients)
->groupBy(get::value('bloodType'))
//we have multi-dimensional array, where key is bloodType, value is array of patients
->map(
//we map array of patients for each blood type to percentage value, so lets compose a function
FluentComposer::forArray()
//split array of patients into two arrays, first females, second males
->partition(is::eq('sex', 'female'))
//map those arrays to its size, so we have number of females and males
->map(func::unary('count'))
//calculate a percent
->collect(function($elements){
list($femalesCount, $malesCount) = $elements;
return $femalesCount / ($femalesCount + $malesCount) * 100;
})
)
//get our result with index preserving
->toMap();