PHP code example of stratadox / deserializer
1. Go to this page and download the library: Download stratadox/deserializer 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/ */
stratadox / deserializer example snippets
use Stratadox\Deserializer\ObjectDeserializer;
$deserialize = ObjectDeserializer::forThe(Foo::class);
$foo = $deserialize->from([
'bar' => 'Bar.',
'baz' => 'BAZ!',
]);
assert($foo instanceof Foo);
assert('Bar.' === $foo->bar);
assert('BAZ!' === $foo->getBaz());
use Stratadox\Deserializer\CollectionDeserializer;
$deserialize = CollectionDeserializer::forImmutable(Numbers::class);
$numbers = $deserialize->from([10, 11, 12]);
assert($numbers instanceof Numbers);
assert(count($numbers) === 3);
assert($numbers[0] === 10);
assert($numbers[1] === 11);
assert($numbers[2] === 12);
use Stratadox\Deserializer\CollectionDeserializer;
$deserialize = CollectionDeserializer::forMutable(ArrayObject::class);
$numbers = $deserialize->from([10, 11, 12]);
assert($numbers instanceof ArrayObject);
assert(count($numbers) === 3);
assert($numbers[0] === 10);
assert($numbers[1] === 11);
assert($numbers[2] === 12);
use Stratadox\Deserializer\ArrayDeserializer;
$deserialize = ArrayDeserializer::make();
$input = ['foo', 'bar'];
$output = $deserialize->from($input);
assert($input === $output);
use Stratadox\Deserializer\ObjectDeserializer;
use Stratadox\Instantiator\Instantiator;
use Stratadox\Hydrator\ObjectHydrator;
$deserialize = ObjectDeserializer::using(
Instantiator::forThe(Foo::class),
ObjectHydrator::default()
);
use Stratadox\Deserializer\ForDataSets;
use Stratadox\Deserializer\Condition\HaveTheDiscriminatorValue;
use Stratadox\Deserializer\ObjectDeserializer;
use Stratadox\Deserializer\OneOfThese;
$deserialize = OneOfThese::deserializers(
ForDataSets::that(
HaveTheDiscriminatorValue::of('type', 'A'),
ObjectDeserializer::forThe(ChildA::class)
),
ForDataSets::that(
HaveTheDiscriminatorValue::of('type', 'B'),
ObjectDeserializer::forThe(ChildB::class)
)
);
$a = $deserialize->from([
'type' => 'A',
'property' => 'value',
]);
$b = $deserialize->from([
'type' => 'B',
'attribute' => 'different value',
]);
assert($a instanceof ChildA);
assert($b instanceof ChildB);