Example #1
0
// 8. Sort multidimensional array (sort list of users by their names)
$users = [array('name' => 'Robert', 'age' => 20), array('name' => 'Alex', 'age' => 30), array('name' => 'Jack', 'age' => 25)];
$sortedByName = sorted($users, itemGetter('name'));
display('8.1. Users sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, itemGetter('name'));
display('8.2. Users sorted by name in descending order', $sortedByNameDesc);
// 9. Sort list of objects by property value (sort list of users by their name)
$users = [new User('Robert', 20), new User('Alex', 30), new User('Jack', 25)];
$sortedByName = sorted($users, propertyGetter('name'));
display('9.1. Users presented as list of objects sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, propertyGetter('name'));
display('9.2. Users presented as list of objects sorted by name in descending order', $sortedByNameDesc);
// 10. Sort list of objects by method result (sort list of users by their age)
$sortedByAge = sorted($users, methodCaller('getAge'));
display('10.1. Users presented as list of objects sorted by age', $sortedByAge);
$sortedByAgeDesc = sorted($users, true, methodCaller('getAge'));
display('10.2. Users presented as list of objects sorted by age in descending order', $sortedByAgeDesc);
class User
{
    public $name;
    public $age;
    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
Example #2
0
require_once __DIR__ . '/../autoload.php';
use function nspl\ds\defaultarray;
use function nspl\ds\dstring;
use function nspl\f\map;
use function nspl\op\methodCaller;
// 1. Cast default array to regular PHP array
$wordCounter = defaultarray(0);
++$wordCounter['hello'];
++$wordCounter['hello'];
++$wordCounter['hello'];
++$wordCounter['world'];
echo "Word counter:\n";
print_r($wordCounter->toArray());
// 2. Multidimensional default array
// Note that we create nested default array with an anonymous function.
// Otherwise, default array object will be shared across all parent array fields.
$matrix = defaultarray(function () {
    return defaultarray(0);
});
for ($i = 0; $i < 3; ++$i) {
    for ($j = 0; $j < 3; ++$j) {
        ++$matrix[$i][$j];
    }
}
echo "Matrix 3x3:\n";
print_r(map(methodCaller('toArray'), $matrix->toArray()));
// casting default array with all nested default arrays to PHP array
// 3. Dynamic email subject
$subject = dstring()->addStr('Something bad happened on ')->addConstant('APPLICATION_ENV')->addStr(' at ')->addFunction('date', ['Y-m-d H:i:s']);
define('APPLICATION_ENV', 'staging');
echo $subject . "\n";
Example #3
0
File: OpTest.php Project: ihor/Nspl
 public function testMethodCaller()
 {
     $users = [new User('John', 18), new User('Jack', 20), new User('Sarah', 19)];
     $this->assertEquals(['John', 'Jack', 'Sarah'], map(methodCaller('getName'), $users));
     $this->assertEquals([18, 20, 19], map(methodCaller('getAge'), $users));
     $this->assertEquals([21, 23, 22], map(methodCaller('getAgeIn', [3]), $users));
 }