PHP code example of iqomp / formatter
1. Go to this page and download the library: Download iqomp/formatter 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/ */
iqomp / formatter example snippets
return [
'formats' => [
'/format-name/' => [
'/field-name/' => [
'type' => '/field-trans-type/',
'@finalize' => '/field-trans-type/'
],
// ...
],
'my-object' => [
'id' => [
'type' => 'number',
'@finalize' => 'format'
],
'name' => [
'type' => 'text'
],
'created_at' => [
'type' => 'date',
'timezone' => 'UTC'
]
]
]
];
use Iqomp\Formatter\Formatter;
// formatting single object
$object = Formatter::format('my-object', $object, $options);
// formatting many object
$objects = Formatter::formatMany('my-object', $objects, $options);
use Iqomp\Formatter\Formattter;
$object = (object)[
'id' => 1,
'name' => 'User Name',
'created' => '2010-01-01 12:22:22'
];
$value = Formatter::applyType('number', $object->id, 'id', $object, [], []);
$res = Class::method($values, $field, $objects, $format, $options);
// ...
public static function addPrefix($values, $field, $objects, $format, $options)
{
$result = [];
$prefix = $options ?? '_';
foreach ($values as $val) {
$result[$val] = $prefix . $val;
}
return $result;
}
// ...
$res = Class::method($value, $field, $object, $format, $options)
// ...
public static function addPrefix($value, $field, $object, $format, $options)
{
$prefix = $options ?? '_';
return $prefix . $val;
}
// ...
namespace MyModule\Formatter;
class MyHandler
{
protected static function getPrefix($format, $options): string
{
$prefix = '_'; // default
// get prefix from user formatter config:
// 'formats' => [
// '/name/' => [
// '/field/' => [
// 'type' => 'prefix',
// 'prefix' => '_'
// ]
// ]
// ]
if (isset($format['prefix'])) {
$prefix = $format['prefix'];
}
// get prefix from user provided options:
// $res = Formatter::format('/name/', $/obj/, [
// '/field/' => '_'
// ])
if ($options) {
$prefix = '_';
}
return $prefix;
}
// the config:
// 'handlers' => [
// 'prefix-one' => [
// 'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixSingle',
// 'collective' => false
// ]
// ]
public static function addPrefixSingle($value, $field, $object, $format, $options)
{
$prefix = self::getPrefix($format, $options);
return $prefix . $value;
}
// the config
// 'handlers' => [
// 'prefix-two' => [
// 'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixCollective',
// 'collective' => true,
// 'field' => null
// ]
// ]
public static function addPrefixCollective($values, $field, $objects, $format, $options)
{
$prefix = self::getPrefix($format, $options);
$result = [];
foreach ($values as $value) {
$result[$value] = $prefix . $value;
}
return $result;
}
// the config
// 'handlers' => [
// 'prefix-three' => [
// 'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixById',
// 'collective' => true,
// 'field' => 'id'
// ]
// ]
public static function addPrefixById($values, $field, $objects, $format, $options)
{
$prefix = self::getPrefix($format, $options);
$result = [];
foreach ($objects as $object) {
$result[$object->id] = $prefix . $object->$field;
}
return $result;
}
// the config
// 'handlers' => [
// 'prefix-four' => [
// 'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixByMD5',
// 'collective' => '_MD5_',
// 'field' => null
// ]
// ]
public static function addPrefixByMD5($values, $field, $objects, $format, $options)
{
$prefix = self::getPrefix($format, $options);
$result = [];
foreach ($values as $value) {
$hash = md5($value);
$result[$hash] = $prefix . $value;
}
return $result;
}
}
// ...
return [
'formatter' => [
'handlers' => [
'prefix-one' => [
'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixSingle',
'collective' => false
],
'prefix-two' => [
'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixCollective',
'collective' => true,
'field' => null
],
'prefix-three' => [
'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixById',
'collective' => true,
'field' => 'id'
],
'prefix-four' => [
'handler' => 'MyModule\\Formatter\\MyHandler::addPrefixByMD5',
'collective' => '_MD5_',
'field' => null
]
]
]
];
/// ...
return [
'formats' => [
'/my-object/' => [
'/field/' => [
'type' => 'text',
'@default' => 'Hello World'
]
]
]
];
return [
'formats' => [
'/my-object/' => [
'@rest' => [
'type' => 'delete'
],
'id' => [
'type' => 'number',
],
'created_at' => [
'type' => 'date'
]
]
]
];
return [
'formats' => [
'/my-object/' => [
'user_id' => [
'type' => 'number'
],
'user' => [
'@clone' => 'user_id',
'type' => 'std-id'
]
]
]
];
return [
'user_id' => [
'@rename' => 'user',
'type' => 'std-id'
]
];
// ...
'/field/' => [
'type' => 'number',
'@finalize' => 'format'
]
// ...
// ...
'/field/' => [
'type' => 'bool' // 'boolean'
]
// ...
// ...
'/field/' => [
'type' => 'clone',
'source' => [
'field' => 'user.name.first',
'type' => 'text' // optional. convert the value to type text
]
]
// ...
// ...
'/field/' => [
'type' => 'clone',
'sources' => [
'name' => [
'field' => 'user.name.first',
'type' => 'text'
],
'bdate' => [
'field' => 'birthdate',
'type' => 'date'
]
]
]
// ...
// ...
'/field/' => [
'type' => 'custom',
'handler' => 'Class::method'
]
// ...
// ...
'/field/' => [
'type' => 'date',
'timezone' => 'UTC' // optional
]
// ...
// ...
'/field/' => [
'type' => 'delete'
]
// ...
// ...
'/field/' => [
'type' => 'embed'
]
// ...
// ...
'/field/' => [
'type' => 'interval'
]
// ...
// ...
'/field/' => [
'type' => 'multiple-text',
'separator' => ',' // 'json'
]
// ...
// ...
'/field/' => [
'type' => 'number',
'decimal' => 2 // optional
]
// ...
// ...
'/field/' => [
'type' => 'text'
]
// ...
// ...
'/field/' => [
'type' => 'json',
'format' => 'my-other-object'
]
// ...
// ...
'/field/' => [
'type' => 'join',
'fields' => ['My', 'name', 'is', '$user.name.first'],
'separator' => ' '
]
// ...
// ...
'/field/' => [
'type' => 'rename',
'to' => '/new-field/'
]
// ...
// ...
'/field/' => [
'type' => 'std-id'
]
// ...
// ...
'/field/' => [
'type' => 'switch',
'case' => [
'/name-1/' => [
'field' => '/object-property-name/',
'operator' => '=',
'expected' => 1,
'result' => [
'type' => 'number'
]
],
'/name-2' => [
'field' => '/object-property-name/',
'operator' => '>',
'expected' => 2,
'result' => [
'type' => 'text'
]
]
]
]
// ...
$val->format(string $format);
$val->timezone;
$val->time;
$val->value;
$val->{DateTime functions}(...);
$val->url;
$val->provider;
$val->html;
$val->format(string $format);
$val->interval();
$val->time;
$val->value;
$val->DateTime;
$val->DateInterval;
$val->value;
$val->format([$decimal=0, [$dec_separator=',', [$tho_separator='.']]]);
$val->id;
$val->chars(int $len);
$val->words(int $len);
$val->safe;
$val->clean;
$val->value;
bash
php bin/hyperf.php vendor:publish iqomp/formatter