Skip to content

Commit c60b5bf

Browse files
committed
Added nullable array support
1 parent 2cabf62 commit c60b5bf

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

src/TypeParser.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ protected function parseField($definition)
7474
}
7575

7676
$required = $definition[0] === '!';
77+
$nullable = $this->isNullable($definition);
7778

7879
$matches = [];
79-
if (is_string($definition) && preg_match('/\[(.*?)\]/', $definition, $matches)) {
80-
return [$required, $this->parseArrayField([$matches[1]])];
80+
if (preg_match('/\[(.*?)\]/', $definition, $matches)) {
81+
return [$required, $this->parseArrayField([$matches[1]], $nullable)];
8182
}
8283

8384
if ($required) {
@@ -87,12 +88,14 @@ protected function parseField($definition)
8788
return [$required, $this->parseString($definition)];
8889
}
8990

90-
protected function parseArrayField($definition)
91+
protected function parseArrayField($definition, $nullable = false)
9192
{
92-
return [
93+
$schema= [
9394
'type' => 'array',
9495
'items' => $this->parseString($definition[0]),
96+
'nullable' => $nullable,
9597
];
98+
return $this->makeNullableSchema($schema, $nullable);
9699
}
97100

98101
protected function parseArray($definition)
@@ -147,14 +150,18 @@ protected function parseInputField($definition)
147150
];
148151
}
149152

153+
private function isNullable(string $definition): bool
154+
{
155+
return $definition[strlen($definition) - 1] === '?';
156+
}
157+
150158
protected function parseObject($definition)
151159
{
152160
if (is_subclass_of($definition, InputType::class)) {
153161
return $this->parseInputType($definition);
154162
}
155163

156-
$nullable = $definition[strlen($definition) - 1] === '?';
157-
164+
$nullable = $this->isNullable($definition);
158165
if ($nullable) {
159166
$definition = trim($definition, '?');
160167
}
@@ -204,8 +211,7 @@ protected function parseObject($definition)
204211

205212
protected function parseEnum(string $definition)
206213
{
207-
$nullable = $definition[strlen($definition) - 1] === '?';
208-
214+
$nullable = $this->isNullable($definition);
209215
if ($nullable) {
210216
$definition = trim($definition, '?');
211217
}
@@ -243,7 +249,7 @@ protected function makeNullableSchema(array $schema, $nullable)
243249

244250
protected function parseScalar($definition)
245251
{
246-
$nullable = $definition[strlen($definition) - 1] === '?';
252+
$nullable = $this->isNullable($definition);
247253

248254
if ($nullable) {
249255
$definition = trim($definition, '?');

0 commit comments

Comments
 (0)