alexeyshockov/colada-x

This package is abandoned and no longer maintained. No replacement package was suggested.

Helper function for simplification callbacks

v0.3.0 2019-02-15 11:36 UTC

This package is auto-updated.

Last update: 2022-04-15 18:44:17 UTC


README

Helper function for simplify callbacks.

Latest Stable Version Scrutinizer Code Quality Build Status

Installation

$ composer require alexeyshockov/colada-x

Usage

With ColadaX:

$activeUsers = array_filter($users, \Colada\x()->isActive());
$role = 'ADMIN';
$administrators = array_filter($users, \Colada\x()->hasRole($role));

Instead of pure PHP:

$activeUsers = array_filter($users, function ($user) {
    return $user->isActive();
});
$role = 'ADMIN';
$activeUsers = array_filter($users, function ($user) use ($role) {
    return $user->hasRole($role);
});

Useful together

Some code examples for your imagination.

Laravel 5 Collections

$activeUsers = $users->filter(\Colada\x()->isActive());

PHP Collection (schmittjoh/php-collection)

$activeUsers = $users->filter(\Colada\x()->isActive());

functional-php (lstrojny/functional-php)

The library already has partial_method function, but it's less powerful.

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.

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.