Skip to content

Commit 1d60020

Browse files
committed
Add annotation -> attribute aliases
1 parent ef45660 commit 1d60020

File tree

70 files changed

+474
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+474
-296
lines changed

Annotation/Context.php

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,13 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
// do not deprecate in 6.4/7.0, to make it easier for the ecosystem to support 6.4, 7.4 and 8.0 simultaneously
1515

16-
/**
17-
* Annotation class for @Context().
18-
*
19-
* @Annotation
20-
* @NamedArgumentConstructor
21-
* @Target({"PROPERTY", "METHOD"})
22-
*
23-
* @author Maxime Steinhausser <[email protected]>
24-
*/
25-
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
26-
class Context
27-
{
28-
private array $groups;
29-
30-
/**
31-
* @param string|string[] $groups
32-
*
33-
* @throws InvalidArgumentException
34-
*/
35-
public function __construct(
36-
private readonly array $context = [],
37-
private readonly array $normalizationContext = [],
38-
private readonly array $denormalizationContext = [],
39-
string|array $groups = [],
40-
) {
41-
if (!$context && !$normalizationContext && !$denormalizationContext) {
42-
throw new InvalidArgumentException(sprintf('At least one of the "context", "normalizationContext", or "denormalizationContext" options must be provided as a non-empty array to "%s".', static::class));
43-
}
44-
45-
$this->groups = (array) $groups;
46-
47-
foreach ($this->groups as $group) {
48-
if (!\is_string($group)) {
49-
throw new InvalidArgumentException(sprintf('Parameter "groups" given to "%s" must be a string or an array of strings, "%s" given.', static::class, get_debug_type($group)));
50-
}
51-
}
52-
}
53-
54-
public function getContext(): array
55-
{
56-
return $this->context;
57-
}
58-
59-
public function getNormalizationContext(): array
60-
{
61-
return $this->normalizationContext;
62-
}
63-
64-
public function getDenormalizationContext(): array
65-
{
66-
return $this->denormalizationContext;
67-
}
16+
class_exists(\Symfony\Component\Serializer\Attribute\Context::class);
6817

69-
public function getGroups(): array
18+
if (false) {
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
class Context
7021
{
71-
return $this->groups;
7222
}
7323
}

Annotation/DiscriminatorMap.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
class_exists(\Symfony\Component\Serializer\Attribute\DiscriminatorMap::class);
1515

16-
/**
17-
* Annotation class for @DiscriminatorMap().
18-
*
19-
* @Annotation
20-
* @NamedArgumentConstructor
21-
* @Target({"CLASS"})
22-
*
23-
* @author Samuel Roze <[email protected]>
24-
*/
25-
#[\Attribute(\Attribute::TARGET_CLASS)]
26-
class DiscriminatorMap
27-
{
28-
public function __construct(
29-
private readonly string $typeProperty,
30-
private readonly array $mapping,
31-
) {
32-
if (empty($typeProperty)) {
33-
throw new InvalidArgumentException(sprintf('Parameter "typeProperty" given to "%s" cannot be empty.', static::class));
34-
}
35-
36-
if (empty($mapping)) {
37-
throw new InvalidArgumentException(sprintf('Parameter "mapping" given to "%s" cannot be empty.', static::class));
38-
}
39-
}
40-
41-
public function getTypeProperty(): string
42-
{
43-
return $this->typeProperty;
44-
}
45-
46-
public function getMapping(): array
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_CLASS)]
18+
class DiscriminatorMap
4719
{
48-
return $this->mapping;
4920
}
5021
}

Annotation/Groups.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
class_exists(\Symfony\Component\Serializer\Attribute\Groups::class);
1515

16-
/**
17-
* Annotation class for @Groups().
18-
*
19-
* @Annotation
20-
* @NamedArgumentConstructor
21-
* @Target({"PROPERTY", "METHOD", "CLASS"})
22-
*
23-
* @author Kévin Dunglas <[email protected]>
24-
*/
25-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS)]
26-
class Groups
27-
{
28-
/**
29-
* @var string[]
30-
*/
31-
private readonly array $groups;
32-
33-
/**
34-
* @param string|string[] $groups
35-
*/
36-
public function __construct(string|array $groups)
37-
{
38-
$this->groups = (array) $groups;
39-
40-
if (!$this->groups) {
41-
throw new InvalidArgumentException(sprintf('Parameter given to "%s" cannot be empty.', static::class));
42-
}
43-
44-
foreach ($this->groups as $group) {
45-
if (!\is_string($group) || '' === $group) {
46-
throw new InvalidArgumentException(sprintf('Parameter given to "%s" must be a string or an array of non-empty strings.', static::class));
47-
}
48-
}
49-
}
50-
51-
/**
52-
* @return string[]
53-
*/
54-
public function getGroups(): array
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS)]
18+
class Groups
5519
{
56-
return $this->groups;
5720
}
5821
}

Annotation/Ignore.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
/**
15-
* Annotation class for @Ignore().
16-
*
17-
* @Annotation
18-
* @Target({"PROPERTY", "METHOD"})
19-
*
20-
* @author Kévin Dunglas <[email protected]>
21-
*/
22-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
23-
final class Ignore
24-
{
14+
class_exists(\Symfony\Component\Serializer\Attribute\Ignore::class);
15+
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
18+
final class Ignore
19+
{
20+
}
2521
}

