diff --git a/CHANGELOG.md b/CHANGELOG.md index be448a2..d588368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ All notable changes to this project will be documented in this file, in reverse ### Changed -- Nothing. +- [#25](https://github.com/zendframework/zend-escaper/pull/25) changes the behavior of the `Escaper` constructor; it now raises an + exception for non-null, non-string `$encoding` arguments. ### Deprecated diff --git a/src/Escaper.php b/src/Escaper.php index 49b3264..5cec968 100644 --- a/src/Escaper.php +++ b/src/Escaper.php @@ -95,7 +95,11 @@ class Escaper public function __construct($encoding = null) { if ($encoding !== null) { - $encoding = (string) $encoding; + if (! is_string($encoding)) { + throw new Exception\InvalidArgumentException( + get_class($this) . ' constructor parameter must be a string, received ' . gettype($encoding) + ); + } if ($encoding === '') { throw new Exception\InvalidArgumentException( get_class($this) . ' constructor parameter does not allow a blank value' diff --git a/test/EscaperTest.php b/test/EscaperTest.php index d0520d7..f71f58e 100644 --- a/test/EscaperTest.php +++ b/test/EscaperTest.php @@ -180,6 +180,15 @@ public function setUp() $this->escaper = new Escaper('UTF-8'); } + /** + * @expectedException \Zend\Escaper\Exception\InvalidArgumentException + * @expectedExceptionMessage Zend\Escaper\Escaper constructor parameter must be a string, received integer + */ + public function testSettingEncodingToNonStringShouldThrowException() + { + $escaper = new Escaper(1); + } + /** * @expectedException \Zend\Escaper\Exception\InvalidArgumentException */