vendor/shopware/core/Framework/DataAbstractionLayer/FieldSerializer/AbstractFieldSerializer.php line 63

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityTranslationDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Inherited;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\DataStack\KeyValuePair;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteParameterBag;
  13. use Shopware\Core\Framework\Log\Package;
  14. use Shopware\Core\Framework\Util\HtmlSanitizer;
  15. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  16. use Symfony\Component\Validator\Constraint;
  17. use Symfony\Component\Validator\ConstraintViolation;
  18. use Symfony\Component\Validator\ConstraintViolationList;
  19. use Symfony\Component\Validator\Validator\ValidatorInterface;
  20. /**
  21.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  22.  */
  23. #[Package('core')]
  24. abstract class AbstractFieldSerializer implements FieldSerializerInterface
  25. {
  26.     /**
  27.      * @var ValidatorInterface
  28.      */
  29.     protected $validator;
  30.     /**
  31.      * @var DefinitionInstanceRegistry
  32.      */
  33.     protected $definitionRegistry;
  34.     /**
  35.      * @var array<Constraint[]>
  36.      */
  37.     private array $cachedConstraints = [];
  38.     public function __construct(ValidatorInterface $validatorDefinitionInstanceRegistry $definitionRegistry)
  39.     {
  40.         $this->validator $validator;
  41.         $this->definitionRegistry $definitionRegistry;
  42.     }
  43.     public function normalize(Field $field, array $dataWriteParameterBag $parameters): array
  44.     {
  45.         return $data;
  46.     }
  47.     protected function validate(
  48.         array $constraints,
  49.         KeyValuePair $data,
  50.         string $path
  51.     ): void {
  52.         $violationList = new ConstraintViolationList();
  53.         foreach ($constraints as $constraint) {
  54.             $violations $this->validator->validate($data->getValue(), $constraint);
  55.             /** @var ConstraintViolation $violation */
  56.             foreach ($violations as $violation) {
  57.                 $fieldName $data->getKey();
  58.                 // correct pointer for json fields with pre-defined structure
  59.                 if ($violation->getPropertyPath()) {
  60.                     $property str_replace('][''/'$violation->getPropertyPath());
  61.                     $property trim($property'][');
  62.                     $fieldName .= '/' $property;
  63.                 }
  64.                 $fieldName '/' $fieldName;
  65.                 $violationList->add(
  66.                     new ConstraintViolation(
  67.                         $violation->getMessage(),
  68.                         $violation->getMessageTemplate(),
  69.                         $violation->getParameters(),
  70.                         $violation->getRoot(),
  71.                         $fieldName,
  72.                         $violation->getInvalidValue(),
  73.                         $violation->getPlural(),
  74.                         $violation->getCode(),
  75.                         $violation->getConstraint(),
  76.                         $violation->getCause()
  77.                     )
  78.                 );
  79.             }
  80.         }
  81.         if (\count($violationList)) {
  82.             throw new WriteConstraintViolationException($violationList$path);
  83.         }
  84.     }
  85.     protected function requiresValidation(
  86.         Field $field,
  87.         EntityExistence $existence,
  88.         $value,
  89.         WriteParameterBag $parameters
  90.     ): bool {
  91.         if ($value !== null) {
  92.             return true;
  93.         }
  94.         if ($existence->isChild() && $this->isInherited($field$parameters)) {
  95.             return false;
  96.         }
  97.         if ($existence->hasEntityName()
  98.             && $this->definitionRegistry->getByEntityName($existence->getEntityName()) instanceof EntityTranslationDefinition
  99.             && $parameters->getCurrentWriteLanguageId() !== Defaults::LANGUAGE_SYSTEM
  100.         ) {
  101.             return false;
  102.         }
  103.         return $field->is(Required::class);
  104.     }
  105.     protected function isInherited(Field $fieldWriteParameterBag $parameters): bool
  106.     {
  107.         if ($parameters->getDefinition()->isInheritanceAware()) {
  108.             return $field->is(Inherited::class);
  109.         }
  110.         if (!$parameters->getDefinition() instanceof EntityTranslationDefinition) {
  111.             return false;
  112.         }
  113.         $parent $parameters->getDefinition()->getParentDefinition();
  114.         $field $parent->getFields()->get($field->getPropertyName());
  115.         return $field->is(Inherited::class);
  116.     }
  117.     protected function validateIfNeeded(Field $fieldEntityExistence $existenceKeyValuePair $dataWriteParameterBag $parameters): void
  118.     {
  119.         if (!$this->requiresValidation($field$existence$data->getValue(), $parameters)) {
  120.             return;
  121.         }
  122.         $constraints $this->getCachedConstraints($field);
  123.         $this->validate($constraints$data$parameters->getPath());
  124.     }
  125.     /**
  126.      * @return Constraint[]
  127.      */
  128.     protected function getConstraints(Field $field): array
  129.     {
  130.         return [];
  131.     }
  132.     /**
  133.      * @return Constraint[]
  134.      */
  135.     protected function getCachedConstraints(Field $field): array
  136.     {
  137.         $key $field->getPropertyName() . spl_object_id($field);
  138.         if (\array_key_exists($key$this->cachedConstraints)) {
  139.             return $this->cachedConstraints[$key];
  140.         }
  141.         return $this->cachedConstraints[$key] = $this->getConstraints($field);
  142.     }
  143.     protected function sanitize(HtmlSanitizer $sanitizerKeyValuePair $dataField $fieldEntityExistence $existence): ?string
  144.     {
  145.         if ($data->getValue() === null) {
  146.             return null;
  147.         }
  148.         if (!$field->is(AllowHtml::class)) {
  149.             return strip_tags((string) $data->getValue());
  150.         }
  151.         $flag $field->getFlag(AllowHtml::class);
  152.         if ($flag instanceof AllowHtml && $flag->isSanitized()) {
  153.             $fieldKey sprintf('%s.%s', (string) $existence->getEntityName(), $field->getPropertyName());
  154.             return $sanitizer->sanitize((string) $data->getValue(), [], false$fieldKey);
  155.         }
  156.         return (string) $data->getValue();
  157.     }
  158. }