2017 © Pedro Peláez
 

library transform

A set of tools to make array_map and array_filter more friendly.

image

tomphp/transform

A set of tools to make array_map and array_filter more friendly.

  • Sunday, March 27, 2016
  • by tomphp
  • Repository
  • 1 Watchers
  • 14 Stars
  • 495 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 14 Versions
  • 5 % Grown

The README.md

Transform

Build Status, (*1)

Transform is a simple library which aims to make using PHP's array_map function a more pleasant experience - and resulting in cleaner code., (*2)

Transform is a collection of helper functions for common transform actions., (*3)

For a great companion library of predicates to make your array_filter code also look great, see Pentothal., (*4)

Namespace

From this point on, assume the TomPHP\Transform namespace is used like so:, (*5)

use TomPHP\Transform as T;

Example

Take this code:, (*6)

$names = array_map(
    function ($user) {
        return $user->getName();
    },
    $allUsers
);

Using Transform this looks like this:, (*7)

$names = array_map(T\callMethod('getName'), $allUsers);

Installation

Using composer:, (*8)

composer require tomphp/transform, (*9)

Chaining

Multiple transformations can be composed using the chain function:, (*10)

T\chain(T\getProperty('user'), T\getElement('name'));

// Is equivalent to:

function ($object) {
    return $object->user['name'];
}

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., (*11)

Consider:, (*12)

$dobs = array_map(
    function (User $user) {
        return $user->getMetaData()['dob']->format('Y-m-d');
    },
    $users
);

With the builder you can simply write:, (*13)

use function TomPHP\Transform\__;

$dobs = array_map(__()->getMetaData()['dob']->format('Y-m-d'), $users);

Transformations

Object Transformations

T\callMethod(string $methodName, mixed ...$args)

T\classMethod('getName');

// Is equivalent to:

function ($object) {
    return $object->getName();
}
T\classMethod('format', 'Y-m-d');

// Is equivalent to:

function ($object) {
    return $object->format('Y-m-d');
}

T\callStatic(string $methodName, mixed ...$args)

T\callStatic('getSomethingWith', 'param1', 'param2');

// Is equivalent to:

function ($object) {
    return $object::getSomethingWith('param1', 'param2');
}

// or to:
function ($classAsString) {
    return $classAsString::getSomethingWith('param1', 'param2');
}

T\getProperty(string $name)

T\getProperty('name');

// Is equivalent to:

function ($object) {
    return $object->name;
}

Array Transformations

T\getElement(string|int $name)

T\getElement('name');

// Is equivalent to:

function ($array) {
    return $array['name'];
}
T\getElement(['user', 'name']);

// Is equivalent to:

function ($array) {
    return $array['user']['name'];
}

String Transformations

T\prepend(string $prefix)

T\prepend('prefix: ');

// Is equivalent to:

function ($value) {
    return 'prefix: ' . $value;
}

T\append(string $suffix)

T\append(' - suffix');

// Is equivalent to:

function ($value) {
    return $value . ' - suffix';
}

T\concat(...$arguments)

Use __ as the placeholder for where you want the value inserted:, (*14)

use const TomPHP\Transform\__;

T\concat('Hello ', __, '!');

// Is equivilent to:

function ($value) {
    return 'Hello ' . $value . '!';
}

Generic Transformations

T\argumentTo(callable $callable, array $argments = [__])

T\argumentTo('strtolower');

// Is equivalent to:

function ($value) {
    return strtolower($value);
}

You can also provide a list of arguments using __ as the placeholder for where you want the value inserted:, (*15)

use const TomPHP\Transform\__;

T\argumentTo('strpos', ['Tom: My name is Tom', __, 4]);

// Is equivalent to:

function ($value) {
    return strpos('Tom: My name is Tom', $value, 4);
}

T\newInstance(string $className, array $arguments = [__])

T\newInstance(Widget::class);

// Is equivalent to:

function ($value) {
    return new Widget($value);
}

You can also provide a list of arguments using __ as the placeholder for where you want the value inserted:, (*16)

use const TomPHP\Transform\__;

T\newInstance(Widget, ['first', __, 'last']);

// Is equivalent to:

function ($value) {
    return new Widget('first', $value, 'last');
}

T\valueOrDefault(mixed $default, callable $predicate = null)

T\valueOrDefault('default')

// Is equivalent to:

function ($value) {
    return $value ?: 'default';
}

A predicate function can also be supplied:, (*17)

```php T\valueOrDefault('negative number', function ($value) { return $value >= 0; }), (*18)

// Is equivalent to:, (*19)

function ($value) { return $value >= 0 ? $value : 'negative number'; }, (*20)

The Versions

27/03 2016

dev-master

9999999-dev https://github.com/tomphp/php-transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

function transform map functional programming array_map

27/03 2016

v0.2.4

0.2.4.0 https://github.com/tomphp/php-transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

function transform map functional programming array_map

22/03 2016

v0.2.3

0.2.3.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

21/03 2016

v0.2.2

0.2.2.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

13/03 2016

v0.2.1

0.2.1.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

07/02 2016

v0.2.0

0.2.0.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

14/01 2016

v0.1.6

0.1.6.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

14/01 2016

v0.1.5

0.1.5.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

14/01 2016

v0.1.4

0.1.4.0 https://github.com/tomphp/Transform

A set of tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

13/01 2016

v0.1.3

0.1.3.0 https://github.com/tomphp/Transform

A set tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

11/01 2016

v0.1.2

0.1.2.0 https://github.com/tomphp/Transform

A set tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Requires

 

The Development Requires

function transform map functional programming array_map

11/01 2016

v0.1.1

0.1.1.0 https://github.com/tomphp/Transform

A set tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Development Requires

function transform map functional programming array_map

11/01 2016

v0.1.0

0.1.0.0 https://github.com/tomphp/Transform

A set tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Development Requires

function transform map functional programming array_map

10/01 2016

v0.0.1

0.0.1.0

A set tools to make array_map and array_filter more friendly.

  Sources   Download

MIT

The Development Requires