Skip to content

Commit

Permalink
Improved EAVDataDenormalizer with ability to merge into existing data…
Browse files Browse the repository at this point in the history
… for EAV relations

This is used when denormalizing at the same time to related objects, one without any information on it's identifier
  • Loading branch information
VincentChalnot committed Jun 15, 2020
1 parent 387baf6 commit 3ad6358
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Serializer/Denormalizer/EAVDataDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
Expand All @@ -50,6 +51,8 @@ class EAVDataDenormalizer implements DenormalizerInterface, DenormalizerAwareInt
{
use ObjectToPopulateTrait;

public const ORIGINAL_DATA = 'original_data';

/** @var FamilyRegistry */
protected $familyRegistry;

Expand Down Expand Up @@ -211,6 +214,7 @@ protected function handleAttributeValue(
$format,
array $context
) {
$context[static::ORIGINAL_DATA] = $entity;
if (null === $normalizedValue) {
$value = null;
} elseif ($family->hasAttribute($attributeCode)) {
Expand Down Expand Up @@ -355,6 +359,16 @@ protected function denormalizeEAVAttribute(
$context['family'] = array_pop($allowedFamilies);
}
$context = array_merge($context, $additionalContext);
if (array_key_exists('merge_with_existing', $options)
&& $options['merge_with_existing']
&& array_key_exists(static::ORIGINAL_DATA, $context)
) {
$entity = $context[static::ORIGINAL_DATA];
if ($entity instanceof DataInterface) {
// EAV Context is already set in entity
$context[AbstractNormalizer::OBJECT_TO_POPULATE] = $entity->get($attribute->getCode());
}
}

return $this->denormalizeRelation($value, $targetClass, $format, $context, $attributeReference);
}
Expand Down

0 comments on commit 3ad6358

Please sign in to comment.