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

Add StringPrefix filter with tests #69

Merged
merged 8 commits into from
Dec 12, 2018
84 changes: 84 additions & 0 deletions src/StringPrefix.php
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;
}
}
85 changes: 85 additions & 0 deletions src/StringSuffix.php
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();
}
}
98 changes: 98 additions & 0 deletions test/StringPrefixTest.php
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'));
thexpand marked this conversation as resolved.
Show resolved Hide resolved
}

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()));
}
}
98 changes: 98 additions & 0 deletions test/StringSuffixTest.php
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()));
}
}