1. Go to this page and download the library: Download dobrosite/php-mapping 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/ */
dobrosite / php-mapping example snippets
public function input(mixed $source): mixed;
public function output(mixed $source): mixed;
use DobroSite\Mapping;
$mapper = new Mapping\Apply(
input: new Mapping\Callback(
input: fn(mixed $source) => is_numeric($source) ? new Mapping\FloatType() : new Mapping\AsIs(),
output: fn(mixed $source) => is_float($source) ? new Mapping\FloatType() : new Mapping\AsIs(),
)
);
$mapper->input('123.45'); // 123.45
$mapper->input('foo'); // 'foo'
use DobroSite\Mapping;
$mapper = new Mapping\ArrayValues([
'active' => new Mapping\BooleanType('yes', 'no'),
]);
$mapper->input(['active' => 'yes']); // ['active' => true]
$mapper->output(['active' => true]); // ['active' => 'yes']
use DobroSite\Mapping;
$mapper = new Mapping\AsIs();
$mapper->input('foo'); // 'foo'
$mapper->output('foo'); // 'foo'
use DobroSite\Mapping;
$mapper = new Mapping\BooleanType();
$mapper->input('true'); // true
$mapper->output(true); // 'true'
$mapper = new Mapping\BooleanType(true: 'да', false: 'нет');
$mapper->input('Нет'); // false
$mapper->output(false); // 'нет'
use DobroSite\Mapping;
$mapper = new Mapping\Callback(
input: strtolower(...),
output: strtoupper(...),
);
$mapper->input('FOO'); // 'foo'
$mapper->output('foo'); // 'FOO'
use DobroSite\Mapping;
$mapper = new Mapping\Chained(
$mapper1,
$mapper2,
// …
);
use DobroSite\Mapping;
$mapper = new Mapping\Collection(
new Mapping\FloatType(),
);
$mapper->input(['123.45', '67.89']); // [123.45, 67.89]
use DobroSite\Mapping;
$mapper = new Mapping\Constant(input: 'foo', output: 'bar');
$mapper->input(uniqid()); // 'foo'
$mapper->output(uniqid()); // 'bar'
use App\Foo;
use DobroSite\Mapping;
$mapper = new Mapping\Constructor(Foo::class);
$instanceOfFoo = $mapper->input(['foo' => 'foo value']);
use App\Foo;
use App\Bar;
use DobroSite\Mapping;
$mapper = new Mapping\ObjectConstructor(
Mapping\Callback(
fn(array $properties) => array_key_exists('bar', $properties) ? Bar::class : Foo::class,
)
);
$instanceOfFoo = $mapper->input(['foo' => 'foo value']);
$instanceOfBar = $mapper->input(['bar' => 'bar value']);
use App\SomeEnum;
use DobroSite\Mapping;
$mapper = new Mapping\EnumType(SomeEnum::class);
$mapper->input('foo'); // SomeEnum::Foo
$mapper->output(SomeEnum::Foo); // 'foo'
use DobroSite\Mapping;
$mapper = new Mapping\FloatType();
$mapper->input('1234.56'); // 1_234.56
$mapper = new Mapping\FloatType(
new \NumberFormatter('ru_RU', \NumberFormatter::DEFAULT_STYLE)
);
$mapper->input('1 234,56'); // 1_234.56
use DobroSite\Mapping;
$mapper = new Mapping\Map(['foo' => 'bar']);
$mapper->input('foo'); // 'bar'
$mapper->output('bar'); // 'foo'
use DobroSite\Mapping;
$float = new Mapping\FloatType();
$nullable = new Mapping\Nullable($float);
$nullable->input('123'); // 123
$nullable->input(null); // NULL
$float->input(null); // → InvalidArgumentException
use DobroSite\Mapping;
$mapper = new Mapping\ObjectFactory('\App\factory_function');
$mapper = new Mapping\ObjectFactory(factory_function(...));
$mapper = new Mapping\ObjectFactory([Factory::class, 'staticMethod']);
$mapper = new Mapping\ObjectFactory([$factory, 'method']);
$mapper = new Mapping\ClassType\CallableObjectFactory(
fn(string $foo, string $bar) => new SomeClass($foo, $bar)
);
use DobroSite\Mapping;
$mapper = new Mapping\ObjectMapper(
input: new Mapping\Constructor(Foo::class),
);