|
| 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