-
Notifications
You must be signed in to change notification settings - Fork 701
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
[RFC] Added the constraint option on the ParamFetcher #700
Merged
lsmith77
merged 1 commit into
FriendsOfSymfony:master
from
ClementGautier:feat-validator
Mar 10, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
use Doctrine\Common\Util\ClassUtils; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\Validator\Constraints\Regex; | ||
use Symfony\Component\Validator\ValidatorInterface; | ||
|
||
/** | ||
* Helper to validate parameters of the active request. | ||
|
@@ -48,16 +50,23 @@ class ParamFetcher implements ParamFetcherInterface | |
*/ | ||
private $controller; | ||
|
||
/** | ||
* @var ValidatorInterface | ||
*/ | ||
private $validator; | ||
|
||
/** | ||
* Initializes fetcher. | ||
* | ||
* @param ParamReader $paramReader Query param reader | ||
* @param Request $request Active request | ||
* @param ParamReader $paramReader Query param reader | ||
* @param Request $request Active request | ||
* @param ValidatorInterface $validator The validator service | ||
*/ | ||
public function __construct(ParamReader $paramReader, Request $request) | ||
public function __construct(ParamReader $paramReader, Request $request, ValidatorInterface $validator) | ||
{ | ||
$this->paramReader = $paramReader; | ||
$this->request = $request; | ||
$this->request = $request; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
|
@@ -103,17 +112,9 @@ public function get($name, $strict = null) | |
} | ||
|
||
if ($config->array) { | ||
$failMessage = null; | ||
|
||
if (!is_array($param)) { | ||
$failMessage = sprintf("Query parameter value of '%s' is not an array", $name); | ||
} elseif (count($param) !== count($param, COUNT_RECURSIVE)) { | ||
$failMessage = sprintf("Query parameter value of '%s' must not have a depth of more than one", $name); | ||
} | ||
|
||
if (null !== $failMessage) { | ||
if ($strict) { | ||
throw new BadRequestHttpException($failMessage); | ||
throw new BadRequestHttpException(sprintf("Query parameter value of '%s' is not an array", $name)); | ||
} | ||
|
||
return $default; | ||
|
@@ -159,16 +160,39 @@ public function cleanParamWithRequirements(Param $config, $param, $strict) | |
{ | ||
$default = $config->default; | ||
|
||
if ('' !== $config->requirements | ||
&& ($param !== $default || null === $default) | ||
&& !preg_match('#^'.$config->requirements.'$#xsu', $param) | ||
) { | ||
if ($strict) { | ||
$paramType = $config instanceof QueryParam ? 'Query' : 'Request'; | ||
if (null === $config->requirements || ($param === $default && null !== $default)) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can remove this empty line? |
||
return $param; | ||
} | ||
|
||
$constraint = $config->requirements; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you remove this empty line? |
||
throw new BadRequestHttpException( | ||
$paramType . " parameter value '$param', does not match requirements '{$config->requirements}'" | ||
); | ||
if (is_scalar($constraint)) { | ||
if (is_array($param)) { | ||
if ($strict) { | ||
throw new BadRequestHttpException("Query parameter is an array"); | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
$constraint = new Regex(array( | ||
'pattern' => '#^'.preg_quote($config->requirements).'$#xsu', | ||
'message' => sprintf( | ||
"%s parameter value '%s', does not match requirements '%s'", | ||
$config instanceof QueryParam ? 'Query' : 'Request', | ||
$param, | ||
$config->requirements | ||
) | ||
)); | ||
} | ||
|
||
$errors = $this->validator->validateValue($param, $constraint); | ||
|
||
if (0 !== count($errors)) { | ||
|
||
if ($strict) { | ||
throw new BadRequestHttpException((string) $errors); | ||
} | ||
|
||
return null === $default ? '' : $default; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should refactor this method and split it up to make things more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lsmith77 And what about using the same option for requirements and constraints ? I mean, requirements === Constraints\Regex no ? So we could use always "requirements" and in case requirements is not an instance of
Constraint
we replace it by a Regex constraint with the requirements argument.If I do this this part of the method will be way more simple so I'll not have to split it up.
I'm not 100% sure that it is possible to TypeHint an annotation on mixed
string|\Classname
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm that might be an idea to simplify the code indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, i'll do the job this week-end.