for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Chubbyphp\Deserialization\Denormalizer\Relation;
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface;
use Chubbyphp\Deserialization\Denormalizer\DenormalizerInterface;
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
use Chubbyphp\Deserialization\DeserializerLogicException;
use Chubbyphp\Deserialization\DeserializerRuntimeException;
final class EmbedOneFieldDenormalizer implements FieldDenormalizerInterface
{
/**
* @var string
*/
private $class;
* @var AccessorInterface
private $accessor;
* @param string $class
public function __construct($class, AccessorInterface $accessor)
$this->class = $class;
$this->accessor = $accessor;
}
* @param object $object
* @param mixed $value
*
* @throws DeserializerLogicException
* @throws DeserializerRuntimeException
public function denormalizeField(
string $path,
$object,
$value,
DenormalizerContextInterface $context,
DenormalizerInterface $denormalizer = null
): void {
if (null === $value) {
$this->accessor->setValue($object, $value);
return;
if (null === $denormalizer) {
throw DeserializerLogicException::createMissingDenormalizer($path);
if (!is_array($value)) {
throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
$relatedObject = $this->accessor->getValue($object) ?? $this->class;
$this->accessor->setValue($object, $denormalizer->denormalize($relatedObject, $value, $context, $path));