Skip to content

Commit e851ae4

Browse files
authored
Request Parameters section Improved:
* Parameters auto generated from Request Rules
1 parent 0ef3b11 commit e851ae4

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

src/ApiDoctor/ApiDoctorSwaggerSections.php

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ public static function buildParameters($parameters)
135135
if (!empty($rules)) {
136136
$properties = self::buildProperties($rules);
137137

138-
$description = "";
138+
$description = "Parameters Validation Rules:\n\n\n";
139139
foreach ($rules as $key => $value) {
140-
$description .= "=> " . $key . " - " . $value . " ";
140+
$description .= "" . $key . ": " . $value . "\n\n";
141141
}
142142

143143
$schema = "
144-
description: {$description}
144+
description: '{$description}'
145145
schema:
146146
type: object
147147
properties:
@@ -160,24 +160,61 @@ public static function buildParameters($parameters)
160160
return $result;
161161
}
162162

163-
164163
public static function buildProperties($rules)
165164
{
166-
$result = "";
165+
$result = [];
167166
try {
168167
foreach ($rules as $rule => $value) {
169-
preg_match('/(.*?)\|/', $value, $matches);
170-
$type = $matches[1];
171-
$propertyString = "
172-
{$rule}:
173-
type: string
174-
description: {$value}
175-
";
176-
$result .= $propertyString;
168+
$type = 'string';
169+
try {
170+
preg_match('/(.*?)\|/', $value, $matches);
171+
$type = $matches[1];
172+
} catch (\Exception $e) {
173+
$type = 'string';
174+
}
175+
$result[$rule] = $type;
177176
}
178177
} catch (\Exception $e) {
179178
// echo $e->getMessage();
180179
}
181-
return $result;
180+
181+
// ===========
182+
//remove duplicate additional properties that are mentioned with key '.*'
183+
//example:
184+
//{
185+
// "sort_by": "string",
186+
// "is_ascending": "string",
187+
// "with": "string",
188+
// "with.*": "string",
189+
// "artist_ids": "string",
190+
// "artist_ids.*": "string",
191+
// "genre_ids": "string",
192+
// "genre_ids.*": "string"
193+
//}
194+
//to:
195+
//{
196+
// "sort_by": "string",
197+
// "is_ascending": "string",
198+
// "with": "string",
199+
// "artist_ids": "string",
200+
// "genre_ids": "string"
201+
//}
202+
// ===========
203+
$result = array_filter($result, function ($key) {
204+
return !preg_match('/\.\*/', $key);
205+
}, ARRAY_FILTER_USE_KEY);
206+
207+
208+
//build properties section
209+
$properties = "";
210+
foreach ($result as $property => $type) {
211+
$propertyString = "
212+
{$property}:
213+
type: string
214+
description: {$type}
215+
";
216+
$properties .= $propertyString;
217+
}
218+
return $properties;
182219
}
183220
}

0 commit comments

Comments
 (0)