Skip to content

Commit

Permalink
Add withLength() immutable helper
Browse files Browse the repository at this point in the history
Add the immutable variant of length().
  • Loading branch information
markstory committed Nov 26, 2016
1 parent a30ee07 commit f4d7d35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Network/Response.php
Expand Up @@ -1541,6 +1541,7 @@ public function protocol($protocol = null)
*
* @param int|null $bytes Number of bytes
* @return int|null
* @deprecated 3.4.0 Use withLength() to set length instead.
*/
public function length($bytes = null)
{
Expand All @@ -1555,6 +1556,17 @@ public function length($bytes = null)
return null;
}

/**
* Create a new response with the Content-Length header set.
*
* @param int|string $bytes Number of bytes
* @return static
*/
public function withLength($bytes)
{
return $this->withHeader('Content-Length', (string)$bytes);
}

/**
* Checks whether a response has not been modified according to the 'If-None-Match'
* (Etags) and 'If-Modified-Since' (last modification date) request
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -695,6 +695,23 @@ public function testLength()
$this->assertEquals('100', $response->getHeaderLine('Content-Length'));
}

/**
* Tests settings the content length
*
* @return void
*/
public function testWithLength()
{
$response = new Response();
$this->assertFalse($response->hasHeader('Content-Length'));

$new = $response->withLength(100);
$this->assertFalse($response->hasHeader('Content-Length'), 'Old instance not modified');

$this->assertSame('100', $new->getHeaderLine('Content-Length'));
$this->assertSame('100', $new->length(), 'new method is compat with old.');
}

/**
* Tests setting the expiration date
*
Expand Down

0 comments on commit f4d7d35

Please sign in to comment.