Debian Codegraph @

lib/3rdParty/composer/symfony/property-info/Type.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\PropertyInfo;
13
14 trigger_deprecation('symfony/property-info', '7.3', 'The "%s" class is deprecated. Use "%s" class from "symfony/type-info" instead.', Type::class, \Symfony\Component\TypeInfo\Type::class);
15
16 /**
17 * Type value object (immutable).
18 *
19 * @author Kévin Dunglas <[email protected]>
20 *
21 * @deprecated since Symfony 7.3, use "Symfony\Component\TypeInfo\Type" class from "symfony/type-info" instead
22 *
23 * @final
24 */
25 class Type
26 {
27 public const BUILTIN_TYPE_INT = 'int';
28 public const BUILTIN_TYPE_FLOAT = 'float';
29 public const BUILTIN_TYPE_STRING = 'string';
30 public const BUILTIN_TYPE_BOOL = 'bool';
31 public const BUILTIN_TYPE_RESOURCE = 'resource';
32 public const BUILTIN_TYPE_OBJECT = 'object';
33 public const BUILTIN_TYPE_ARRAY = 'array';
34 public const BUILTIN_TYPE_NULL = 'null';
35 public const BUILTIN_TYPE_FALSE = 'false';
36 public const BUILTIN_TYPE_TRUE = 'true';
37 public const BUILTIN_TYPE_CALLABLE = 'callable';
38 public const BUILTIN_TYPE_ITERABLE = 'iterable';
39
40 /**
41 * List of PHP builtin types.
42 *
43 * @var string[]
44 */
45 public static array $builtinTypes = [
46 self::BUILTIN_TYPE_INT,
47 self::BUILTIN_TYPE_FLOAT,
48 self::BUILTIN_TYPE_STRING,
49 self::BUILTIN_TYPE_BOOL,
50 self::BUILTIN_TYPE_RESOURCE,
51 self::BUILTIN_TYPE_OBJECT,
52 self::BUILTIN_TYPE_ARRAY,
53 self::BUILTIN_TYPE_CALLABLE,
54 self::BUILTIN_TYPE_FALSE,
55 self::BUILTIN_TYPE_TRUE,
56 self::BUILTIN_TYPE_NULL,
57 self::BUILTIN_TYPE_ITERABLE,
58 ];
59
60 /**
61 * List of PHP builtin collection types.
62 *
63 * @var string[]
64 */
65 public static array $builtinCollectionTypes = [
66 self::BUILTIN_TYPE_ARRAY,
67 self::BUILTIN_TYPE_ITERABLE,
68 ];
69
70 private array $collectionKeyType;
71 private array $collectionValueType;
72
73 /**
74 * @param Type[]|Type|null $collectionKeyType
75 * @param Type[]|Type|null $collectionValueType
76 *
77 * @throws \InvalidArgumentException
78 */
79 public function __construct(
80 private string $builtinType,
81 private bool $nullable = false,
82 private ?string $class = null,
83 private bool $collection = false,
84 array|self|null $collectionKeyType = null,
85 array|self|null $collectionValueType = null,
86 ) {
87 if (!\in_array($builtinType, self::$builtinTypes, true)) {
88 throw new \InvalidArgumentException(\sprintf('"%s" is not a valid PHP type.', $builtinType));
89 }
90
91 $this->collectionKeyType = $this->validateCollectionArgument($collectionKeyType, 5, '$collectionKeyType') ?? [];
92 $this->collectionValueType = $this->validateCollectionArgument($collectionValueType, 6, '$collectionValueType') ?? [];
93 }
94
95 private function validateCollectionArgument(array|self|null $collectionArgument, int $argumentIndex, string $argumentName): ?array
96 {
97 if (null === $collectionArgument) {
98 return null;
99 }
100
101 if (\is_array($collectionArgument)) {
102 foreach ($collectionArgument as $type) {
103 if (!$type instanceof self) {
104 throw new \TypeError(\sprintf('"%s()": Argument #%d (%s) must be of type "%s[]", "%s" or "null", array value "%s" given.', __METHOD__, $argumentIndex, $argumentName, self::class, self::class, get_debug_type($collectionArgument)));
105 }
106 }
107
108 return $collectionArgument;
109 }
110
111 return [$collectionArgument];
112 }
113
114 /**
115 * Gets built-in type.
116 *
117 * Can be bool, int, float, string, array, object, resource, null, callback or iterable.
118 */
119 public function getBuiltinType(): string
120 {
121 return $this->builtinType;
122 }
123
124 public function isNullable(): bool
125 {
126 return $this->nullable;
127 }
128
129 /**
130 * Gets the class name.
131 *
132 * Only applicable if the built-in type is object.
133 */
134 public function getClassName(): ?string
135 {
136 return $this->class;
137 }
138
139 public function isCollection(): bool
140 {
141 return $this->collection;
142 }
143
144 /**
145 * Gets collection key types.
146 *
147 * Only applicable for a collection type.
148 *
149 * @return Type[]
150 */
151 public function getCollectionKeyTypes(): array
152 {
153 return $this->collectionKeyType;
154 }
155
156 /**
157 * Gets collection value types.
158 *
159 * Only applicable for a collection type.
160 *
161 * @return Type[]
162 */
163 public function getCollectionValueTypes(): array
164 {
165 return $this->collectionValueType;
166 }
167 }