PHP code example of morebec / orkestra-normalization
1. Go to this page and download the library: Download morebec/orkestra-normalization 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/ */
use Morebec\Orkestra\Normalization\ObjectNormalizer;$project = new Project('prj123456789', 'A new Project', 'This is our latest project');
$project->addTask(new Task('tsk123456789', 'Deploy to production', $dueDate));
// Normalize
$normalizer = new ObjectNormalizer();
$data = $normalizer->normalize($project);
print_r($data);
// Would print:
[
'id' => 'prj123456789',
'title' => 'A new Project',
'description' => 'This is our latest project',
'tasks' => [
[
'id' => 'tsk123456789',
'name' => 'Deploy to production',
'dueDate' => '2021-01-01T10:25:55+00:00',
'completed' => false
]
]
];
// Would print:
use Morebec\Orkestra\Normalization\ObjectNormalizer;$data = [
'id' => 'prj123456789',
'title' => 'A new Project',
'description' => 'This is our latest project',
'tasks' => [
[
'id' => 'tsk123456789',
'name' => 'Deploy to production',
'dueDate' => '2021-01-01T10:25:55+00:00',
'completed' => false
]
]
];
$normalizer = new ObjectNormalizer();
$project = $normalizer->denormalize($data, Project::class);
class Username {
/** @var string */
private $value;
public function __construct(string $value) {
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
}
[
'value' => 'the_username'
];
use Morebec\Orkestra\Normalization\Denormalizer\DenormalizationContextInterface;
use Morebec\Orkestra\Normalization\ObjectNormalizer;
use Morebec\Orkestra\Normalization\Normalizer\ObjectNormalizer\FluentNormalizer;
use Morebec\Orkestra\Normalization\Denormalizer\ObjectDenormalizer\FluentDenormalizer;
$normalizer = new ObjectNormalizer();
$normalizer->addNormalizer(FluentNormalizer::for(Username::class)->asString());
$normalizer->addDenormalizer(FluentDenormalizer::for(Username::class)->as(static function (DenormalizationContextInterface $context) {
return new Username($context->getValue());
}));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.