Download the PHP package tomphp/transform without Composer
On this page you can find all versions of the php package tomphp/transform. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomphp/transform
More information about tomphp/transform
Files in tomphp/transform
Package transform
Short Description A set of tools to make array_map and array_filter more friendly.
License MIT
Homepage https://github.com/tomphp/php-transform
Informations about the package transform
Transform
Transform is a simple library which aims to make using PHP's array_map
function a more pleasant experience - and resulting in cleaner code.
Transform is a collection of helper functions for common transform actions.
For a great companion library of predicates to make your array_filter
code also look great, see Pentothal.
Namespace
From this point on, assume the TomPHP\Transform
namespace is used like so:
Example
Take this code:
Using Transform this looks like this:
Installation
Using composer:
composer require tomphp/transform
Chaining
Multiple transformations can be composed using the chain
function:
The Traversal Builder
If you want to chain together a collection of callMethod
, getProperty
and
getElement
calls, the __()
function provides a builder to write this in
a more elegant way.
Consider:
With the builder you can simply write:
Transformations
Object Transformations
T\callMethod(string $methodName, mixed ...$args)
T\callStatic(string $methodName, mixed ...$args)
T\getProperty(string $name)
Array Transformations
T\getElement(string|int $name)
String Transformations
T\prepend(string $prefix)
T\append(string $suffix)
T\concat(...$arguments)
Use __
as the placeholder for where you want the value inserted:
Generic Transformations
T\argumentTo(callable $callable, array $argments = [__])
You can also provide a list of arguments using __
as the placeholder for where
you want the value inserted:
T\newInstance(string $className, array $arguments = [__])
You can also provide a list of arguments using __
as the placeholder for where
you want the value inserted:
T\valueOrDefault(mixed $default, callable $predicate = null)
A predicate function can also be supplied:
T\valueOrDefault('negative number', function ($value) {
return $value >= 0;
})
// Is equivalent to:
function ($value) {
return $value >= 0 ? $value : 'negative number';
}