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

Skip to content

Commit e9544ef

Browse files
committed
[HttpFoundation] add HeaderBag::getFirst and HeaderBag::getAll
1 parent d31b716 commit e9544ef

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,44 @@ public function add(array $headers)
105105
* @param string|null $default The default value
106106
* @param bool $first Whether to return the first value or all header values
107107
*
108-
* @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise
108+
* @return string|string[]|int|null The first header value or default value if $first is true, an array of values otherwise
109109
*/
110110
public function get($key, $default = null, $first = true)
111+
{
112+
// do we trigger an deprecation ?
113+
// @trigger_error(sprintf('%s::%s is deprecated since Symfony 4.4, use %s::getFirst() or %s::getAll() instead.', __CLASS__, __METHOD__, __CLASS__, __CLASS__), E_USER_DEPRECATED);
114+
if ($first === true) {
115+
return $this->getFirst($key, $default);
116+
}
117+
118+
return $this->getAll($key, $default);
119+
}
120+
121+
public function getFirst($key, $default = null)
122+
{
123+
$all = $this->getAll($key, $default);
124+
125+
if (\count($all)) {
126+
return isset($all[0]) ? $all[0] : null;
127+
}
128+
129+
return $default;
130+
}
131+
132+
public function getAll($key, $default = null): array
111133
{
112134
$key = str_replace('_', '-', strtolower($key));
113135
$headers = $this->all();
114136

115137
if (!\array_key_exists($key, $headers)) {
116138
if (null === $default) {
117-
return $first ? null : [];
139+
return [];
118140
}
119141

120-
return $first ? $default : [$default];
142+
return [$default];
121143
}
122-
123-
if ($first) {
124-
return \count($headers[$key]) ? $headers[$key][0] : $default;
125-
}
126-
127144
return $headers[$key];
145+
128146
}
129147

130148
/**

src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,15 @@ public function testGet()
9393
// defaults
9494
$this->assertNull($bag->get('none'), '->get unknown values returns null');
9595
$this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
96+
$this->assertEquals($bag->getFirst('none', 'default'), $bag->get('none', 'default'), '->get unknown values returns default as array');
97+
9698
$this->assertEquals(['default'], $bag->get('none', 'default', false), '->get unknown values returns default as array');
99+
$this->assertEquals($bag->getAll('none', 'default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
97100

98101
$bag->set('foo', 'bor', false);
99102
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
103+
$this->assertEquals($bag->getAll('foo', 'nope'), $bag->get('foo', 'nope', false), '->get return all values as array');
104+
100105
$this->assertEquals(['bar', 'bor'], $bag->get('foo', 'nope', false), '->get return all values as array');
101106
}
102107

0 commit comments

Comments
 (0)