From fdca5eeeb1e8b91af0240c670abd116705155284 Mon Sep 17 00:00:00 2001 From: Tihomir Tonov Date: Tue, 14 Aug 2018 16:33:11 +0300 Subject: [PATCH] Add method to generate validation rules for nested array properties --- .../ApiDoc/Generators/AbstractGenerator.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php index 313883f4..90bbb5f1 100644 --- a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php +++ b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php @@ -79,8 +79,9 @@ protected function getDocblockResponse($tags) */ protected function getParameters($routeData, $routeAction, $bindings) { - $validator = Validator::make([], $this->getRouteRules($routeAction['uses'], $bindings)); - foreach ($validator->getRules() as $attribute => $rules) { + $rules = $this->simplifyRules($this->getRouteRules($routeAction['uses'], $bindings)); + + foreach ($rules as $attribute => $rules) { $attributeData = [ 'required' => false, 'type' => null, @@ -88,6 +89,7 @@ protected function getParameters($routeData, $routeAction, $bindings) 'value' => '', 'description' => [], ]; + foreach ($rules as $ruleName => $rule) { $this->parseRule($rule, $attribute, $attributeData, $routeData['id']); } @@ -97,6 +99,30 @@ protected function getParameters($routeData, $routeAction, $bindings) return $routeData; } + /** + * Format the validation rules as plain array + * + * @param array $rules + * @return array + */ + protected function simplifyRules($rules) + { + $simplifiedRules = Validator::make([], $rules)->getRules(); + + if (count($simplifiedRules) === 0) { + return $simplifiedRules; + } + + $values = collect($simplifiedRules) + ->filter(function ($values) { + return in_array('array', $values); + })->map(function ($val, $key) { + return ['']; + })->all(); + + return Validator::make($values, $rules)->getRules(); + } + /** * @param $route * @param $bindings