Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Attribute/Context.php

raw symbols SCIP
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 * @author Maxime Steinhausser <[email protected]>
18 */
19 #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20 class Context
21 {
22 public readonly array $groups;
23
24 /**
25 * @param array<string, mixed> $context The common context to use when serializing or deserializing
26 * @param array<string, mixed> $normalizationContext The context to use when serializing
27 * @param array<string, mixed> $denormalizationContext The context to use when deserializing
28 * @param string|string[] $groups The groups to use when serializing or deserializing
29 *
30 * @throws InvalidArgumentException
31 */
32 public function __construct(
33 public readonly array $context = [],
34 public readonly array $normalizationContext = [],
35 public readonly array $denormalizationContext = [],
36 string|array $groups = [],
37 ) {
38 if (!$context && !$normalizationContext && !$denormalizationContext) {
39 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));
40 }
41
42 $this->groups = (array) $groups;
43
44 foreach ($this->groups as $group) {
45 if (!\is_string($group)) {
46 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)));
47 }
48 }
49 }
50
51 #[\Deprecated('Use the "context" property instead', 'symfony/serializer:7.4')]
52 public function getContext(): array
53 {
54 return $this->context;
55 }
56
57 #[\Deprecated('Use the "normalizationContext" property instead', 'symfony/serializer:7.4')]
58 public function getNormalizationContext(): array
59 {
60 return $this->normalizationContext;
61 }
62
63 #[\Deprecated('Use the "denormalizationContext" property instead', 'symfony/serializer:7.4')]
64 public function getDenormalizationContext(): array
65 {
66 return $this->denormalizationContext;
67 }
68
69 #[\Deprecated('Use the "groups" property instead', 'symfony/serializer:7.4')]
70 public function getGroups(): array
71 {
72 return $this->groups;
73 }
74 }
75
76 if (!class_exists(\Symfony\Component\Serializer\Annotation\Context::class, false)) {
77 class_alias(Context::class, \Symfony\Component\Serializer\Annotation\Context::class);
78 }