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

Appearance settings

Commit ed6c9d8

Browse filesBrowse files
committed
[HttpFoundation] add InputBag
1 parent c3455b4 commit ed6c9d8
Copy full SHA for ed6c9d8

File tree

5 files changed

+181
-10
lines changed
Filter options

5 files changed

+181
-10
lines changed

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Added `Symfony\Component\HttpFoundation\InputBag`
8+
* Deprecated retriving non-string values using `InputBag::get()` ( use `InputBag::getAll()`, or `InputBag::getRaw()` instead )
9+
410
5.0.0
511
-----
612

+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Exception;
13+
14+
/**
15+
* Raised when a user sends a malformed request.
16+
*/
17+
class BadRequestException extends \UnexpectedValueException implements RequestExceptionInterface
18+
{
19+
}
+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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;
13+
14+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
15+
16+
/**
17+
* InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
18+
*
19+
* @author Saif Eddin Gmati <saif.gmati@symfony.com>
20+
*/
21+
final class InputBag extends ParameterBag
22+
{
23+
/**
24+
* Returns a string input value by name.
25+
*
26+
* @param string|null $default The default value if the input key does not exist
27+
*
28+
* @return string|null
29+
*/
30+
public function get(string $key, $default = null)
31+
{
32+
if (null !== $default && !\is_string($default)) {
33+
@trigger_error(sprintf('Passing a non-string value as 2nd argument to "%s::%s()" is deprecated since Symfony 5.1, pass a string or null instead.', static::class, __METHOD__), E_USER_DEPRECATED);
34+
}
35+
36+
$value = parent::get($key, $this);
37+
38+
if (null !== $value && $this !== $value && !\is_string($value)) {
39+
@trigger_error(sprintf('Retrieving a non-string value from "%s::%s()" is deprecated since Symfony 5.1, and will throw a "%s" exception in Symfony 6.0, use "%s::all()" instead.', static::class, __METHOD__, BadRequestException::class, static::class), E_USER_DEPRECATED);
40+
}
41+
42+
return $this === $value ? $default : $value;
43+
}
44+
45+
/**
46+
* Returns the inputs.
47+
*
48+
* @param string|null $key The name of the input to return or null to get them all
49+
*/
50+
public function all(string $key = null): array
51+
{
52+
if (null !== $key) {
53+
$value = $this->parameters[$key] ?? [];
54+
if (!\is_array($value)) {
55+
throw new Exception\BadRequestException(sprintf('Unexpected value for %s input.', $key));
56+
}
57+
58+
return $value;
59+
}
60+
61+
return $this->parameters;
62+
}
63+
64+
/**
65+
* Replaces the current input values by a new set.
66+
*/
67+
public function replace(array $inputs = [])
68+
{
69+
$this->parameters = [];
70+
$this->add($inputs);
71+
}
72+
73+
/**
74+
* Adds input values.
75+
*/
76+
public function add(array $inputs = [])
77+
{
78+
foreach ($inputs as $input => $value) {
79+
$this->set($input, $value);
80+
}
81+
}
82+
83+
/**
84+
* Sets an input by name.
85+
*
86+
* @param string|array $value The value
87+
*/
88+
public function set(string $key, $value)
89+
{
90+
if (!\is_string($value) && !\is_array($value)) {
91+
@trigger_error(sprintf('Passing "%s" as a 2nd Argument to "%s::%s()" is deprecated since Symfony 5.1, pass a string or an array instead.', \is_object($value) ? \get_class($value) : \gettype($value), static::class, __METHOD__), E_USER_DEPRECATED);
92+
}
93+
94+
$this->parameters[$key] = $value;
95+
}
96+
}

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ class Request
7676
/**
7777
* Request body parameters ($_POST).
7878
*
79-
* @var ParameterBag
79+
* @var InputBag
8080
*/
8181
public $request;
8282

8383
/**
8484
* Query string parameters ($_GET).
8585
*
86-
* @var ParameterBag
86+
* @var InputBag
8787
*/
8888
public $query;
8989

@@ -104,7 +104,7 @@ class Request
104104
/**
105105
* Cookies ($_COOKIE).
106106
*
107-
* @var ParameterBag
107+
* @var InputBag
108108
*/
109109
public $cookies;
110110

@@ -254,10 +254,10 @@ public function __construct(array $query = [], array $request = [], array $attri
254254
*/
255255
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
256256
{
257-
$this->request = new ParameterBag($request);
258-
$this->query = new ParameterBag($query);
257+
$this->request = new InputBag($request);
258+
$this->query = new InputBag($query);
259259
$this->attributes = new ParameterBag($attributes);
260-
$this->cookies = new ParameterBag($cookies);
260+
$this->cookies = new InputBag($cookies);
261261
$this->files = new FileBag($files);
262262
$this->server = new ServerBag($server);
263263
$this->headers = new HeaderBag($this->server->getHeaders());
@@ -288,7 +288,7 @@ public static function createFromGlobals()
288288
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
289289
) {
290290
parse_str($request->getContent(), $data);
291-
$request->request = new ParameterBag($data);
291+
$request->request = new InputBag($data);
292292
}
293293

294294
return $request;
@@ -430,16 +430,16 @@ public function duplicate(array $query = null, array $request = null, array $att
430430
{
431431
$dup = clone $this;
432432
if (null !== $query) {
433-
$dup->query = new ParameterBag($query);
433+
$dup->query = new InputBag($query);
434434
}
435435
if (null !== $request) {
436-
$dup->request = new ParameterBag($request);
436+
$dup->request = new InputBag($request);
437437
}
438438
if (null !== $attributes) {
439439
$dup->attributes = new ParameterBag($attributes);
440440
}
441441
if (null !== $cookies) {
442-
$dup->cookies = new ParameterBag($cookies);
442+
$dup->cookies = new InputBag($cookies);
443443
}
444444
if (null !== $files) {
445445
$dup->files = new FileBag($files);
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
16+
use Symfony\Component\HttpFoundation\InputBag;
17+
18+
class InputBagTest extends TestCase
19+
{
20+
public function testGet()
21+
{
22+
$bag = new InputBag(['foo' => 'bar', 'null' => null]);
23+
24+
$this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
25+
$this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
26+
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
27+
}
28+
29+
public function testGetDoesNotUseDeepByDefault()
30+
{
31+
$bag = new InputBag(['foo' => ['bar' => 'moo']]);
32+
33+
$this->assertNull($bag->get('foo[bar]'));
34+
}
35+
36+
public function testAllWithInputKey()
37+
{
38+
$bag = new InputBag(['foo' => ['bar', 'baz'], 'null' => null]);
39+
40+
$this->assertEquals(['bar', 'baz'], $bag->all('foo'), '->all() gets the value of a parameter');
41+
$this->assertEquals([], $bag->all('unknown'), '->all() returns an empty array if a parameter is not defined');
42+
}
43+
44+
public function testAllThrowsForNonArrayValues()
45+
{
46+
$this->expectException(BadRequestException::class);
47+
$bag = new InputBag(['foo' => 'bar', 'null' => null]);
48+
$bag->all('foo');
49+
}
50+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.