Skip to content

Commit 2eaaa03

Browse files
committed
:octocat: HeaderUtil::normalize() skip empty array values and ignore numeric keys
1 parent d1a5a12 commit 2eaaa03

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/HeaderUtil.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
*
1818
*/
19-
class HeaderUtil{
19+
final class HeaderUtil{
2020

2121
/**
2222
* Normalizes an array of header lines to format ["Name" => "Value (, Value2, Value3, ...)", ...]
@@ -30,7 +30,6 @@ public static function normalize(iterable $headers):array{
3030

3131
// the key is numeric, so $val is either a string or an array
3232
if(is_numeric($key)){
33-
3433
// "key: val"
3534
if(is_string($val)){
3635
$header = explode(':', $val, 2);
@@ -43,9 +42,14 @@ public static function normalize(iterable $headers):array{
4342
$val = $header[1];
4443
}
4544
// [$key, $val], ["key" => $key, "val" => $val]
46-
elseif(is_array($val)){
45+
elseif(is_array($val) && !empty($val)){
4746
$key = array_keys($val)[0];
4847
$val = array_values($val)[0];
48+
49+
// skip if the key is numeric
50+
if(is_numeric($key)){
51+
continue;
52+
}
4953
}
5054
else{
5155
continue;
@@ -92,10 +96,6 @@ public static function normalize(iterable $headers):array{
9296
* header-field = field-name ":" OWS field-value OWS
9397
* OWS = *( SP / HTAB )
9498
*
95-
* @param string[] $values header values
96-
*
97-
* @return string[] Trimmed header values
98-
*
9999
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4
100100
*/
101101
public static function trimValues(iterable $values):iterable{

tests/HeaderUtilTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public static function headerDataProvider():array{
3030
'numericvalue' => [['numericvalue:1'], ['Numericvalue' => '1']],
3131
'numericvalue2' => [['numericvalue' => 2], ['Numericvalue' => '2']],
3232
'keyvaluearray' => [[['foo' => 'bar']], ['Foo' => 'bar']],
33+
'emptykvearray' => [[[]], []],
34+
'kvarraynumkey' => [[[69 => 420]], []],
3335
'arrayvalue' => [['foo' => ['bar', 'baz']], ['Foo' => 'bar, baz']],
3436
'invalid: 2' => [[2 => 2], []],
3537
'invalid: what' => [['what'], []],

0 commit comments

Comments
 (0)