Skip to content
Permalink
Browse files

Add withCharset() immutable helper.

  • Loading branch information...
markstory committed Nov 26, 2016
1 parent 5326287 commit f22ec6886976040bb90e92d847e60365c7ee94fc
Showing with 32 additions and 0 deletions.
  1. +16 −0 src/Network/Response.php
  2. +16 −0 tests/TestCase/Network/ResponseTest.php
@@ -1150,6 +1150,7 @@ public function mapType($ctype)
*
* @param string|null $charset Character set string.
* @return string Current charset
* @deprecated 3.4.0 Use withCharset() instead.
*/
public function charset($charset = null)
{
@@ -1162,6 +1163,21 @@ public function charset($charset = null)
return $this->_charset;
}
/**
* Get a new instance with an updated charset.
*
* @param string $charset Character set string.
* @return static
*/
public function withCharset($charset)
{
$new = clone $this;
$new->_charset = $charset;
$new->_setContentType();
return $new;
}
/**
* Sets the correct headers to instruct the client to not cache the response
*
@@ -130,6 +130,22 @@ public function testCharset()
$this->assertEquals('UTF-16', $response->charset('UTF-16'));
}
/**
* Tests withCharset method
*
* @return void
*/
public function testWithCharset()
{
$response = new Response();
$this->assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
$new = $response->withCharset('iso-8859-1');
$this->assertNotContains('iso', $response->getHeaderLine('Content-Type'), 'Old instance not changed');
$this->assertEquals('text/html; charset=iso-8859-1', $new->getHeaderLine('Content-Type'));
}
/**
* Tests the statusCode method
*

0 comments on commit f22ec68

Please sign in to comment.
You can’t perform that action at this time.