You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -56,6 +60,14 @@ public function __construct(array $shape)
56
60
ksort($sortedShape);
57
61
58
62
$this->shape = $sortedShape;
63
+
64
+
if ($this->extraKeyTypexor$this->extraValueType) {
65
+
thrownewInvalidArgumentException(\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
+
thrownewInvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $extraKeyType));
70
+
}
59
71
}
60
72
61
73
/**
@@ -66,6 +78,21 @@ public function getShape(): array
66
78
return$this->shape;
67
79
}
68
80
81
+
publicfunctionisSealed(): bool
82
+
{
83
+
returnnull === $this->extraValueType;
84
+
}
85
+
86
+
publicfunctiongetExtraKeyType(): ?Type
87
+
{
88
+
return$this->extraKeyType;
89
+
}
90
+
91
+
publicfunctiongetExtraValueType(): ?Type
92
+
{
93
+
return$this->extraKeyType;
94
+
}
95
+
69
96
publicfunctionaccepts(mixed$value): bool
70
97
{
71
98
if (!\is_array($value)) {
@@ -80,11 +107,12 @@ public function accepts(mixed $value): bool
80
107
81
108
foreach ($valueas$key => $itemValue) {
82
109
$valueType = $this->shape[$key]['type'] ?? false;
83
-
if (!$valueType) {
110
+
111
+
if ($valueType && !$valueType->accepts($itemValue)) {
84
112
returnfalse;
85
113
}
86
114
87
-
if (!$valueType->accepts($itemValue)) {
115
+
if (!$valueType && ($this->isSealed() || !$this->extraKeyType->accepts($key) || !$this->extraValueType->accepts($itemValue))) {
88
116
returnfalse;
89
117
}
90
118
}
@@ -105,6 +133,12 @@ public function __toString(): string
0 commit comments