<?php
namespace MyProject\Serialization;
use Chubbyphp\Serialization\Link\LinkBuilder;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingBuilder;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface;
use Chubbyphp\Serialization\Mapping\NormalizationLinkMapping;
use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingInterface;
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer;
use MyProject\Model\Model;
final class ModelMapping implements NormalizationObjectMappingInterface
{
/**
* @return string
*/
public function getClass(): string
{
return Model::class;
}
/**
* @return string
*/
public function getNormalizationType(): string
{
return 'model';
}
/**
* @param string $path
* @param string|null $type
*
* @return array<int, NormalizationFieldMappingInterface>
*/
public function getNormalizationFieldMappings(string $path, string $type = null): array
{
return [
NormalizationFieldMappingBuilder::create('id')->getMapping(),
NormalizationFieldMappingBuilder::create('name')->getMapping(),
];
}
/**
* @param string $path
*
* @return array<int, NormalizationFieldMappingInterface>
*/
public function getNormalizationEmbeddedFieldMappings(string $path): array
{
return [];
}
/**
* @param string $path
*
* @return array<int, NormalizationLinkMappingInterface>
*/
public function getNormalizationLinkMappings(string $path): array
{
return [
new NormalizationLinkMapping(
'self',
[],
new CallbackLinkNormalizer(
function (string $path, Model $model) {
return LinkBuilder::create('/api/model/' . $model->getId())
->setAttributes([
'method' => 'GET'
])
->getLink();
}
)
),
];
}
}
<?php
namespace MyProject\Model;
final class Model
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*
* @return self
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}