Skip to content

Commit

Permalink
Add encode filter-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick van der Staaij committed Jun 26, 2015
1 parent e67a606 commit b86c34d
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 18 deletions.
47 changes: 30 additions & 17 deletions src/FilterResource.php
Expand Up @@ -35,7 +35,7 @@ public function __construct(Filter $filter, $keys = null)
}

/**
* Add the alphabetic numeric rule to the chain
* Results rule that returns alphabetic numeric characters from the value
*
* @return $this
*/
Expand All @@ -45,7 +45,7 @@ public function alnum()
}

/**
* Add append filter-rule to the chain
* Results rule that returns the value appended with a given value
*
* @param string $append
* @return Chain
Expand All @@ -56,7 +56,7 @@ public function append($append)
}

/**
* Add bool filter-rule to the chain
* Results rule that returns a casted boolean
*
* @return $this
*/
Expand All @@ -66,8 +66,9 @@ public function bool()
}

/**
* Add callback filter-rule to the chain
* Returns rule that returns a value modified by a callable closure
*
* @param callable $callable
* @return $this
*/
public function callback(callable $callable)
Expand All @@ -76,7 +77,19 @@ public function callback(callable $callable)
}

/**
* Add float filter-rule to the chain
* Returns rule that returns an value in a specific encoding format
*
* @param string|null $toEncodingFormat
* @param string|null $fromEncodingFormat
* @return FilterResource
*/
public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
{
return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
}

/**
* Returns rule that results a casted float
*
* @return $this
*/
Expand All @@ -86,7 +99,7 @@ public function float()
}

/**
* Add int filter-rule to the chain
* Returns rule that results a casted int
*
* @return $this
*/
Expand All @@ -96,7 +109,7 @@ public function int()
}

/**
* Add the letters-rule to the chain
* Returns rule that results all letters of a value
*
* @return $this
*/
Expand All @@ -106,7 +119,7 @@ public function letters()
}

/**
* Add lower filter-rule to the chain
* Returns rule that results a lower-cased value
*
* @return $this
*/
Expand All @@ -116,7 +129,7 @@ public function lower()
}

/**
* Add the numbers-rule to the chain
* Returns rule that results all numbers of a value
*
* @return $this
*/
Expand All @@ -126,7 +139,7 @@ public function numbers()
}

/**
* Add prepend filter-rule to the chain
* Results rule that returns the value prepended with a given value
*
* @param string $prepend
* @return Chain
Expand All @@ -137,7 +150,7 @@ public function prepend($prepend)
}

/**
* Add replace filter-rule to the chain
* Results rule that returns a value with replacements by a regex
*
* @param string $searchRegex
* @param string $replace
Expand All @@ -149,7 +162,7 @@ public function regexReplace($searchRegex, $replace)
}

/**
* Add replace filter-rule to the chain
* Results rule that returns a value with replacements
*
* @param mixed $search
* @param mixed $replace
Expand All @@ -161,7 +174,7 @@ public function replace($search, $replace)
}

/**
* Add string filter-rule to the chain
* Returns rule that results a casted string
*
* @return $this
*/
Expand All @@ -171,7 +184,7 @@ public function string()
}

/**
* Add stripHtml filter-rule to the chain
* Results rule that results a html-stripped value
*
* @param null|string $excludeTags
* @return $this
Expand All @@ -182,7 +195,7 @@ public function stripHtml($excludeTags = null)
}

/**
* Add trim filter-rule to the chain
* Returns rule that results a trimmed value
*
* @param string|null $characters
* @return $this
Expand All @@ -193,7 +206,7 @@ public function trim($characters = null)
}

/**
* Add upper filter-rule to the chain
* Results rule that returns an upper-cased value
*
* @return $this
*/
Expand All @@ -203,7 +216,7 @@ public function upper()
}

/**
* Add upper-first filter-rule to the chain
* Returns rule that results a value starting with a upper-cased character
*
* @return $this
*/
Expand Down
62 changes: 62 additions & 0 deletions src/FilterRule/Encode.php
@@ -0,0 +1,62 @@
<?php
/**
* Particle.
*
* @link http://github.com/particle-php for the canonical source repository
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
*/
namespace Particle\Filter\FilterRule;

use Particle\Filter\FilterRule;

/**
* Class Encode
*
* @package Particle\Filter\FilterRule
*/
class Encode extends FilterRule
{
/**
* @var string
*/
protected $toEncoding;

/**
* @var string|null
*/
protected $fromEncoding;

/**
* @param string|null $toEncoding
* @param string|null $fromEncoding
*/
public function __construct($toEncoding = null, $fromEncoding = null)
{
if ($toEncoding === null) {
$toEncoding = $this->encodingFormat;
}

$this->toEncoding = $toEncoding;
$this->fromEncoding = $fromEncoding;
}

/**
* Changes encoding of the value
*
* @param mixed $value
* @return string
*/
public function filter($value)
{
if ($this->toEncoding === null) {
return $value;
}

if ($this->fromEncoding === null) {
return mb_convert_encoding($value, $this->toEncoding);
}

return mb_convert_encoding($value, $this->toEncoding, $this->fromEncoding);
}
}
52 changes: 52 additions & 0 deletions tests/FilterRule/EncodeTest.php
@@ -0,0 +1,52 @@
<?php
namespace Particle\Tests\Filter\FilterRule;

use Particle\Filter\Filter;

class EncodeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Filter
*/
protected $filter;

/**
* Prepare the filter
*/
public function setUp()
{
$this->filter = new Filter();
}

/**
* @dataProvider getRegexReplaceResults
* @param string $value
* @param string|null $toFormat
* @param string|null $fromFormat
* @param string $filteredValue
*/
public function testRegexReplaceFilterRule($value, $toFormat, $fromFormat, $filteredValue)
{
$this->filter->value('test')->encode($toFormat, $fromFormat);

$result = $this->filter->filter([
'test' => $value
]);

$this->assertEquals($filteredValue, $result['test']);
}

/**
* @return array
*/
public function getRegexReplaceResults()
{
return [
['', null, null, ''],
['hello', 'UTF-8', null, 'hello'],
['hello', 'Base64', null, 'aGVsbG8='],
['hello', 'Base64', 'UTF-8', 'aGVsbG8='],
['aGVsbG8=', 'UTF-8', 'Base64', 'hello'],
];
}
}
2 changes: 1 addition & 1 deletion tests/FilterRule/RegexReplaceTest.php
Expand Up @@ -21,7 +21,7 @@ public function setUp()
/**
* @dataProvider getRegexReplaceResults
* @param string $value
* @param string $search
* @param string $searchRegex
* @param string $replace
* @param string $filteredValue
*/
Expand Down

0 comments on commit b86c34d

Please sign in to comment.