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');
// ...
// 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|