PHP code example of tomphp / transform

1. Go to this page and download the library: Download tomphp/transform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomphp / transform example snippets


use TomPHP\Transform as T;

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

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

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

// Is equivalent to:

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


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

use function TomPHP\Transform\__;

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

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('getSomethingWith', 'param1', 'param2');

// Is equivalent to:

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

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

T\getProperty('name');

// Is equivalent to:

function ($object) {
    return $object->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'];
}

T\prepend('prefix: ');

// Is equivalent to:

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

T\append(' - suffix');

// Is equivalent to:

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

use const TomPHP\Transform\__;

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

// Is equivilent to:

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

T\argumentTo('strtolower');

// Is equivalent to:

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

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(Widget::class);

// Is equivalent to:

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

use const TomPHP\Transform\__;

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

// Is equivalent to:

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

T\valueOrDefault('default')

// Is equivalent to:

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