Skip to content

Commit d624665

Browse files
committed
use parse_str instead of preg_match
1 parent 261e522 commit d624665

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/Filter/QueryParameterValidateListener.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public function onKernelRequest(GetResponseEvent $event)
6464
continue;
6565
}
6666

67-
if (false !== strpos($name, '[')) { // array notation of filter
68-
if (!$this->isArrayNotationFilterValid($name, $request)) {
69-
$errorList[] = sprintf('Query parameter "%s" is required', $name);
70-
}
71-
} elseif (null === $request->query->get($name)) {
67+
if (!$this->isRequiredFilterValid($name, $request)) {
7268
$errorList[] = sprintf('Query parameter "%s" is required', $name);
7369
}
7470

@@ -79,14 +75,30 @@ public function onKernelRequest(GetResponseEvent $event)
7975
}
8076
}
8177

82-
private function isArrayNotationFilterValid($name, $request): bool
78+
/**
79+
* Test if required filter is valid. It validates array notation too like "required[bar]".
80+
*/
81+
private function isRequiredFilterValid($name, $request): bool
8382
{
8483
$matches = [];
85-
preg_match('/([^[]+)\[(.*)\]/', $name, $matches);
86-
list(, $rootName, $keyName) = $matches;
87-
$keyName = $keyName ?: 0; // array without index should test the first key
88-
$queryParameter = $request->query->get($rootName);
84+
parse_str($name, $matches);
85+
if (empty($matches)) {
86+
return false;
87+
}
88+
89+
$rootName = array_keys($matches)[0] ?? '';
90+
if (!$rootName) {
91+
return false;
92+
}
93+
94+
if (is_array($matches[$rootName])) {
95+
$keyName = array_keys($matches[$rootName])[0];
96+
97+
$queryParameter = $request->query->get($rootName);
98+
99+
return is_array($queryParameter) && isset($queryParameter[$keyName]);
100+
}
89101

90-
return is_array($queryParameter) && isset($queryParameter[$keyName]);
102+
return null !== $request->query->get($rootName);
91103
}
92104
}

0 commit comments

Comments
 (0)