adhocore / with
With provides object like fluent interface for scalars and non objects
Fund package maintenance!
adhocore
paypal.me/ji10
Requires (Dev)
- phpunit/phpunit: ^5.7.0
This package is auto-updated.
Last update: 2022-10-01 08:06:05 UTC
README
- Objectify scalars and non objects
- Fluent method chaining instead of nested function calls
- For PHP7
Installation
composer require adhocore/with
Usage
use function Ahc\with; // OR use Ahc\With\With; $val = ['a' => 10, 'b' => 12, 'c' => 13]; $with = with($val) // OR (new With($val)) ->array_values() // _ at the end means the current value is appended to the supplied arguments (default is prepend). ->array_map_(function ($v) { return $v + 2; }) ->array_sum() ; // Get the final result echo $with(); // 41 // Passing value through closures or class methods: with($value)->via(function ($val) { return $val; }); with($value)->via([new SomeClass, 'method']);
Why
TL;DR: Provides more intuitiveness, comprehensibility and less cognitive overhead than nested function calls.
Did you ever had to pass a scalar/non-object through many layers of functions? Then you might have probably ended up with having to call the first the function which had to be called at the last logically. For example if you want to sum the keys of an array after adding 2 to them?
// Without: array_sum(array_map(function ($a) { return $a + 2; }, array_keys($array))); // With: with($array)->array_keys()->array_map_(function ($a) { return $a + 2; })->array_sum();