Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Mapping/ClassDiscriminatorMapping.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\Mapping;
13
14 /**
15 * @author Samuel Roze <[email protected]>
16 */
17 class ClassDiscriminatorMapping
18 {
19 /**
20 * @param array<string, string> $typesMapping
21 */
22 public function __construct(
23 private readonly string $typeProperty,
24 private array $typesMapping = [],
25 private readonly ?string $defaultType = null,
26 ) {
27 uasort($this->typesMapping, static function (string $a, string $b): int {
28 if (is_a($a, $b, true)) {
29 return -1;
30 }
31
32 if (is_a($b, $a, true)) {
33 return 1;
34 }
35
36 return 0;
37 });
38 }
39
40 public function getTypeProperty(): string
41 {
42 return $this->typeProperty;
43 }
44
45 public function getClassForType(string $type): ?string
46 {
47 return $this->typesMapping[$type] ?? null;
48 }
49
50 public function getMappedObjectType(object|string $object): ?string
51 {
52 foreach ($this->typesMapping as $type => $typeClass) {
53 if (is_a($object, $typeClass, true)) {
54 return $type;
55 }
56 }
57
58 return null;
59 }
60
61 public function getTypesMapping(): array
62 {
63 return $this->typesMapping;
64 }
65
66 public function getDefaultType(): ?string
67 {
68 return $this->defaultType;
69 }
70 }