Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Add QueryParameterRequestMatcher #51324

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

Merged
merged 1 commit into from
Feb 3, 2024
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `UploadedFile::getClientOriginalPath()`
* Add `QueryParameterRequestMatcher`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\RequestMatcher;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

/**
* Checks the presence of HTTP query parameters of a Request.
*
* @author Alexandre Daubois <[email protected]>
*/
class QueryParameterRequestMatcher implements RequestMatcherInterface
{
/**
* @var string[]
*/
private array $parameters;

/**
* @param string[]|string $parameters A parameter or a list of parameters
* Strings can contain a comma-delimited list of query parameters
*/
public function __construct(array|string $parameters)
{
$this->parameters = array_reduce(array_map(strtolower(...), (array) $parameters), static fn (array $parameters, string $parameter) => array_merge($parameters, preg_split('/\s*,\s*/', $parameter)), []);
}

public function matches(Request $request): bool
{
if (!$this->parameters) {
return true;
}

return 0 === \count(array_diff_assoc($this->parameters, $request->query->keys()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Tests\RequestMatcher;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\QueryParameterRequestMatcher;

class QueryParameterRequestMatcherTest extends TestCase
{
/**
* @dataProvider getDataForArray
*/
public function testArray(string $uri, bool $matches)
{
$matcher = new QueryParameterRequestMatcher(['foo', 'bar']);
$request = Request::create($uri);
$this->assertSame($matches, $matcher->matches($request));
}

/**
* @dataProvider getDataForArray
*/
public function testCommaSeparatedString(string $uri, bool $matches)
{
$matcher = new QueryParameterRequestMatcher('foo, bar');
$request = Request::create($uri);
$this->assertSame($matches, $matcher->matches($request));
}

/**
* @dataProvider getDataForSingleString
*/
public function testSingleString(string $uri, bool $matches)
{
$matcher = new QueryParameterRequestMatcher('foo');
$request = Request::create($uri);
$this->assertSame($matches, $matcher->matches($request));
}

public static function getDataForArray(): \Generator
{
yield ['https://example.com?foo=&bar=', true];
yield ['https://example.com?foo=foo1&bar=bar1', true];
yield ['https://example.com?foo=foo1&bar=bar1&baz=baz1', true];
yield ['https://example.com?foo=', false];
yield ['https://example.com', false];
}

public static function getDataForSingleString(): \Generator
{
yield ['https://example.com?foo=&bar=', true];
yield ['https://example.com?foo=foo1', true];
yield ['https://example.com?foo=', true];
yield ['https://example.com?bar=bar1&baz=baz1', false];
yield ['https://example.com', false];
}
}