Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route info 2 not defined #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -171,6 +171,22 @@ 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.
*
Expand Down
40 changes: 0 additions & 40 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,44 +742,4 @@ public function routeParamValidationProvider()
),
);
}

/*
public function testUseTemplate()
{
$this->setupGet();
$hostnameValidator = v::regex('/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/')->setTemplate('Hostname {{name}} is not valid');
$entryValidator = v::regex('/^[a-zA-Z]$/')->setTemplate('Entry {{name}} should contain only letters');
$expectedValidators = array(
'username' => $hostnameValidator,
'age' => $entryValidator,
);
$mw = new Validation($expectedValidators, null, ['useTemplate' => true]);

$errors = null;
$hasErrors = null;
$validators = [];
$next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
$errors = $req->getAttribute('errors');
$hasErrors = $req->getAttribute('has_errors');
$validators = $req->getAttribute('validators');

return $res;
};

$response = $mw($this->request, $this->response, $next);

$expectedErrors = array(
'username' => array(
'Hostname "davidepastore" is not valid',
),
'age' => array(
'Entry "89" should contain only letters',
),
);

$this->assertTrue($hasErrors);
$this->assertEquals($expectedErrors, $errors);
$this->assertEquals($newValidators, $validators);
}
*/
}