This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from thexpand/string-prefix-suffix
Add StringPrefix filter with tests
- Loading branch information
Showing
4 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-filter for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-filter/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use Traversable; | ||
|
||
class StringPrefix extends AbstractFilter | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $options = [ | ||
'prefix' => null, | ||
]; | ||
|
||
/** | ||
* @param string|array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options !== null) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Set the prefix string | ||
* | ||
* @param string $prefix | ||
* | ||
* @return self | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function setPrefix($prefix) | ||
{ | ||
if (! is_string($prefix)) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'%s expects "prefix" to be string; received "%s"', | ||
__METHOD__, | ||
(is_object($prefix) ? get_class($prefix) : gettype($prefix)) | ||
)); | ||
} | ||
|
||
$this->options['prefix'] = $prefix; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the prefix string, which is appended at the beginning of the input value | ||
* | ||
* @return string | ||
*/ | ||
public function getPrefix() | ||
{ | ||
if (! isset($this->options['prefix'])) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'%s expects a "prefix" option; none given', | ||
__CLASS__ | ||
)); | ||
} | ||
|
||
return $this->options['prefix']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (! is_scalar($value)) { | ||
return $value; | ||
} | ||
|
||
$value = (string)$value; | ||
|
||
return $this->getPrefix() . $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-filter for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-filter/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use Traversable; | ||
|
||
class StringSuffix extends AbstractFilter | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $options = [ | ||
'suffix' => null, | ||
]; | ||
|
||
/** | ||
* @param string|array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options !== null) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Set the suffix string | ||
* | ||
* @param string $suffix | ||
* | ||
* @return self | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function setSuffix($suffix) | ||
{ | ||
if (! is_string($suffix)) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'%s expects "suffix" to be string; received "%s"', | ||
__METHOD__, | ||
(is_object($suffix) ? get_class($suffix) : gettype($suffix)) | ||
)); | ||
} | ||
|
||
$this->options['suffix'] = $suffix; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the suffix string, which is appended at the end of the input value | ||
* | ||
* @return string | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function getSuffix() | ||
{ | ||
if (! isset($this->options['suffix'])) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'%s expects a "suffix" option; none given', | ||
__CLASS__ | ||
)); | ||
} | ||
|
||
return $this->options['suffix']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (! is_scalar($value)) { | ||
return $value; | ||
} | ||
|
||
$value = (string)$value; | ||
|
||
return $value . $this->getSuffix(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-filter for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-filter/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zend\Filter\Exception\InvalidArgumentException; | ||
use Zend\Filter\StringPrefix as StringPrefixFilter; | ||
|
||
class StringPrefixTest extends TestCase | ||
{ | ||
/** | ||
* @var StringPrefixFilter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new StringPrefixFilter(); | ||
} | ||
|
||
/** | ||
* Ensures that the filter follows expected behavior | ||
* | ||
* @return void | ||
*/ | ||
public function testBasic() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$prefix = 'ABC123'; | ||
$filter->setPrefix($prefix); | ||
|
||
$this->assertStringStartsWith($prefix, $filter('sample')); | ||
} | ||
|
||
public function testWithoutPrefix() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('expects a "prefix" option; none given'); | ||
$filter('sample'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function invalidPrefixesDataProvider() | ||
{ | ||
return [ | ||
[1], | ||
[1.00], | ||
[true], | ||
[null], | ||
[[]], | ||
[fopen('php://memory', 'rb+')], | ||
[ | ||
function () { | ||
}, | ||
], | ||
[new \stdClass()], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider invalidPrefixesDataProvider | ||
* | ||
* @param mixed $prefix | ||
*/ | ||
public function testInvalidPrefixes($prefix) | ||
{ | ||
$filter = $this->filter; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('expects "prefix" to be string'); | ||
|
||
$filter->setPrefix($prefix); | ||
$filter('sample'); | ||
} | ||
|
||
public function testNonScalarInput() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$prefix = 'ABC123'; | ||
$filter->setPrefix($prefix); | ||
|
||
$this->assertInstanceOf(\stdClass::class, $filter(new \stdClass())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-filter for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-filter/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zend\Filter\Exception\InvalidArgumentException; | ||
use Zend\Filter\StringSuffix as StringSuffixFilter; | ||
|
||
class StringSuffixTest extends TestCase | ||
{ | ||
/** | ||
* @var StringSuffixFilter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new StringSuffixFilter(); | ||
} | ||
|
||
/** | ||
* Ensures that the filter follows expected behavior | ||
* | ||
* @return void | ||
*/ | ||
public function testBasic() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$suffix = 'ABC123'; | ||
$filter->setSuffix($suffix); | ||
|
||
$this->assertStringEndsWith($suffix, $filter('sample')); | ||
} | ||
|
||
public function testWithoutSuffix() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('expects a "suffix" option; none given'); | ||
$filter('sample'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function invalidSuffixesDataProvider() | ||
{ | ||
return [ | ||
[1], | ||
[1.00], | ||
[true], | ||
[null], | ||
[[]], | ||
[fopen('php://memory', 'rb+')], | ||
[ | ||
function () { | ||
}, | ||
], | ||
[new \stdClass()], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider invalidSuffixesDataProvider | ||
* | ||
* @param mixed $suffix | ||
*/ | ||
public function testInvalidSuffixes($suffix) | ||
{ | ||
$filter = $this->filter; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('expects "suffix" to be string'); | ||
|
||
$filter->setSuffix($suffix); | ||
$filter('sample'); | ||
} | ||
|
||
public function testNonScalarInput() | ||
{ | ||
$filter = $this->filter; | ||
|
||
$suffix = 'ABC123'; | ||
$filter->setSuffix($suffix); | ||
|
||
$this->assertInstanceOf(\stdClass::class, $filter(new \stdClass())); | ||
} | ||
} |