invalidSerializerField example


#[Package('core')] class DateFieldSerializer extends AbstractFieldSerializer
{
    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof DateField) {
            throw DataAbstractionLayerException::invalidSerializerField(DateField::class$field);
        }

        $value = $data->getValue();

        if (\is_string($value)) {
            $value = new \DateTimeImmutable($value);
        }

        if (\is_array($value) && \array_key_exists('date', $value)) {
            $value = new \DateTimeImmutable($value['date']);
        }

        
return $data;
    }

    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof VersionField) {
            throw DataAbstractionLayerException::invalidSerializerField(VersionField::class$field);
        }

        if ($data->getValue() === null) {
            $result = $this->normalize($field[$field->getPropertyName() => $data->getValue()]$parameters);
            $data->setValue($result[$field->getPropertyName()]);
        }

        yield $field->getStorageName() => Uuid::fromHexToBytes($data->getValue());
    }

    public function decode(Field $field, mixed $value): ?string
    {

#[Package('core')] class IntFieldSerializer extends AbstractFieldSerializer
{
    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof IntField) {
            throw DataAbstractionLayerException::invalidSerializerField(IntField::class$field);
        }

        $this->validateIfNeeded($field$existence$data$parameters);

        yield $field->getStorageName() => $data->getValue();
    }

    public function decode(Field $field, mixed $value): ?int
    {
        return $value === null ? null : (int) $value;
    }

    
