Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

add non-string check parameter in __construct #25

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions test/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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
*/
Expand Down