diff --git a/doc/reference/annotations.rst b/doc/reference/annotations.rst index c17b1e2b5..ca517551e 100644 --- a/doc/reference/annotations.rst +++ b/doc/reference/annotations.rst @@ -1,53 +1,6 @@ Annotations =========== -PHP 8 support -------------- - -JMS serializer now supports PHP 8 attributes, with a few caveats: -- To avoid BC breaks, all annotations must use named parameters. -- Due to the missing support for nested annotations, the syntax for a few annotations has changed - -Converting your annotations to attributes ------------------------------------------ - -Example: - -.. code-block :: php - - /** - * @VirtualProperty( - * "classlow", - * exp="object.getVirtualValue(1)", - * options={@Until("8")} - * ) - * @VirtualProperty( - * "classhigh", - * exp="object.getVirtualValue(8)", - * options={@Since("6")} - * ) - */ - #[VirtualProperty(name: 'classlow', exp: 'object.getVirtualValue(1)', options: [[Until::class, ['8']]])] - #[VirtualProperty(name: 'classhigh', exp: 'object.getVirtualValue(8)', options: [[Since::class, ['6']]])] - class ObjectWithVersionedVirtualProperties - { - /** - * @Groups({"versions"}) - * @VirtualProperty - * @SerializedName("low") - * @Until("8") - */ - #[Groups(groups: ['versions'])] - #[VirtualProperty] - #[SerializedName(name: 'low')] - #[Until(version: '8')] - public function getVirtualLowValue() - { - return 1; - } - ... - - @ExclusionPolicy ~~~~~~~~~~~~~~~~ This annotation can be defined on a class to indicate the exclusion strategy @@ -902,3 +855,53 @@ Resulting XML: + + +PHP 8 support +------------- + +JMS serializer now supports PHP 8 attributes, with a few caveats: +- Due to the missing support for nested annotations, the syntax for a few annotations has changed + (see the ``VirtualProperty`` ``options`` syntax here below) +- There is an edge case when setting this exact serialization group ``#[Groups(['value' => 'any value here'])]``. + (when there is only one item in th serialization groups array and has as key ``value`` the attribute will not work as expected, + please use the alternative syntax ``#[Groups(groups: ['value' => 'any value here'])]`` that works with no issues), + +Converting your annotations to attributes +----------------------------------------- + +Example: + +.. code-block :: php + + /** + * @VirtualProperty( + * "classlow", + * exp="object.getVirtualValue(1)", + * options={@Until("8")} + * ) + * @VirtualProperty( + * "classhigh", + * exp="object.getVirtualValue(8)", + * options={@Since("6")} + * ) + */ + #[VirtualProperty('classlow', exp: 'object.getVirtualValue(1)', options: [[Until::class, ['8']]])] + #[VirtualProperty('classhigh', exp: 'object.getVirtualValue(8)', options: [[Since::class, ['6']]])] + class ObjectWithVersionedVirtualProperties + { + /** + * @Groups({"versions"}) + * @VirtualProperty + * @SerializedName("low") + * @Until("8") + */ + #[Groups(['versions'])] + #[VirtualProperty] + #[SerializedName('low')] + #[Until('8')] + public function getVirtualLowValue() + { + return 1; + } + ... diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a3b51cbe7..95c672822 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,6 +6,7 @@ parameters: - '~Class Doctrine\\ODM\\MongoDB\\PersistentCollection not found~' - '~Class Symfony\\Component\\Translation\\TranslatorInterface not found~' - '#Instantiated class Doctrine\\Common\\Cache\\FilesystemCache not found\.#' + - '#Constructor of class JMS\\Serializer\\Annotation\\.*? has an unused parameter#' paths: - %currentWorkingDirectory%/src diff --git a/src/Annotation/AccessType.php b/src/Annotation/AccessType.php index b75d243c4..514a91616 100644 --- a/src/Annotation/AccessType.php +++ b/src/Annotation/AccessType.php @@ -13,6 +13,8 @@ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)] final class AccessType { + use AnnotationUtilsTrait; + /** * @Required * @var string|null @@ -21,14 +23,6 @@ final class AccessType public function __construct(array $values = [], ?string $type = null) { - if (array_key_exists('value', $values)) { - $type = $values['value']; - } - - if (array_key_exists('type', $values)) { - $type = $values['type']; - } - - $this->type = $type; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/Accessor.php b/src/Annotation/Accessor.php index 62e518bcb..da88e782f 100644 --- a/src/Annotation/Accessor.php +++ b/src/Annotation/Accessor.php @@ -13,6 +13,8 @@ #[\Attribute(\Attribute::TARGET_PROPERTY)] final class Accessor { + use AnnotationUtilsTrait; + /** * @var string|null */ @@ -25,19 +27,6 @@ final class Accessor public function __construct(array $values = [], ?string $getter = null, ?string $setter = null) { - if (array_key_exists('value', $values)) { - $getter = $values['value']; - } - - if (array_key_exists('getter', $values)) { - $getter = $values['getter']; - } - - if (array_key_exists('setter', $values)) { - $setter = $values['setter']; - } - - $this->getter = $getter; - $this->setter = $setter; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/AccessorOrder.php b/src/Annotation/AccessorOrder.php index 0761c5455..7b1436c67 100644 --- a/src/Annotation/AccessorOrder.php +++ b/src/Annotation/AccessorOrder.php @@ -15,6 +15,9 @@ #[\Attribute(\Attribute::TARGET_CLASS)] final class AccessorOrder { + use AnnotationUtilsTrait; + use AnnotationUtilsTrait; + /** * @Required * @var string|null @@ -28,19 +31,6 @@ final class AccessorOrder public function __construct(array $values = [], ?string $order = null, array $custom = []) { - if (array_key_exists('value', $values)) { - $order = $values['value']; - } - - if (array_key_exists('order', $values)) { - $order = $values['order']; - } - - if (array_key_exists('custom', $values)) { - $custom = $values['custom']; - } - - $this->order = $order; - $this->custom = $custom; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/AnnotationUtilsTrait.php b/src/Annotation/AnnotationUtilsTrait.php new file mode 100644 index 000000000..1f35b083e --- /dev/null +++ b/src/Annotation/AnnotationUtilsTrait.php @@ -0,0 +1,40 @@ + $vars['values']]; + } else { + $values = $vars['values']; + } + + unset($vars['values']); + + if (array_key_exists('value', $values)) { + $values[key($vars)] = $values['value']; + unset($values['value']); + } + + foreach ($values as $key => $value) { + $vars[$key] = $value; + } + + foreach ($vars as $key => $value) { + if (!property_exists(static::class, $key)) { + throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, static::class)); + } + + $this->{$key} = $value; + } + } +} diff --git a/src/Annotation/Discriminator.php b/src/Annotation/Discriminator.php index 28e2b15b5..b84a311be 100644 --- a/src/Annotation/Discriminator.php +++ b/src/Annotation/Discriminator.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_CLASS)] class Discriminator { + use AnnotationUtilsTrait; + /** @var array */ public $map = []; @@ -25,25 +27,6 @@ class Discriminator public function __construct(array $values = [], string $field = 'type', array $groups = [], array $map = [], bool $disabled = false) { - if (array_key_exists('field', $values)) { - $field = $values['field']; - } - - if (array_key_exists('groups', $values)) { - $groups = $values['groups']; - } - - if (array_key_exists('map', $values)) { - $map = $values['map']; - } - - if (array_key_exists('disabled', $values)) { - $disabled = $values['disabled']; - } - - $this->field = $field; - $this->groups = $groups; - $this->map = $map; - $this->disabled = $disabled; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/Exclude.php b/src/Annotation/Exclude.php index 07a7f8bde..8660e9a42 100644 --- a/src/Annotation/Exclude.php +++ b/src/Annotation/Exclude.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] final class Exclude { + use AnnotationUtilsTrait; + /** * @var string|null */ @@ -18,14 +20,6 @@ final class Exclude public function __construct(array $values = [], ?string $if = null) { - if (array_key_exists('value', $values)) { - $if = $values['value']; - } - - if (array_key_exists('if', $values)) { - $if = $values['if']; - } - - $this->if = $if; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/ExclusionPolicy.php b/src/Annotation/ExclusionPolicy.php index 55b37c4b9..f5f462789 100644 --- a/src/Annotation/ExclusionPolicy.php +++ b/src/Annotation/ExclusionPolicy.php @@ -13,40 +13,25 @@ #[\Attribute(\Attribute::TARGET_CLASS)] final class ExclusionPolicy { + use AnnotationUtilsTrait; + use AnnotationUtilsTrait; + public const NONE = 'NONE'; public const ALL = 'ALL'; /** * @var string|null */ - public $policy = null; + public $policy = 'NONE'; - public function __construct(array $values = [], ?string $policy = null) + public function __construct($values = [], ?string $policy = null) { - $value = self::NONE; - - if (array_key_exists('value', $values)) { - $value = $values['value']; - } - - if (array_key_exists('policy', $values)) { - $value = $values['policy']; - } + $this->loadAnnotationParameters(get_defined_vars()); - if (null !== $policy) { - $value = $policy; - } - - if (!\is_string($value)) { - throw new RuntimeException('Exclusion policy value must be of string type.'); - } + $this->policy = strtoupper($this->policy); - $value = strtoupper($value); - - if (self::NONE !== $value && self::ALL !== $value) { + if (self::NONE !== $this->policy && self::ALL !== $this->policy) { throw new RuntimeException('Exclusion policy must either be "ALL", or "NONE".'); } - - $this->policy = $value; } } diff --git a/src/Annotation/Expose.php b/src/Annotation/Expose.php index ad766ff07..3f03743a1 100644 --- a/src/Annotation/Expose.php +++ b/src/Annotation/Expose.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class Expose { + use AnnotationUtilsTrait; + /** * @var string|null */ @@ -18,14 +20,6 @@ final class Expose public function __construct(array $values = [], ?string $if = null) { - if (array_key_exists('value', $values)) { - $if = $values['value']; - } - - if (array_key_exists('if', $values)) { - $if = $values['if']; - } - - $this->if = $if; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/Groups.php b/src/Annotation/Groups.php index 9d9ef1d7d..1e9d4c5d6 100644 --- a/src/Annotation/Groups.php +++ b/src/Annotation/Groups.php @@ -11,19 +11,25 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class Groups { + use AnnotationUtilsTrait; + /** @var array @Required */ public $groups = []; public function __construct(array $values = [], array $groups = []) { - if (array_key_exists('value', $values)) { - $groups = $values['value']; - } - - if (array_key_exists('groups', $values)) { - $groups = $values['groups']; + $vars = get_defined_vars(); + /* + if someone wants to set as Groups(['value' => '...']) this check will miserably fail (only one group with 'value' as only key). + That is because doctrine annotations uses for @Groups("abc") the same values content (buy validation will fail since groups has to be an array). + All the other cases should work as expected. + The alternative here is to use the explicit syntax Groups(groups=['value' => '...']) + */ + if (count($values) > 0 && (!isset($values['value']) || count($values) > 1) && 0 === count($groups)) { + $vars['groups'] = $values; + $vars['values'] = []; } - $this->groups = $groups; + $this->loadAnnotationParameters($vars); } } diff --git a/src/Annotation/MaxDepth.php b/src/Annotation/MaxDepth.php index ddebe83d1..82e7506b7 100644 --- a/src/Annotation/MaxDepth.php +++ b/src/Annotation/MaxDepth.php @@ -11,22 +11,16 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class MaxDepth { + use AnnotationUtilsTrait; + /** * @Required * @var int */ public $depth; - public function __construct(array $values = [], int $depth = 0) + public function __construct($values = [], int $depth = 0) { - if (array_key_exists('value', $values)) { - $depth = $values['value']; - } - - if (array_key_exists('depth', $values)) { - $depth = $values['depth']; - } - - $this->depth = $depth; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/ReadOnlyProperty.php b/src/Annotation/ReadOnlyProperty.php index 22c34d9b1..d748b68c8 100644 --- a/src/Annotation/ReadOnlyProperty.php +++ b/src/Annotation/ReadOnlyProperty.php @@ -13,6 +13,8 @@ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)] /* final */ class ReadOnlyProperty { + use AnnotationUtilsTrait; + /** * @var bool */ @@ -20,14 +22,6 @@ public function __construct(array $values = [], bool $readOnly = true) { - if (array_key_exists('value', $values)) { - $readOnly = $values['value']; - } - - if (array_key_exists('readOnly', $values)) { - $readOnly = $values['readOnly']; - } - - $this->readOnly = $readOnly; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/SerializedName.php b/src/Annotation/SerializedName.php index 4dad9fd66..f2b5a6446 100644 --- a/src/Annotation/SerializedName.php +++ b/src/Annotation/SerializedName.php @@ -11,21 +11,15 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class SerializedName { + use AnnotationUtilsTrait; + /** * @var string|null */ public $name = null; - public function __construct(array $values = [], ?string $name = null) + public function __construct($values = [], ?string $name = null) { - if (array_key_exists('value', $values)) { - $name = $values['value']; - } - - if (array_key_exists('name', $values)) { - $name = $values['name']; - } - - $this->name = $name; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/Type.php b/src/Annotation/Type.php index 4b43fc295..f1d87f385 100644 --- a/src/Annotation/Type.php +++ b/src/Annotation/Type.php @@ -11,22 +11,16 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class Type { + use AnnotationUtilsTrait; + /** * @Required * @var string|null */ public $name = null; - public function __construct(array $values = [], ?string $name = null) + public function __construct($values = [], ?string $name = null) { - if (array_key_exists('value', $values)) { - $name = $values['value']; - } - - if (array_key_exists('name', $values)) { - $name = $values['name']; - } - - $this->name = $name; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/Version.php b/src/Annotation/Version.php index ddebfdab9..4e5cc0dd4 100644 --- a/src/Annotation/Version.php +++ b/src/Annotation/Version.php @@ -6,22 +6,16 @@ abstract class Version { + use AnnotationUtilsTrait; + /** * @Required * @var string|null */ public $version = null; - public function __construct(array $values = [], ?string $version = null) + public function __construct($values = [], ?string $version = null) { - if (array_key_exists('value', $values)) { - $version = $values['value']; - } - - if (array_key_exists('version', $values)) { - $version = $values['version']; - } - - $this->version = $version; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/VirtualProperty.php b/src/Annotation/VirtualProperty.php index 841a34f9f..dd2dfec57 100644 --- a/src/Annotation/VirtualProperty.php +++ b/src/Annotation/VirtualProperty.php @@ -4,8 +4,6 @@ namespace JMS\Serializer\Annotation; -use JMS\Serializer\Exception\InvalidArgumentException; - /** * @Annotation * @Target({"METHOD", "CLASS"}) @@ -15,6 +13,8 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] final class VirtualProperty { + use AnnotationUtilsTrait; + /** * @var string|null */ @@ -30,28 +30,11 @@ final class VirtualProperty */ public $options = []; - public function __construct(array $data = [], ?string $name = null, ?string $exp = null, ?array $options = []) + public function __construct($values = [], ?string $name = null, ?string $exp = null, array $options = []) { - if (isset($data['value'])) { - $data['name'] = $data['value']; - unset($data['value']); - } - - foreach ($data as $key => $value) { - if (!property_exists(self::class, $key)) { - throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class)); - } - - $this->{$key} = $value; - } - - if (null !== $name) { - $this->name = $name; - } - - if (null !== $exp) { - $this->exp = $exp; - } + $vars = get_defined_vars(); + unset($vars['options']); + $this->loadAnnotationParameters($vars); if (0 !== count($options)) { $this->options = $options; diff --git a/src/Annotation/XmlAttribute.php b/src/Annotation/XmlAttribute.php index 43bd54d19..31fc46376 100644 --- a/src/Annotation/XmlAttribute.php +++ b/src/Annotation/XmlAttribute.php @@ -11,21 +11,15 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class XmlAttribute { + use AnnotationUtilsTrait; + /** * @var string|null */ public $namespace = null; - public function __construct(array $values = [], ?string $namespace = null) + public function __construct($values = [], ?string $namespace = null) { - if (array_key_exists('value', $values)) { - $namespace = $values['value']; - } - - if (array_key_exists('namespace', $values)) { - $namespace = $values['namespace']; - } - - $this->namespace = $namespace; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlCollection.php b/src/Annotation/XmlCollection.php index 95585e857..e34ff58a0 100644 --- a/src/Annotation/XmlCollection.php +++ b/src/Annotation/XmlCollection.php @@ -6,6 +6,8 @@ abstract class XmlCollection { + use AnnotationUtilsTrait; + /** * @var string */ @@ -28,25 +30,6 @@ abstract class XmlCollection public function __construct(array $values = [], string $entry = 'entry', bool $inline = false, ?string $namespace = null, bool $skipWhenEmpty = true) { - if (array_key_exists('entry', $values)) { - $entry = $values['entry']; - } - - if (array_key_exists('inline', $values)) { - $inline = $values['inline']; - } - - if (array_key_exists('namespace', $values)) { - $namespace = $values['namespace']; - } - - if (array_key_exists('skipWhenEmpty', $values)) { - $skipWhenEmpty = $values['skipWhenEmpty']; - } - - $this->entry = $entry; - $this->inline = $inline; - $this->namespace = $namespace; - $this->skipWhenEmpty = $skipWhenEmpty; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlDiscriminator.php b/src/Annotation/XmlDiscriminator.php index 3bf56383b..bcb0e7950 100644 --- a/src/Annotation/XmlDiscriminator.php +++ b/src/Annotation/XmlDiscriminator.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_CLASS)] class XmlDiscriminator { + use AnnotationUtilsTrait; + /** * @var bool */ @@ -28,24 +30,6 @@ class XmlDiscriminator public function __construct(array $values = [], bool $attribute = false, bool $cdata = false, ?string $namespace = null) { - if (array_key_exists('value', $values)) { - $namespace = $values['value']; - } - - if (array_key_exists('attribute', $values)) { - $attribute = $values['attribute']; - } - - if (array_key_exists('cdata', $values)) { - $cdata = $values['cdata']; - } - - if (array_key_exists('namespace', $values)) { - $namespace = $values['namespace']; - } - - $this->attribute = $attribute; - $this->cdata = $cdata; - $this->namespace = $namespace; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlElement.php b/src/Annotation/XmlElement.php index 96ccb6675..bbf198283 100644 --- a/src/Annotation/XmlElement.php +++ b/src/Annotation/XmlElement.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class XmlElement { + use AnnotationUtilsTrait; + /** * @var bool */ @@ -23,15 +25,6 @@ final class XmlElement public function __construct(array $values = [], bool $cdata = true, ?string $namespace = null) { - if (array_key_exists('cdata', $values)) { - $cdata = $values['cdata']; - } - - if (array_key_exists('namespace', $values)) { - $namespace = $values['namespace']; - } - - $this->cdata = $cdata; - $this->namespace = $namespace; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlMap.php b/src/Annotation/XmlMap.php index d882932fb..f1f86cdb2 100644 --- a/src/Annotation/XmlMap.php +++ b/src/Annotation/XmlMap.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class XmlMap extends XmlCollection { + use AnnotationUtilsTrait; + /** * @var string */ @@ -18,11 +20,6 @@ final class XmlMap extends XmlCollection public function __construct(array $values = [], string $keyAttribute = '_key', string $entry = 'entry', bool $inline = false, ?string $namespace = null, bool $skipWhenEmpty = true) { - if (array_key_exists('keyAttribute', $values)) { - $keyAttribute = $values['keyAttribute']; - } - - parent::__construct($values, $entry, $inline, $namespace, $skipWhenEmpty); - $this->keyAttribute = $keyAttribute; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlNamespace.php b/src/Annotation/XmlNamespace.php index 43929b48d..5568aede6 100644 --- a/src/Annotation/XmlNamespace.php +++ b/src/Annotation/XmlNamespace.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] final class XmlNamespace { + use AnnotationUtilsTrait; + /** * @Required * @var string|null @@ -24,15 +26,6 @@ final class XmlNamespace public function __construct(array $values = [], ?string $uri = null, string $prefix = '') { - if (array_key_exists('uri', $values)) { - $uri = $values['uri']; - } - - if (array_key_exists('prefix', $values)) { - $prefix = $values['prefix']; - } - - $this->uri = $uri; - $this->prefix = $prefix; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlRoot.php b/src/Annotation/XmlRoot.php index 948261ab9..9034d35da 100644 --- a/src/Annotation/XmlRoot.php +++ b/src/Annotation/XmlRoot.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_CLASS)] final class XmlRoot { + use AnnotationUtilsTrait; + /** * @Required * @var string|null @@ -27,26 +29,8 @@ final class XmlRoot */ public $prefix = null; - public function __construct(array $values = [], ?string $name = null, ?string $namespace = null, ?string $prefix = null) + public function __construct($values = [], ?string $name = null, ?string $namespace = null, ?string $prefix = null) { - if (array_key_exists('value', $values)) { - $name = $values['value']; - } - - if (array_key_exists('name', $values)) { - $name = $values['name']; - } - - if (array_key_exists('namespace', $values)) { - $namespace = $values['namespace']; - } - - if (array_key_exists('prefix', $values)) { - $prefix = $values['prefix']; - } - - $this->name = $name; - $this->namespace = $namespace; - $this->prefix = $prefix; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/src/Annotation/XmlValue.php b/src/Annotation/XmlValue.php index 7873aac07..bb49967f6 100644 --- a/src/Annotation/XmlValue.php +++ b/src/Annotation/XmlValue.php @@ -11,6 +11,8 @@ #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class XmlValue { + use AnnotationUtilsTrait; + /** * @var bool */ @@ -18,14 +20,6 @@ final class XmlValue public function __construct(array $values = [], bool $cdata = true) { - if (array_key_exists('value', $values)) { - $cdata = $values['value']; - } - - if (array_key_exists('cdata', $values)) { - $cdata = $values['cdata']; - } - - $this->cdata = $cdata; + $this->loadAnnotationParameters(get_defined_vars()); } } diff --git a/tests/Fixtures/BlogPost.php b/tests/Fixtures/BlogPost.php index 0b4722eb4..db0e7b8a6 100644 --- a/tests/Fixtures/BlogPost.php +++ b/tests/Fixtures/BlogPost.php @@ -34,9 +34,9 @@ class BlogPost * @XmlElement(cdata=false) * @Groups({"comments","post"}) */ - #[Type(name: 'string')] + #[Type('string')] #[XmlElement(cdata: false)] - #[Groups(groups: ['comments', 'post'])] + #[Groups(['comments', 'post'])] private $id = 'what_a_nice_id'; /** @@ -64,7 +64,7 @@ class BlogPost * @Groups({"post"}) */ #[Type(name: 'boolean')] - #[SerializedName(name: 'is_published')] + #[SerializedName('is_published')] #[XmlAttribute] #[Groups(groups: ['post'])] private $published;