Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit762ae91

Browse files
[HttpFoundation] AddQueryParameterRequestMatcher
1 parentf0959b4 commit762ae91

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-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+
namespaceSymfony\Component\HttpFoundation\RequestMatcher;
13+
14+
useSymfony\Component\HttpFoundation\Request;
15+
useSymfony\Component\HttpFoundation\RequestMatcherInterface;
16+
17+
/**
18+
* Checks the presence of HTTP query parameters of a Request.
19+
*
20+
* @author Alexandre Daubois <alex.daubois@gmail.com>
21+
*/
22+
class QueryParameterRequestMatcherimplements RequestMatcherInterface
23+
{
24+
/**
25+
* @var string[]
26+
*/
27+
privatearray$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+
publicfunction__construct(array|string$parameters)
34+
{
35+
$this->parameters =array_reduce(array_map(strtolower(...), (array)$parameters),staticfn (array$parameters,string$parameter) =>array_merge($parameters,preg_split('/\s*,\s*/',$parameter)), []);
36+
}
37+
38+
publicfunctionmatches(Request$request):bool
39+
{
40+
if (!$this->parameters) {
41+
returntrue;
42+
}
43+
44+
return0 ===\count(array_diff_assoc($this->parameters,$request->query->keys()));
45+
}
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
namespaceSymfony\Component\HttpFoundation\Tests\RequestMatcher;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\HttpFoundation\Request;
16+
useSymfony\Component\HttpFoundation\RequestMatcher\QueryParameterRequestMatcher;
17+
18+
class QueryParameterRequestMatcherTestextends TestCase
19+
{
20+
/**
21+
* @dataProvider getDataForArray
22+
*/
23+
publicfunctiontestArray(string$uri,bool$matches)
24+
{
25+
$matcher =newQueryParameterRequestMatcher(['foo','bar']);
26+
$request = Request::create($uri);
27+
$this->assertSame($matches,$matcher->matches($request));
28+
}
29+
30+
/**
31+
* @dataProvider getDataForArray
32+
*/
33+
publicfunctiontestCommaSeparatedString(string$uri,bool$matches)
34+
{
35+
$matcher =newQueryParameterRequestMatcher('foo, bar');
36+
$request = Request::create($uri);
37+
$this->assertSame($matches,$matcher->matches($request));
38+
}
39+
40+
/**
41+
* @dataProvider getDataForSingleString
42+
*/
43+
publicfunctiontestSingleString(string$uri,bool$matches)
44+
{
45+
$matcher =newQueryParameterRequestMatcher('foo');
46+
$request = Request::create($uri);
47+
$this->assertSame($matches,$matcher->matches($request));
48+
}
49+
50+
publicstaticfunctiongetDataForArray():\Generator
51+
{
52+
yield ['https://example.com?foo=&bar=',true];
53+
yield ['https://example.com?foo=foo1&bar=bar1',true];
54+
yield ['https://example.com?foo=foo1&bar=bar1&baz=baz1',true];
55+
yield ['https://example.com?foo=',false];
56+
yield ['https://example.com',false];
57+
}
58+
59+
publicstaticfunctiongetDataForSingleString():\Generator
60+
{
61+
yield ['https://example.com?foo=&bar=',true];
62+
yield ['https://example.com?foo=foo1',true];
63+
yield ['https://example.com?foo=',true];
64+
yield ['https://example.com?bar=bar1&baz=baz1',false];
65+
yield ['https://example.com',false];
66+
}
67+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp