Skip to content

Commit 157361b

Browse files
committed
nested array syntax for body param
1 parent 91084f6 commit 157361b

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

src/Parameters/BodyParameterGenerator.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,13 @@ public function getParameters()
2929

3030
foreach ($this->rules as $param => $rule) {
3131
$paramRules = $this->splitRules($rule);
32-
$enums = $this->getEnumValues($paramRules);
32+
$nameTokens = explode('.', $param);
33+
34+
$this->addToProperties($properties, $nameTokens, $paramRules);
3335

3436
if ($this->isParamRequired($paramRules)) {
3537
$required[] = $param;
3638
}
37-
38-
$propObj = [
39-
'type' => $this->getParamType($paramRules)
40-
];
41-
42-
if (!empty($enums)) {
43-
$propObj['enum'] = $enums;
44-
}
45-
46-
$properties[$param] = $propObj;
4739
}
4840

4941
if (!empty($required)) {
@@ -59,4 +51,47 @@ public function getParamLocation()
5951
{
6052
return 'body';
6153
}
54+
55+
protected function addToProperties(&$properties, $nameTokens, $rules)
56+
{
57+
if (empty($nameTokens)) {
58+
return;
59+
}
60+
61+
$name = array_shift($nameTokens);
62+
63+
if (!empty($nameTokens)) {
64+
$type = $this->getNestedParamType($nameTokens);
65+
} else {
66+
$type = $this->getParamType($rules);
67+
}
68+
69+
$propObj = [
70+
'type' => $type
71+
];
72+
73+
if ($enums = $this->getEnumValues($rules)) {
74+
$propObj['enum'] = $enums;
75+
}
76+
77+
if ($name === '*') {
78+
$name = 0;
79+
}
80+
81+
$properties[$name] = $propObj;
82+
83+
if ($type === 'array') {
84+
$properties[$name]['items'] = [];
85+
$this->addToProperties($properties[$name]['items'], $nameTokens, $rules);
86+
}
87+
}
88+
89+
protected function getNestedParamType($nameTokens)
90+
{
91+
if (current($nameTokens) === '*') {
92+
return 'array';
93+
} else {
94+
return 'object';
95+
}
96+
}
6297
}

tests/Parameters/BodyParameterGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,31 @@ public function testEnumInBody()
7676
], $bodyParameters['schema']['properties']);
7777
}
7878

79+
public function testArraySyntax()
80+
{
81+
$bodyParameters = $this->getBodyParameters([
82+
'matrix' => 'array',
83+
'matrix.*' => 'array',
84+
'matrix.*.*' => 'integer',
85+
]);
86+
87+
$this->assertEquals([
88+
'matrix' => [
89+
'type' => 'array',
90+
'items' => [
91+
[
92+
'type' => 'array',
93+
'items' => [
94+
[
95+
'type' => 'integer'
96+
]
97+
]
98+
]
99+
]
100+
]
101+
], $bodyParameters['schema']['properties']);
102+
}
103+
79104
private function getBodyParameters(array $rules)
80105
{
81106
$bodyParameters = (new BodyParameterGenerator($rules))->getParameters();

0 commit comments

Comments
 (0)