Git Product home page Git Product logo

php-transform's Introduction

Transform

Build Status

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:

use TomPHP\Transform as T;

Example

Take this code:

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

Using Transform this looks like this:

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

Installation

Using composer:

composer require tomphp/transform

Chaining

Multiple transformations can be composed using the chain function:

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.

Consider:

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

With the builder you can simply write:

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\prepend(' - suffix');

// Is equivalent to:

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

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:

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);
}

php-transform's People

Contributors

danizord avatar gmazzap avatar tomphp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.