1. Go to this page and download the library: Download web-fu/anymapper 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/ */
web-fu / anymapper example snippets
final class MyClass
{
private string $foo;
public string $bar;
public function setFoo(string $foo): MyClass {
$this->foo = $foo . ' and I was set in a setter';
return $this;
}
public function getFoo(): string
{
return $this->foo;
}
}
$source = [
'foo' => 'I am foo',
'bar' => 'I am bar',
];
// Fill an existing class
$destination = new MyClass();
(new \WebFu\AnyMapper\AnyMapper())
->map($source)
->into($destination)
->run();
echo $destination->getFoo(); // I am foo and I was set in a setter
echo PHP_EOL;
echo $destination->bar; // I am bar;
echo PHP_EOL;
// Create a new object of a class
$destination = (new \WebFu\AnyMapper\AnyMapper())
->map($source)
->as(MyClass::class)
->run();
echo $destination->getFoo(); // I am foo and I was set in a setter
echo PHP_EOL;
echo $destination->bar; // I am bar;
echo PHP_EOL;
// Use a strategy to customize mapping
final class MyClass
{
public DateTime $value;
}
$source = [
'value' => '2022-12-01',
];
$destination = (new \WebFu\AnyMapper\AnyMapper())
->map($source)
->using(
(new \WebFu\AnyMapper\Strategy\AllowedCastingStrategy())
->allow('string', DateTime::class)
)
->as(MyClass::class)
->run();
echo $destination->value->format('Y-m-d H:i:s'); // 2022-12-01 00:00:00
echo PHP_EOL;
final class MyClass
{
public int $value;
}
$source = [
'value' => true,
];
$destination = (new \WebFu\AnyMapper\AnyMapper())
->map($source)
->using(
(new \WebFu\AnyMapper\Strategy\CallbackCastingStrategy())
->addMethod(
'bool',
'int',
fn (bool $value) => (int) $value,
)
)
->as(MyClass::class)
->run();
echo $destination->value; // 1
// Use a strategy to customize mapping
final class MyClass
{
/** @var DateTime */
public $value;
}
$source = [
'value' => '2022-12-01',
];
$destination = (new \WebFu\AnyMapper\AnyMapper())
->map($source)
->using(
(new \WebFu\AnyMapper\Strategy\DocBlockDetectStrategy())
)
->as(MyClass::class)
->run();
echo $destination->value->format('Y-m-d H:i:s'); // 2022-12-01 00:00:00
echo PHP_EOL;
// Perform a standard serialization
final class Bar
{
private string $element;
public function getElement(): string
{
return $this->element;
}
public function setElement(string $element): Bar
{
$this->element = $element;
return $this;
}
}
final class Foo
{
/** @var Bar[] */
private array $bars;
/**
* @return array
*/
public function getBars(): array
{
return $this->bars;
}
/**
* @param array $bars
* @return Foo
*/
public function setBars(array $bars): Foo
{
$this->bars = $bars;
return $this;
}
}
$foo = new Foo();
$foo->setBars([
(new Bar())->setElement('string'),
]);
$destination = (new \WebFu\AnyMapper\AnyMapper())
->map($foo)
->serialize();
var_export($destination);
/*
array (
'bars' =>
array (
0 =>
array (
'element' => 'string',
),
),
)
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.