Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Attribute/DiscriminatorMap.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 Samuel Roze <[email protected]>
18 */
19 #[\Attribute(\Attribute::TARGET_CLASS)]
20 class DiscriminatorMap
21 {
22 /**
23 * @param string $typeProperty The property holding the type discriminator
24 * @param array<string, class-string> $mapping The mapping between types and classes (i.e. ['admin_user' => AdminUser::class])
25 * @param ?string $defaultType The fallback value if nothing specified by $typeProperty
26 *
27 * @throws InvalidArgumentException
28 */
29 public function __construct(
30 public readonly string $typeProperty,
31 public readonly array $mapping,
32 public readonly ?string $defaultType = null,
33 ) {
34 if (!$typeProperty) {
35 throw new InvalidArgumentException(\sprintf('Parameter "typeProperty" given to "%s" cannot be empty.', static::class));
36 }
37
38 if (!$mapping) {
39 throw new InvalidArgumentException(\sprintf('Parameter "mapping" given to "%s" cannot be empty.', static::class));
40 }
41
42 if (null !== $this->defaultType && !\array_key_exists($this->defaultType, $this->mapping)) {
43 throw new InvalidArgumentException(\sprintf('Default type "%s" given to "%s" must be present in "mapping" types.', $this->defaultType, static::class));
44 }
45 }
46
47 #[\Deprecated('Use the "typeProperty" property instead', 'symfony/serializer:7.4')]
48 public function getTypeProperty(): string
49 {
50 return $this->typeProperty;
51 }
52
53 #[\Deprecated('Use the "mapping" property instead', 'symfony/serializer:7.4')]
54 public function getMapping(): array
55 {
56 return $this->mapping;
57 }
58
59 #[\Deprecated('Use the "defaultType" property instead', 'symfony/serializer:7.4')]
60 public function getDefaultType(): ?string
61 {
62 return $this->defaultType;
63 }
64 }
65
66 if (!class_exists(\Symfony\Component\Serializer\Annotation\DiscriminatorMap::class, false)) {
67 class_alias(DiscriminatorMap::class, \Symfony\Component\Serializer\Annotation\DiscriminatorMap::class);
68 }