| 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\TypeInfo\Type; |
| 13 |
|
| 14 |
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\TypeInfo\Type; |
| 16 |
use Symfony\Component\TypeInfo\TypeIdentifier; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents a key/value collection type. |
| 20 |
* |
| 21 |
* @author Mathias Arlaud <[email protected]> |
| 22 |
* @author Baptiste Leduc <[email protected]> |
| 23 |
* |
| 24 |
* @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType|GenericType |
| 25 |
* |
| 26 |
* @implements WrappingTypeInterface<T> |
| 27 |
*/ |
| 28 |
class CollectionType extends Type implements WrappingTypeInterface |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @param T $type |
| 32 |
*/ |
| 33 |
public function __construct( |
| 34 |
private readonly BuiltinType|ObjectType|GenericType $type, |
| 35 |
private readonly bool $isList = false, |
| 36 |
) { |
| 37 |
if ($type instanceof BuiltinType && TypeIdentifier::ARRAY !== $type->getTypeIdentifier() && TypeIdentifier::ITERABLE !== $type->getTypeIdentifier()) { |
| 38 |
throw new InvalidArgumentException(\sprintf('Cannot create "%s" with "%s" type.', self::class, $type)); |
| 39 |
} |
| 40 |
|
| 41 |
if ($this->isList()) { |
| 42 |
if (!$type->isIdentifiedBy(TypeIdentifier::ARRAY)) { |
| 43 |
trigger_deprecation('symfony/type-info', '7.3', 'Creating a "%s" that is a list and not an array is deprecated and will throw a "%s" in 8.0.', self::class, InvalidArgumentException::class); |
| 44 |
// throw new InvalidArgumentException(\sprintf('Cannot create a "%s" as list when type is not "array".', self::class)); |
| 45 |
} |
| 46 |
|
| 47 |
$keyType = $this->getCollectionKeyType(); |
| 48 |
|
| 49 |
if (!$keyType instanceof BuiltinType || TypeIdentifier::INT !== $keyType->getTypeIdentifier()) { |
| 50 |
throw new InvalidArgumentException(\sprintf('"%s" is not a valid list key type.', (string) $keyType)); |
| 51 |
} |
| 52 |
} elseif ($type instanceof GenericType && $type->getWrappedType() instanceof BuiltinType && TypeIdentifier::ARRAY === $type->getWrappedType()->getTypeIdentifier()) { |
| 53 |
$keyType = $this->getCollectionKeyType(); |
| 54 |
|
| 55 |
$this->assertValidArrayKeyType($keyType); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
private function assertValidArrayKeyType(Type $keyType, ?Type $rootType = null): void |
| 60 |
{ |
| 61 |
$rootType ??= $keyType; |
| 62 |
|
| 63 |
if ($keyType instanceof UnionType) { |
| 64 |
foreach ($keyType->getTypes() as $type) { |
| 65 |
$this->assertValidArrayKeyType($type, $rootType); |
| 66 |
} |
| 67 |
|
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ($keyType instanceof TemplateType) { |
| 72 |
$this->assertValidArrayKeyType($keyType->getBound(), $rootType); |
| 73 |
|
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ($keyType instanceof BuiltinType && \in_array($keyType->getTypeIdentifier(), [TypeIdentifier::INT, TypeIdentifier::STRING], true)) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
throw new InvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $rootType)); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param array<Type> $types |
| 86 |
*/ |
| 87 |
public static function mergeCollectionValueTypes(array $types): Type |
| 88 |
{ |
| 89 |
if (!$types) { |
| 90 |
throw new InvalidArgumentException('The $types cannot be empty.'); |
| 91 |
} |
| 92 |
|
| 93 |
$normalizedTypes = []; |
| 94 |
$boolTypes = []; |
| 95 |
$objectTypes = []; |
| 96 |
|
| 97 |
foreach ($types as $type) { |
| 98 |
foreach (($type instanceof UnionType ? $type->getTypes() : [$type]) as $t) { |
| 99 |
// cannot create an union with a standalone type |
| 100 |
if ($t->isIdentifiedBy(TypeIdentifier::MIXED)) { |
| 101 |
return Type::mixed(); |
| 102 |
} |
| 103 |
|
| 104 |
if ($t->isIdentifiedBy(TypeIdentifier::TRUE, TypeIdentifier::FALSE, TypeIdentifier::BOOL)) { |
| 105 |
$boolTypes[] = $t; |
| 106 |
|
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
if ($t->isIdentifiedBy(TypeIdentifier::OBJECT)) { |
| 111 |
$objectTypes[] = $t; |
| 112 |
|
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$normalizedTypes[] = $t; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$boolTypes = array_unique($boolTypes); |
| 121 |
$objectTypes = array_unique($objectTypes); |
| 122 |
|
| 123 |
// cannot create an union with either "true" and "false", "bool" must be used instead |
| 124 |
if ($boolTypes) { |
| 125 |
$normalizedTypes[] = \count($boolTypes) > 1 ? Type::bool() : $boolTypes[0]; |
| 126 |
} |
| 127 |
|
| 128 |
// cannot create a union with either "object" and a class name, "object" must be used instead |
| 129 |
if ($objectTypes) { |
| 130 |
$hasBuiltinObjectType = array_filter($objectTypes, static fn (Type $t): bool => $t->isSatisfiedBy(static fn (Type $t): bool => $t instanceof BuiltinType)); |
| 131 |
$normalizedTypes = [...$normalizedTypes, ...($hasBuiltinObjectType ? [Type::object()] : $objectTypes)]; |
| 132 |
} |
| 133 |
|
| 134 |
$normalizedTypes = array_values(array_unique($normalizedTypes)); |
| 135 |
|
| 136 |
return \count($normalizedTypes) > 1 ? self::union(...$normalizedTypes) : $normalizedTypes[0]; |
| 137 |
} |
| 138 |
|
| 139 |
public function getWrappedType(): Type |
| 140 |
{ |
| 141 |
return $this->type; |
| 142 |
} |
| 143 |
|
| 144 |
public function isList(): bool |
| 145 |
{ |
| 146 |
return $this->isList; |
| 147 |
} |
| 148 |
|
| 149 |
public function getCollectionKeyType(): Type |
| 150 |
{ |
| 151 |
$defaultCollectionKeyType = self::arrayKey(); |
| 152 |
|
| 153 |
if ($this->type instanceof GenericType) { |
| 154 |
return match (\count($this->type->getVariableTypes())) { |
| 155 |
2 => $this->type->getVariableTypes()[0], |
| 156 |
1 => self::int(), |
| 157 |
default => $defaultCollectionKeyType, |
| 158 |
}; |
| 159 |
} |
| 160 |
|
| 161 |
return $defaultCollectionKeyType; |
| 162 |
} |
| 163 |
|
| 164 |
public function getCollectionValueType(): Type |
| 165 |
{ |
| 166 |
$defaultCollectionValueType = self::mixed(); |
| 167 |
|
| 168 |
if ($this->type instanceof GenericType) { |
| 169 |
return match (\count($this->type->getVariableTypes())) { |
| 170 |
2 => $this->type->getVariableTypes()[1], |
| 171 |
1 => $this->type->getVariableTypes()[0], |
| 172 |
default => $defaultCollectionValueType, |
| 173 |
}; |
| 174 |
} |
| 175 |
|
| 176 |
return $defaultCollectionValueType; |
| 177 |
} |
| 178 |
|
| 179 |
public function wrappedTypeIsSatisfiedBy(callable $specification): bool |
| 180 |
{ |
| 181 |
return $this->getWrappedType()->isSatisfiedBy($specification); |
| 182 |
} |
| 183 |
|
| 184 |
public function accepts(mixed $value): bool |
| 185 |
{ |
| 186 |
if (!parent::accepts($value)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
if ($this->isList() && (!\is_array($value) || !array_is_list($value))) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
$keyType = $this->getCollectionKeyType(); |
| 195 |
$valueType = $this->getCollectionValueType(); |
| 196 |
|
| 197 |
if (is_iterable($value)) { |
| 198 |
foreach ($value as $k => $v) { |
| 199 |
// key or value do not match |
| 200 |
if (!$keyType->accepts($k) || !$valueType->accepts($v)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
public function __toString(): string |
| 210 |
{ |
| 211 |
if ($this->isList && $this->type->isIdentifiedBy(TypeIdentifier::ARRAY)) { |
| 212 |
return 'list<'.$this->getCollectionValueType().'>'; |
| 213 |
} |
| 214 |
|
| 215 |
return (string) $this->type; |
| 216 |
} |
| 217 |
} |