#[Package('core')] class VersionDataPayloadFieldSerializer implements FieldSerializerInterface
{
    public function normalize(Field $field, array $data, WriteParameterBag $parameters): array
    {
        return $data;
    }

    public function encode(Field $field, EntityExistence $existence, KeyValuePair $data, WriteParameterBag $parameters): \Generator
    {
        if (!$field instanceof VersionDataPayloadField) {
            throw DataAbstractionLayerException::invalidSerializerField(VersionDataPayloadField::class$field);
        }

        yield $field->getStorageName() => $data->getValue();
    }

    public function decode(Field $field, mixed $value): mixed
    {
        if ($value === null) {
            return null;
        }

        
use Shopware\Core\Framework\Log\Package;

/** * @internal */
#[Package('core')] class ConfigJsonFieldSerializer extends JsonFieldSerializer
{
    public function encode(Field $field, EntityExistence $existence, KeyValuePair $data, WriteParameterBag $parameters): \Generator
    {
        if (!$field instanceof ConfigJsonField) {
            throw DataAbstractionLayerException::invalidSerializerField(ConfigJsonField::class$field);
        }

        $wrapped = [ConfigJsonField::STORAGE_KEY => $data->getValue()];
        $data->setValue($wrapped);

        return parent::encode($field$existence$data$parameters);
    }

    public function decode(Field $field, mixed $value): mixed
    {
        if (!$field instanceof ConfigJsonField) {
            
use Symfony\Component\Validator\Constraints\NotBlank;

/** * @internal */
#[Package('core')] class FkFieldSerializer extends AbstractFieldSerializer
{
    public function normalize(Field $field, array $data, WriteParameterBag $parameters): array
    {
        if (!$field instanceof FkField) {
            throw DataAbstractionLayerException::invalidSerializerField(FkField::class$field);
        }

        $value = $data[$field->getPropertyName()] ?? null;

        $writeContext = $parameters->getContext();

        if ($this->shouldUseContext($field, true, $value) && $writeContext->has($field->getReferenceDefinition()->getEntityName()$field->getReferenceField())) {
            $data[$field->getPropertyName()] = $writeContext->get($field->getReferenceDefinition()->getEntityName()$field->getReferenceField());
        }

        return $data;
    }
parent::__construct($validator$definitionRegistry);
        $this->configService = $configService;
    }

    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof PasswordField) {
            throw DataAbstractionLayerException::invalidSerializerField(PasswordField::class$field);
        }

        $this->validateIfNeeded($field$existence$data$parameters);

        $value = $data->getValue();
        if ($value) {
            $info = password_get_info($value);
            // if no password algorithm is detected, it might be plain text which needs to be encoded.             // otherwise, passthrough the possibly encoded string             if (!$info['algo']) {
                $value = password_hash((string) $value$field->getAlgorithm()$field->getHashOptions());
            }
 {
        parent::__construct($validator$definitionRegistry);
    }

    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof StringField) {
            throw DataAbstractionLayerException::invalidSerializerField(StringField::class$field);
        }

        $tmp = $data->getValue();
        if (\is_string($tmp)) {
            $tmp = trim($tmp);
        }

        if ($tmp === '' && !$field->is(AllowEmptyString::class)) {
            $data->setValue(null);
        }

        
use Shopware\Core\Framework\Log\Package;

/** * @internal */
#[Package('core')] class UpdatedByFieldSerializer extends FkFieldSerializer
{
    public function encode(Field $field, EntityExistence $existence, KeyValuePair $data, WriteParameterBag $parameters): \Generator
    {
        if (!($field instanceof UpdatedByField)) {
            throw DataAbstractionLayerException::invalidSerializerField(UpdatedByField::class$field);
        }

        if (!$existence->exists()) {
            return;
        }

        $context = $parameters->getContext()->getContext();
        $scope = $context->getScope();

        if (!\in_array($scope$field->getAllowedWriteScopes(), true)) {
            return;
        }
 {
        parent::__construct($validator$definitionRegistry);
    }

    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof LongTextField) {
            throw DataAbstractionLayerException::invalidSerializerField(LongTextField::class$field);
        }

        if ($data->getValue() === '' && !$field->is(AllowEmptyString::class)) {
            $data->setValue(null);
        }

        $this->validateIfNeeded($field$existence$data$parameters);

        $data->setValue($this->sanitize($this->sanitizer, $data$field$existence));

        $this->validateIfNeeded($field$existence$data$parameters);

        
use Shopware\Core\Framework\Uuid\Uuid;

/** * @internal */
#[Package('core')] class ReferenceVersionFieldSerializer implements FieldSerializerInterface
{
    public function normalize(Field $field, array $data, WriteParameterBag $parameters): array
    {
        if (!$field instanceof ReferenceVersionField) {
            throw DataAbstractionLayerException::invalidSerializerField(ReferenceVersionField::class$field);
        }

        $value = $data[$field->getPropertyName()] ?? null;
        if ($value === null && !$field->is(Required::class)) {
            return $data;
        }

        $definition = $parameters->getDefinition();

        $reference = $field->getVersionReferenceDefinition();

        
return $data;
    }

    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof IdField) {
            throw DataAbstractionLayerException::invalidSerializerField(IdField::class$field);
        }

        $value = $data->getValue();
        if ($value) {
            $this->validate([new UuidConstraint()]$data$parameters->getPath());
        } else {
            $value = Uuid::randomHex();
        }

        $parameters->getContext()->set($parameters->getDefinition()->getEntityName()$data->getKey()$value);

        

    /** * @throws DataAbstractionLayerException */
    public function encode(
        Field $field,
        EntityExistence $existence,
        KeyValuePair $data,
        WriteParameterBag $parameters
    ): \Generator {
        if (!$field instanceof UpdatedAtField) {
            throw DataAbstractionLayerException::invalidSerializerField(UpdatedAtField::class$field);
        }
        if (!$existence->exists()) {
            return;
        }

        $data->setValue(new \DateTime());

        yield from parent::encode($field$existence$data$parameters);
    }
}
use Shopware\Core\Framework\Log\Package;

/** * @internal */
#[Package('core')] class CreatedByFieldSerializer extends FkFieldSerializer
{
    public function encode(Field $field, EntityExistence $existence, KeyValuePair $data, WriteParameterBag $parameters): \Generator
    {
        if (!($field instanceof CreatedByField)) {
            throw DataAbstractionLayerException::invalidSerializerField(CreatedByField::class$field);
        }

        // only required for new entities         if ($existence->exists()) {
            return;
        }

        $context = $parameters->getContext()->getContext();
        $scope = $context->getScope();

        if (!\in_array($scope$field->getAllowedWriteScopes(), true)) {
            
/** * @internal */
    public function __construct(
        private readonly WriteCommandExtractor $writeExtractor
    ) {
    }

    public function normalize(Field $field, array $data, WriteParameterBag $parameters): array
    {
        if (!$field instanceof OneToManyAssociationField) {
            throw DataAbstractionLayerException::invalidSerializerField(OneToManyAssociationField::class$field);
        }

        $key = $field->getPropertyName();
        $value = $data[$key] ?? null;
        if ($value === null) {
            return $data;
        }

        $id = $parameters->getContext()->get($parameters->getDefinition()->getEntityName()$field->getLocalField());
        $reference = $field->getReferenceDefinition();

        
Home | Imprint | This part of the site doesn't use cookies.