Refactor HeaderCollection

This commit is contained in:
PJ Dietz 2020-08-12 07:12:42 -04:00
parent 168867206e
commit 2e3475b882
2 changed files with 25 additions and 33 deletions

View File

@ -7,51 +7,44 @@ use Iterator;
/** /**
* HeaderCollection provides case-insensitive access to lists of header values. * HeaderCollection provides case-insensitive access to lists of header values.
* **
* This class is an internal class used by Message and is not intended for
* direct use by consumers.
*
* HeaderCollection preserves the cases of keys as they are set, but treats key * HeaderCollection preserves the cases of keys as they are set, but treats key
* access case insensitively. * access case insensitively.
* *
* Any values added to HeaderCollection are added to list arrays. Subsequent * Any values added to HeaderCollection are added to list arrays. Subsequent
* calls to add a value for a given key will append the new value to the list * calls to add a value for a given key will append the new value to the list
* array of values for that key. * array of values for that key.
*
* @internal This class is an internal class used by Message and is not intended
* for direct use by consumers.
*/ */
class HeaderCollection implements ArrayAccess, Iterator class HeaderCollection implements ArrayAccess, Iterator
{ {
/** /**
* @var array
*
* Hash array mapping lowercase header names to original case header names. * Hash array mapping lowercase header names to original case header names.
*
* @var array<string, string>
*/ */
private $fields; private $fields = [];
/** /**
* @var array
*
* Hash array mapping lowercase header names to values as string[] * Hash array mapping lowercase header names to values as string[]
*
* @var array<string, string[]>
*/ */
private $values; private $values = [];
/** /**
* @var string[]
*
* List array of lowercase header names. * List array of lowercase header names.
*
* @var string[]
*/ */
private $keys; private $keys = [];
/** @var int */ /** @var int */
private $position = 0; private $position = 0;
public function __construct() // -------------------------------------------------------------------------
{
$this->keys = [];
$this->fields = [];
$this->values = [];
}
// ------------------------------------------------------------------------
// ArrayAccess // ArrayAccess
/** /**
@ -111,7 +104,7 @@ class HeaderCollection implements ArrayAccess, Iterator
} }
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// Iterator // Iterator
public function current() public function current()

View File

@ -1,20 +1,19 @@
<?php <?php
namespace WellRESTed\Test\Unit\Message; namespace WellRESTed\Message;
use WellRESTed\Message\HeaderCollection;
use WellRESTed\Test\TestCase; use WellRESTed\Test\TestCase;
class HeaderCollectionTest extends TestCase class HeaderCollectionTest extends TestCase
{ {
public function testAddsSingleHeaderAndIndicatesCaseInsensitiveIsset() public function testAddsSingleHeaderAndIndicatesCaseInsensitiveIsset(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Content-Type'] = 'application/json'; $collection['Content-Type'] = 'application/json';
$this->assertTrue(isset($collection['content-type'])); $this->assertTrue(isset($collection['content-type']));
} }
public function testAddsMultipleHeadersAndIndicatesCaseInsensitiveIsset() public function testAddsMultipleHeadersAndIndicatesCaseInsensitiveIsset(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Set-Cookie'] = 'cat=Molly'; $collection['Set-Cookie'] = 'cat=Molly';
@ -22,17 +21,18 @@ class HeaderCollectionTest extends TestCase
$this->assertTrue(isset($collection['set-cookie'])); $this->assertTrue(isset($collection['set-cookie']));
} }
public function testReturnsHeadersWithCaseInsensitiveHeaderName() public function testReturnsHeadersWithCaseInsensitiveHeaderName(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Set-Cookie'] = 'cat=Molly'; $collection['Set-Cookie'] = 'cat=Molly';
$collection['SET-COOKIE'] = 'dog=Bear'; $collection['SET-COOKIE'] = 'dog=Bear';
$headers = $collection['set-cookie']; $headers = $collection['set-cookie'];
$this->assertEquals(2, count(array_intersect($headers, ['cat=Molly', 'dog=Bear']))); $matched = array_intersect($headers, ['cat=Molly', 'dog=Bear']);
$this->assertCount(2, $matched);
} }
public function testRemovesHeadersWithCaseInsensitiveHeaderName() public function testRemovesHeadersWithCaseInsensitiveHeaderName(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Set-Cookie'] = 'cat=Molly'; $collection['Set-Cookie'] = 'cat=Molly';
@ -41,8 +41,7 @@ class HeaderCollectionTest extends TestCase
$this->assertFalse(isset($collection['set-cookie'])); $this->assertFalse(isset($collection['set-cookie']));
} }
/** @coversNothing */ public function testCloneMakesDeepCopyOfHeaders(): void
public function testCloneMakesDeepCopyOfHeaders()
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Set-Cookie'] = 'cat=Molly'; $collection['Set-Cookie'] = 'cat=Molly';
@ -53,7 +52,7 @@ class HeaderCollectionTest extends TestCase
$this->assertTrue(isset($collection['set-cookie']) && !isset($clone['set-cookie'])); $this->assertTrue(isset($collection['set-cookie']) && !isset($clone['set-cookie']));
} }
public function testIteratesWithOriginalKeys() public function testIteratesWithOriginalKeys(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Content-length'] = '100'; $collection['Content-length'] = '100';
@ -74,7 +73,7 @@ class HeaderCollectionTest extends TestCase
$this->assertEquals(0, $countUnmatched); $this->assertEquals(0, $countUnmatched);
} }
public function testIteratesWithOriginalKeysAndValues() public function testIteratesWithOriginalKeysAndValues(): void
{ {
$collection = new HeaderCollection(); $collection = new HeaderCollection();
$collection['Content-length'] = '100'; $collection['Content-length'] = '100';