Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rick van der Staaij
committed
Jun 26, 2015
1 parent
e67a606
commit b86c34d
Showing
4 changed files
with
145 additions
and
18 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
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,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); | ||
} | ||
} |
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,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'], | ||
]; | ||
} | ||
} |
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