From b0dffabf95bd8d7f222ffb1711fc0340d7104f81 Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Sat, 14 Oct 2017 15:52:35 +0200 Subject: [PATCH] Fix issue with $request->getAttribute('routeInfo')[2] and PHPUnit (closes #26) --- src/Validation.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Validation.php b/src/Validation.php index 669229b..dcf4ea3 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -99,7 +99,7 @@ public function __invoke($request, $response, $next) { $this->errors = []; $params = $request->getParams(); - $params = array_merge((array) $request->getAttribute('routeInfo')[2], $params); + $params = array_merge((array) $this->retrieveRouteInfo($request), $params); $this->validate($params, $this->validators); $request = $request->withAttribute($this->errors_name, $this->getErrors()); @@ -171,6 +171,21 @@ private function getNestedParam($params = [], $keys = []) } } + /** + * Retrieve the route info as an array if any. + * + * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request + * + * @return array An array with the array info. + */ + private function retrieveRouteInfo($request){ + if(isset($request->getAttribute('routeInfo')[2])){ + return (array) $request->getAttribute('routeInfo')[2]; + } else { + return []; + } + } + /** * Check if there are any errors. *