\Colada\x()
Helper function for simplify callbacks., (*1)
, (*2)
Installation
$ composer require alexeyshockov/colada-x
Usage
With ColadaX:, (*3)
$activeUsers = array_filter($users, \Colada\x()->isActive());
```php
$role = 'ADMIN';
$administrators = array_filter($users, \Colada\x()->hasRole($role));, (*4)
Instead of pure PHP:
```php
$activeUsers = array_filter($users, function ($user) {
return $user->isActive();
});
```php
$role = 'ADMIN';
$activeUsers = array_filter($users, function ($user) use ($role) {
return $user->hasRole($role);
});, (*5)
## Useful together
Some code examples for your imagination.
### Laravel 5 Collections
```php
$activeUsers = $users->filter(\Colada\x()->isActive());
$activeUsers = $users->filter(\Colada\x()->isActive());
The library already has partial_method
function, but it's less powerful., (*6)
use Functional as F;
$activeUsers = F\select($users, \Colada\x()->isActive());
Less useful, but still
Doctrine 2 Collections (Symfony 2, Doctine 2 ORM, etc.)
// __asClosure() is needed because all Doctrine's methods accept only \Closure instances :(
$hasActiveUsers = $users->exists(\Colada\x()->isActive()->__asClosure());
Laravel 4 Collections
The same problem as described above with Doctrine., (*7)
Alernatives
If you need only the basic functionaly, take a look at invoke()
, invoke_first()
, invoke_if()
, invoke_last()
,
invoker()
from a great functional-php library., (*8)