Annotation/MaxDepth.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
class_exists(\Symfony\Component\Serializer\Attribute\MaxDepth::class);
1515

16-
/**
17-
* Annotation class for @MaxDepth().
18-
*
19-
* @Annotation
20-
* @NamedArgumentConstructor
21-
* @Target({"PROPERTY", "METHOD"})
22-
*
23-
* @author Kévin Dunglas <[email protected]>
24-
*/
25-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
26-
class MaxDepth
27-
{
28-
public function __construct(private readonly int $maxDepth)
29-
{
30-
if ($maxDepth <= 0) {
31-
throw new InvalidArgumentException(sprintf('Parameter given to "%s" must be a positive integer.', static::class));
32-
}
33-
}
34-
35-
/**
36-
* @return int
37-
*/
38-
public function getMaxDepth()
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
18+
class MaxDepth
3919
{
40-
return $this->maxDepth;
4120
}
4221
}

Annotation/SerializedName.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
class_exists(\Symfony\Component\Serializer\Attribute\SerializedName::class);
1515

16-
/**
17-
* Annotation class for @SerializedName().
18-
*
19-
* @Annotation
20-
* @NamedArgumentConstructor
21-
* @Target({"PROPERTY", "METHOD"})
22-
*
23-
* @author Fabien Bourigault <[email protected]>
24-
*/
25-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
26-
final class SerializedName
27-
{
28-
public function __construct(private readonly string $serializedName)
29-
{
30-
if ('' === $serializedName) {
31-
throw new InvalidArgumentException(sprintf('Parameter given to "%s" must be a non-empty string.', self::class));
32-
}
33-
}
34-
35-
public function getSerializedName(): string
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
18+
final class SerializedName
3619
{
37-
return $this->serializedName;
3820
}
3921
}

Annotation/SerializedPath.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Annotation;
1313

14-
use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
15-
use Symfony\Component\PropertyAccess\PropertyPath;
16-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14+
class_exists(\Symfony\Component\Serializer\Attribute\SerializedPath::class);
1715

18-
/**
19-
* Annotation class for @SerializedPath().
20-
*
21-
* @Annotation
22-
* @NamedArgumentConstructor
23-
* @Target({"PROPERTY", "METHOD"})
24-
*
25-
* @author Tobias Bönner <[email protected]>
26-
*/
27-
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
28-
final class SerializedPath
29-
{
30-
private PropertyPath $serializedPath;
31-
32-
public function __construct(string $serializedPath)
33-
{
34-
try {
35-
$this->serializedPath = new PropertyPath($serializedPath);
36-
} catch (InvalidPropertyPathException $pathException) {
37-
throw new InvalidArgumentException(sprintf('Parameter given to "%s" must be a valid property path.', self::class));
38-
}
39-
}
40-
41-
public function getSerializedPath(): PropertyPath
16+
if (false) {
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
18+
final class SerializedPath
4219
{
43-
return $this->serializedPath;
4420
}
4521
}

Attribute/Context.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Attribute;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
16+
/**
17+
* Annotation class for @Context().
18+
*
19+
* @Annotation
20+
* @NamedArgumentConstructor
21+
* @Target({"PROPERTY", "METHOD"})
22+
*
23+
* @author Maxime Steinhausser <[email protected]>
24+
*/
25+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
26+
class Context
27+
{
28+
private array $groups;
29+
30+
/**
31+
* @param string|string[] $groups
32+
*
33+
* @throws InvalidArgumentException
34+
*/
35+
public function __construct(
36+
private readonly array $context = [],
37+
private readonly array $normalizationContext = [],
38+
private readonly array $denormalizationContext = [],
39+
string|array $groups = [],
40+
) {
41+
if (!$context && !$normalizationContext && !$denormalizationContext) {
42+
throw new InvalidArgumentException(sprintf('At least one of the "context", "normalizationContext", or "denormalizationContext" options must be provided as a non-empty array to "%s".', static::class));
43+
}
44+
45+
$this->groups = (array) $groups;
46+
47+
foreach ($this->groups as $group) {
48+
if (!\is_string($group)) {
49+
throw new InvalidArgumentException(sprintf('Parameter "groups" given to "%s" must be a string or an array of strings, "%s" given.', static::class, get_debug_type($group)));
50+
}
51+
}
52+
}
53+
54+
public function getContext(): array
55+
{
56+
return $this->context;
57+
}
58+
59+
public function getNormalizationContext(): array
60+
{
61+
return $this->normalizationContext;
62+
}
63+
64+
public function getDenormalizationContext(): array
65+
{
66+
return $this->denormalizationContext;
67+
}
68+
69+
public function getGroups(): array
70+
{
71+
return $this->groups;
72+
}
73+
}
74+
75+
if (!class_exists(\Symfony\Component\Serializer\Annotation\Context::class, false)) {
76+
class_alias(Context::class, \Symfony\Component\Serializer\Annotation\Context::class);
77+
}

0 commit comments

Comments
 (0)