| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 18 |
|
| 19 |
> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function getCollectionKeyTypes(): array |
| 152 |
{ |
| 153 |
return $this->collectionKeyType; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getCollectionValueTypes(): array |
| 164 |
{ |
| 165 |
return $this->collectionValueType; |
| 166 |
} |
| 167 |
} |