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 #27 from RickvdStaaij/feature/alnum-and-letters
Alnum and letters filter rules
- Loading branch information
Showing
6 changed files
with
234 additions
and
33 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
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,30 @@ | ||
<?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 AlNum | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class AlNum extends FilterRule | ||
{ | ||
/** | ||
* Only return alphabetic numeric characters | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
return preg_replace('/\W|_/', '', $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,30 @@ | ||
<?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 Letters | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class Letters extends FilterRule | ||
{ | ||
/** | ||
* Only return letters | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
return preg_replace('/[^\pL]/', '', $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,49 @@ | ||
<?php | ||
namespace Particle\Tests\Filter\FilterRule; | ||
|
||
use Particle\Filter\Filter; | ||
|
||
class AlnumTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Filter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* Prepare the filter | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new Filter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getAlnumResults | ||
* @param string $value | ||
* @param string $filteredValue | ||
*/ | ||
public function testAlnumFilterRule($value, $filteredValue) | ||
{ | ||
$this->filter->value('test')->alnum(); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($result['test'], $filteredValue); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAlnumResults() | ||
{ | ||
return [ | ||
['', ''], | ||
['0123456789', '0123456789'], | ||
['onlylettersinhere', 'onlylettersinhere'], | ||
['0l!e#t$t.e-r+s9', '0letters9'], | ||
]; | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
namespace Particle\Tests\Filter\FilterRule; | ||
|
||
use Particle\Filter\Filter; | ||
|
||
class LettersTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Filter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* Prepare the filter | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new Filter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getLettersResults | ||
* @param string $value | ||
* @param string $filteredValue | ||
*/ | ||
public function testLettersFilterRule($value, $filteredValue) | ||
{ | ||
$this->filter->value('test')->letters(); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($result['test'], $filteredValue); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getLettersResults() | ||
{ | ||
return [ | ||
['', ''], | ||
['onlylettersinhere', 'onlylettersinhere'], | ||
['0l!e#t$t.e-r+s9', 'letters'], | ||
]; | ||
} | ||
} |