for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Chubbyphp\Serialization\Normalizer\Relation;
use Chubbyphp\Serialization\Accessor\AccessorInterface;
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
use Chubbyphp\Serialization\SerializerLogicException;
final class EmbedManyFieldNormalizer implements FieldNormalizerInterface
{
/**
* @var AccessorInterface
*/
private $accessor;
public function __construct(AccessorInterface $accessor)
$this->accessor = $accessor;
}
* @param object $object
*
* @throws SerializerLogicException
* @return mixed
public function normalizeField(
string $path,
$object,
NormalizerContextInterface $context,
NormalizerInterface $normalizer = null
) {
if (null === $normalizer) {
throw SerializerLogicException::createMissingNormalizer($path);
if (null === $relatedObjects = $this->accessor->getValue($object)) {
return null;
$values = [];
foreach ($relatedObjects as $i => $relatedObject) {
$subPath = $path.'['.$i.']';
$values[$i] = $normalizer->normalize($relatedObject, $context, $subPath);
return $values;