Skip to content

Commit 1708081

Browse files
authored
Merge pull request #6 from devonliu02/support-nullable-array
Nullable Array Support
2 parents 2cabf62 + 220394d commit 1708081

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

src/TypeParser.php

Lines changed: 14 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,13 @@ 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]),
9596
];
97+
return $this->makeNullableSchema($schema, $nullable);
9698
}
9799

98100
protected function parseArray($definition)
@@ -147,14 +149,18 @@ protected function parseInputField($definition)
147149
];
148150
}
149151

152+
private function isNullable(string $definition): bool
153+
{
154+
return $definition[strlen($definition) - 1] === '?';
155+
}
156+
150157
protected function parseObject($definition)
151158
{
152159
if (is_subclass_of($definition, InputType::class)) {
153160
return $this->parseInputType($definition);
154161
}
155162

156-
$nullable = $definition[strlen($definition) - 1] === '?';
157-
163+
$nullable = $this->isNullable($definition);
158164
if ($nullable) {
159165
$definition = trim($definition, '?');
160166
}
@@ -204,8 +210,7 @@ protected function parseObject($definition)
204210

205211
protected function parseEnum(string $definition)
206212
{
207-
$nullable = $definition[strlen($definition) - 1] === '?';
208-
213+
$nullable = $this->isNullable($definition);
209214
if ($nullable) {
210215
$definition = trim($definition, '?');
211216
}
@@ -243,7 +248,7 @@ protected function makeNullableSchema(array $schema, $nullable)
243248

244249
protected function parseScalar($definition)
245250
{
246-
$nullable = $definition[strlen($definition) - 1] === '?';
251+
$nullable = $this->isNullable($definition);
247252

248253
if ($nullable) {
249254
$definition = trim($definition, '?');

0 commit comments

Comments
 (0)