Skip to content

Commit

Permalink
Add withCharset() immutable helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 26, 2016
1 parent 5326287 commit f22ec68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Network/Response.php
Expand Up @@ -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)
{
Expand All @@ -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
*
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit f22ec68

Please sign in to comment.