dsheiko / extras
Collection of chainable high-order functions to abstract and manipulate PHP types
Installs: 2 793
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 5
Forks: 3
Open Issues: 0
Requires
- php: >=7.0.0
Requires (Dev)
- apigen/apigen: dev-master
- peridot-php/leo: ^1.6
- peridot-php/peridot: ^1.19
- peridot-php/peridot-code-coverage-reporters: ^3.0
- roave/better-reflection: dev-master#c87d856
- squizlabs/php_codesniffer: ^3.1
This package is not auto-updated.
Last update: 2024-12-22 06:07:35 UTC
README
Collection of chainable high-order functions to abstract and manipulate PHP types. The library extends PHP types with JavaScript and Underscore.js methods
The packages takes its name from
Array Extras
referring to the array methods added in ES5 (JavaScript) to abstract generic array manipulation logic
Installation
Require as a composer dependency:
composer require "dsheiko/extras"
Highlights
- Fixing PHP:
- Naming convention: all methods are
camelCase
styles vs PHP built-in functions inlower_case
- Consistent parameter order (
Chain::chain($target)->method(...$options)
or<Type>::method($target, ...$options)
) - Methods are chainable
- Data structure
PlainObject
similar to JavaScript plain object - Manipulation target (value) can always be as reference as well as type literal
- Naming convention: all methods are
- Familiar syntax: JavaScript methods, in addition methods of Underscore.js/Lodash
- Performance: package relies on PHP native methods; no
foreach
where a built-in specific function can be used
Sets
Download
Overview
Examples
None-reference target
<?php use \Dsheiko\Extras\Arrays; function numToArray(int $num): array { return [$num]; } $res = Arrays::map(range(1,3), "numToArray"); // [[1],[2],[3]]
Chaining methods
<?php use \Dsheiko\Extras\Any; $res = Any::chain(new \ArrayObject([1,2,3])) ->toArray() // value is [1,2,3] ->map(function($num){ return [ "num" => $num ]; }) // value is [[ "num" => 1, ..]] ->reduce(function($carry, $arr){ $carry .= $arr["num"]; return $carry; }, "") // value is "123" ->replace("/2/", "") // value is "13" ->then(function($value){ if (empty($value)) { throw new \Exception("Empty value"); } return $value; }) ->value(); echo $res; // "13"
Accessing methods directly
<?php use \Dsheiko\Extras\Arrays; class Foo { public $bar = "BAR"; } $arr = Arrays::from(new Foo); // ["bar" => "BAR"]
Plain object
<?php use \Dsheiko\Extras\Arrays; $po = Arrays::object(["foo" => "FOO", "bar" => ["baz" => "BAZ"]]); echo $po->foo; // FOO echo $po->bar->baz; // BAZ var_dump($po->bar->entries()); // [["baz", "BAZ"]]