PHP code example of selective / transformer

1. Go to this page and download the library: Download selective/transformer 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/ */

    

selective / transformer example snippets



use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'address.first_name')
    ->map('lastName', 'address.last_name')
    ->map('invoice.items', 'root.sub1.sub2.items');

// ...

use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('bar1', 'foo.bar', 'string')
    ->map('bar2', 'foo.bar2.0', 'string')
    ->map('sub.sub2.sub3', 'foo.bar2.1', 'string');

$user = new stdClass();
$user->foo = new stdClass();
$user->foo->bar = 'Hello Bar';
$user->foo->bar2 = [
    0 => 'Test 0',
    1 => 'Test 1',
];

$result = $transformer->toArray((array)$user);

[
    'bar1' => 'Hello Bar',
    'bar2' => 'Test 0',
    'sub' => [
        'sub2' => [
            'sub3' => 'Test 1',
        ],
    ],
];

$data = [
    'first_name' => 'Sally',
    'last_name' => '',
    'email' => '[email protected]',
];



use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name')
    ->map('lastName', 'last_name')
    ->map('email', 'email');
    
$result = $transformer->toArray($data);

[
    'firstName' => 'Sally',
    'email' => '[email protected]',
];

$transformer = new ArrayTransformer();

$transformer->map('id', 'id', $transformer->rule()->integer())
    ->map('first_name', 'first_name', $transformer->rule()->string())
    ->map('last_name', 'last_name', $transformer->rule()->string())
    ->map('phone', 'phone', $transformer->rule()->string())
    ->map('enabled', 'enabled', $transformer->rule()->boolean());

$rows = [];
$rows[] = [
    'id' => '100',
    'first_name' => 'Sally',
    'last_name' => '',
    'phone' => null,
    'enabled' => '1',
];

$rows[] = [
    'id' => '101',
    'first_name' => 'Max',
    'last_name' => 'Doe',
    'phone' => '+123456789',
    'enabled' => '0',
];

$result = $transformer->toArrays($rows);

[
    [
        'id' => 100,
        'first_name' => 'Sally',
        'enabled' => true,
    ],
    [
        'id' => 101,
        'first_name' => 'Max',
        'last_name' => 'Doe',
        'phone' => '+123456789',
        'enabled' => false,
    ],
]



use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name', 'string|  'last_name' => '',
    'email' => '[email protected]',
];
    
$result = $transformer->toArray($data);

[
    'firstName' => 'Sally',
    'lastName' => '',
    'email' => '[email protected]',
];



use Selective\Transformer\ArrayTransformer;

$transformer = new ArrayTransformer();

$transformer->map('firstName', 'first_name', $transformer->rule()->string()->first_name' => 'Sally',
    'last_name' => '',
    'email' => '[email protected]',
];

$result = $transformer->toArray($data);

[
    'firstName' => 'Sally',
    'lastName' => '',
    'email' => '[email protected]',
];

// Cast value to string, convert blank to null
$transformer->rule()->string();

// Cast value to string, allow blank string ''
$transformer->rule()->string(true);

// Cast value to int
$transformer->rule()->integer();

// Cast value to float
$transformer->rule()->float();

// Cast value to bool
$transformer->rule()->boolean();

// Cast value to datetime string, default: Y-m-d H:i:s
$transformer->rule()->date();

// Cast value to date string
$transformer->rule()->date('Y-m-d');

// Format value to number using the number_format function
$transformer->rule()->number();

// Format value to number with a custom number format
$transformer->rule()->number(2, '.', ',');

// Cast value to array
$transformer->rule()->array();

// Cast value using a custom callback function
$transformer->rule()->callback(
    function ($value) {
        return 'My custom value: ' . $value;
    }
);

// Set fixed value
$transformer->set('bar.0.item', 'default-value');

// Apply transformation to array item
$transformer->rule()->transform(
    function (ArrayTransformer $transformer) {
        $transformer
            ->map('id', 'id', 'integer')
            ->map('first_name', 'first_name', 'string');
    }
);

// Apply transformation to a list of arrays
$transformer->rule()->transformList(
    function (ArrayTransformer $transformer) {
        $transformer
            ->map('id', 'id', 'integer')
            ->map('first_name', 'first_name', 'string');
       }
);

$transformer = new ArrayTransformer();

// Add a trim filter using the native trim function
$transformer->registerFilter('trim', 'trim');

// Usage
$transformer->map('destination', 'source', 'trim');

// or

$transformer->map('destination', 'source', $transformer->rule()->filter('trim'));

$transformer = new ArrayTransformer();

$transformer->registerFilter(
    'custom1',
    function ($value) {
        return 'Custom value: ' . $value;
    }
);

// Usage
$transformer->map('destination', 'source', 'custom1');

// or

$transformer->map('destination', 'source', $transformer->rule()->filter('custom1'));

// It is possible to chain multiple filters
$transformer->map(
    'destination',
    'source',
    $transformer->rule()->filter('custom1')->filter('trim')->

$transformer = new ArrayTransformer();

$transformer->map(
    'destination',
    'source',
    $transformer->rule()->callback(
        function ($value) {
            return 'Callback value: ' . $value;
        }
    )
);

use Selective\Transformer\Filter\SprintfFilter;

$transformer = new ArrayTransformer();

// Convert the value using the sprintf function
$transformer->registerFilter('sprintf', new SprintfFilter());

// Usage
$transformer->map('destination', 'source', $transformer->rule()->filter('sprintf', 'Count: %d'));

// Turn all of that into a JSON string
$json = (string)json_encode($transformer->toArray($data));

// Set correct header
$response = $response->withHeader('Content-Type', 'application/json');

// Write json string to the response body
$response->getBody()->write($json);

return $response;

use Selective\Transformer\ArrayTransformer;

$pdo = new PDO($dsn, $username, $password, $options);

$statement = $pdo->query('SELECT id, username, first_name FROM users');
$rows = $statement->fetchAll();

$transformer = new ArrayTransformer();

$transformer->map('id', 'id', 'integer')
    ->map('username', 'username', 'string|

[
    [
        'id' => 1,
        'username' => 'sally',
        'first_name' => 'Sally',
    ]  
    // ... 
];