Skip to content

Commit c3b18c5

Browse files
[HttpFoundation] Add QueryParameterRequestMatcher
1 parent f0959b4 commit c3b18c5

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable`
8+
* Add `QueryParameterRequestMatcher`
89

910
6.3
1011
---
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\RequestMatcher;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
16+
17+
/**
18+
* Checks the HTTP query parameters of a Request.
19+
*
20+
* @author Alexandre Daubois <alex.daubois@gmail.com>
21+
*/
22+
class QueryParameterRequestMatcher implements RequestMatcherInterface
23+
{
24+
/**
25+
* @var string[]
26+
*/
27+
private array $parameters;
28+
29+
/**
30+
* @param string[]|string $parameters A parameter or a list of parameters
31+
* Strings can contain a comma-delimited list of query parameters
32+
*/
33+
public function __construct(array|string $parameters)
34+
{
35+
$this->parameters = array_reduce(array_map('strtolower', (array) $parameters), static fn (array $parameters, string $parameter) => array_merge($parameters, preg_split('/\s*,\s*/', $parameter)), []);
36+
}
37+
38+
public function matches(Request $request): bool
39+
{
40+
if (!$this->parameters) {
41+
return true;
42+
}
43+
44+
return 0 === \count(array_diff_assoc($this->parameters, $request->query->keys()));
45+
}
46+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\RequestMatcher;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestMatcher\QueryParameterRequestMatcher;
17+
18+
class QueryParameterRequestMatcherTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider getDataForArray
22+
*/
23+
public function test(string $uri, bool $matches)
24+
{
25+
$matcher = new QueryParameterRequestMatcher(['foo', 'bar']);
26+
$request = Request::create($uri);
27+
$this->assertSame($matches, $matcher->matches($request));
28+
}
29+
30+
/**
31+
* @dataProvider getDataForSingleString
32+
*/
33+
public function testSingleString(string $uri, bool $matches)
34+
{
35+
$matcher = new QueryParameterRequestMatcher('foo');
36+
$request = Request::create($uri);
37+
$this->assertSame($matches, $matcher->matches($request));
38+
}
39+
40+
public static function getDataForArray(): \Generator
41+
{
42+
yield ['https://example.com?foo=&bar=', true];
43+
yield ['https://example.com?foo=foo1&bar=bar1', true];
44+
yield ['https://example.com?foo=foo1&bar=bar1&baz=baz1', true];
45+
yield ['https://example.com?foo=', false];
46+
yield ['https://example.com', false];
47+
}
48+
49+
public static function getDataForSingleString(): \Generator
50+
{
51+
yield ['https://example.com?foo=&bar=', true];
52+
yield ['https://example.com?foo=foo1', true];
53+
yield ['https://example.com?foo=', true];
54+
yield ['https://example.com?bar=bar1&baz=baz1', false];
55+
yield ['https://example.com', false];
56+
}
57+
}

0 commit comments

Comments
 (0)