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

Skip to content

[HttpFoundation] Wildcard implementation for ParameterBag & Session #2684

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

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,45 @@ public function get($path, $default = null, $deep = false)

return $value;
}

/**
* Returns values by key wilcard like user.*.param?
*
* @param string $pattern Filtering pattern
*
* @api
*/
public function wildcard($pattern)
{
//see https://github.com/andrewtch/phpwildcard/blob/master/wildcard_match.php for details
//convert pattern to regex
$pattern = preg_split('/((?<!\\\)\*)|((?<!\\\)\?)/', $pattern, null,
PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

foreach($pattern as $key => $part) {
if($part == '?') {
$pattern[$key] = '.';
} elseif ($part == '*') {
$pattern[$key] = '.*';
} else {
$pattern[$key] = preg_quote($part);
}
}

$pattern = implode('', $pattern);

$pattern = '/^'.$pattern.'$/';

$return = array();

foreach($this->parameters as $key => $val) {
if(preg_match($pattern, $key)) {
$return[$key] = $val;
}
}

return $return;
}

/**
* Sets a parameter by name.
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,45 @@ public function get($name, $default = null)
{
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
}

/**
* Returns values by key wilcard like user.*.param?
*
* @param string $pattern Filtering pattern
*
* @api
*/
public function wildcard($pattern)
{
//see https://github.com/andrewtch/phpwildcard/blob/master/wildcard_match.php for details
//convert pattern to regex
$pattern = preg_split('/((?<!\\\)\*)|((?<!\\\)\?)/', $pattern, null,
PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

foreach($pattern as $key => $part) {
if($part == '?') {
$pattern[$key] = '.';
} elseif ($part == '*') {
$pattern[$key] = '.*';
} else {
$pattern[$key] = preg_quote($part);
}
}

$pattern = implode('', $pattern);

$pattern = '/^'.$pattern.'$/';

$return = array();

foreach($this->attributes as $key => $val) {
if(preg_match($pattern, $key)) {
$return[$key] = $val;
}
}

return $return;
}

/**
* Sets an attribute.
Expand Down
18 changes: 18 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,22 @@ public function testFilter()


}

public function testWildcard()
{
$bag = new ParameterBag(array('user.name' => 'John Doe',
'user.email' => '[email protected]',
'user.params.param1' => 'boo',
'user.params.param25' => 'foo',
'other.data' => 'bar'));

$this->assertEquals($bag->wildcard('user.*'), array('user.name' => 'John Doe',
'user.email' => '[email protected]',
'user.params.param1' => 'boo',
'user.params.param25' => 'foo'));

$this->assertEquals($bag->wildcard('user.*.param?'), array('user.params.param1' => 'boo'));

$this->assertEquals($bag->wildcard('user.*.param??'), array('user.params.param25' => 'foo'));
}
}
19 changes: 19 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ public function testStorageRemove()

$this->assertNull($this->storage->read('foo'));
}

public function testWildcards()
{
$session = $this->getSession();
$session->set('user.name', 'John Doe');
$session->set('user.email', '[email protected]');
$session->set('user.params.param1', 'boo');
$session->set('user.params.param25', 'foo');
$session->set('other.data', 'bar');

$this->assertEquals($session->wildcard('user.*'), array('user.name' => 'John Doe',
'user.email' => '[email protected]',
'user.params.param1' => 'boo',
'user.params.param25' => 'foo'));

$this->assertEquals($session->wildcard('user.*.param?'), array('user.params.param1' => 'boo'));

$this->assertEquals($session->wildcard('user.*.param??'), array('user.params.param25' => 'foo'));
}

protected function getSession()
{
Expand Down