| 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 the exact shape of an array. |
| 20 |
* |
| 21 |
* @author Mathias Arlaud <[email protected]> |
| 22 |
* |
| 23 |
* @extends CollectionType<GenericType<BuiltinType<TypeIdentifier::ARRAY>>> |
| 24 |
*/ |
| 25 |
final class ArrayShapeType extends CollectionType |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var array<array{type: Type, optional: bool}> |
| 29 |
*/ |
| 30 |
private readonly array $shape; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param array<array{type: Type, optional: bool}> $shape |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
array $shape, |
| 37 |
private readonly ?Type $extraKeyType = null, |
| 38 |
private readonly ?Type $extraValueType = null, |
| 39 |
) { |
| 40 |
$keyTypes = []; |
| 41 |
$valueTypes = []; |
| 42 |
|
| 43 |
foreach ($shape as $k => $v) { |
| 44 |
$keyTypes[] = \is_int($k) ? Type::int() : Type::string(); |
| 45 |
$valueTypes[] = $v['type']; |
| 46 |
} |
| 47 |
|
| 48 |
if ($keyTypes) { |
| 49 |
$keyTypes = array_values(array_unique($keyTypes)); |
| 50 |
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0]; |
| 51 |
} else { |
| 52 |
$keyType = Type::arrayKey(); |
| 53 |
} |
| 54 |
|
| 55 |
$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed(); |
| 56 |
|
| 57 |
parent::__construct(self::generic(self::builtin(TypeIdentifier::ARRAY), $keyType, $valueType)); |
| 58 |
|
| 59 |
$sortedShape = $shape; |
| 60 |
ksort($sortedShape); |
| 61 |
|
| 62 |
$this->shape = $sortedShape; |
| 63 |
|
| 64 |
if ($this->extraKeyType xor $this->extraValueType) { |
| 65 |
throw new InvalidArgumentException(\sprintf('You must provide a value for "$%s" when "$%s" is provided.', $this->extraKeyType ? 'extraValueType' : 'extraKeyType', $this->extraKeyType ? 'extraKeyType' : 'extraValueType')); |
| 66 |
} |
| 67 |
|
| 68 |
if ($extraKeyType && !$extraKeyType->isIdentifiedBy(TypeIdentifier::INT, TypeIdentifier::STRING)) { |
| 69 |
throw new InvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $extraKeyType)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return array<array{type: Type, optional: bool}> |
| 75 |
*/ |
| 76 |
public function getShape(): array |
| 77 |
{ |
| 78 |
return $this->shape; |
| 79 |
} |
| 80 |
|
| 81 |
public function isSealed(): bool |
| 82 |
{ |
| 83 |
return null === $this->extraValueType; |
| 84 |
} |
| 85 |
|
| 86 |
public function getExtraKeyType(): ?Type |
| 87 |
{ |
| 88 |
return $this->extraKeyType; |
| 89 |
} |
| 90 |
|
| 91 |
public function getExtraValueType(): ?Type |
| 92 |
{ |
| 93 |
return $this->extraValueType; |
| 94 |
} |
| 95 |
|
| 96 |
public function accepts(mixed $value): bool |
| 97 |
{ |
| 98 |
if (!\is_array($value)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
foreach ($this->shape as $key => $shapeValue) { |
| 103 |
if (!($shapeValue['optional'] ?? false) && !\array_key_exists($key, $value)) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
foreach ($value as $key => $itemValue) { |
| 109 |
$valueType = $this->shape[$key]['type'] ?? false; |
| 110 |
|
| 111 |
if ($valueType && !$valueType->accepts($itemValue)) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if (!$valueType && ($this->isSealed() || !$this->extraKeyType->accepts($key) || !$this->extraValueType->accepts($itemValue))) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
public function __toString(): string |
| 124 |
{ |
| 125 |
$items = []; |
| 126 |
|
| 127 |
foreach ($this->shape as $key => $value) { |
| 128 |
$itemKey = \is_int($key) ? (string) $key : \sprintf("'%s'", $key); |
| 129 |
if ($value['optional'] ?? false) { |
| 130 |
$itemKey = \sprintf('%s?', $itemKey); |
| 131 |
} |
| 132 |
|
| 133 |
$items[] = \sprintf('%s: %s', $itemKey, $value['type']); |
| 134 |
} |
| 135 |
|
| 136 |
if (!$this->isSealed()) { |
| 137 |
$items[] = $this->extraKeyType->isIdentifiedBy(TypeIdentifier::INT) && $this->extraKeyType->isIdentifiedBy(TypeIdentifier::STRING) && $this->extraValueType->isIdentifiedBy(TypeIdentifier::MIXED) |
| 138 |
? '...' |
| 139 |
: \sprintf('...<%s, %s>', $this->extraKeyType, $this->extraValueType); |
| 140 |
} |
| 141 |
|
| 142 |
return \sprintf('array{%s}', implode(', ', $items)); |
| 143 |
} |
| 144 |
} |