diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 2b256abf3cef0..a1a1108795b06 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -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('/((? $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. diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 721a6c7240b99..8cd1d77d238ab 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -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('/((? $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. diff --git a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php index 2cf9b52c225f6..02c14d5af250a 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php @@ -202,4 +202,22 @@ public function testFilter() } + + public function testWildcard() + { + $bag = new ParameterBag(array('user.name' => 'John Doe', + 'user.email' => 'address@example.com', + 'user.params.param1' => 'boo', + 'user.params.param25' => 'foo', + 'other.data' => 'bar')); + + $this->assertEquals($bag->wildcard('user.*'), array('user.name' => 'John Doe', + 'user.email' => 'address@example.com', + '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')); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 8318101e665d1..23bdbd4572779 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -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', 'address@example.com'); + $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' => 'address@example.com', + '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() {