Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Filter] Adding ToLower and ToUpper filter rules, closes #2
- Loading branch information
Showing
2 changed files
with
130 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,65 @@ | ||
<?php | ||
|
||
namespace DMS\Filter\Rules; | ||
|
||
use DMS\Filter\Exception\FilterException; | ||
|
||
/** | ||
* ToLower Rule | ||
* | ||
* @package DMS | ||
* @subpackage Filter | ||
* | ||
* @Annotation | ||
*/ | ||
class ToLower extends Rule | ||
{ | ||
/** | ||
* Encoding to be used | ||
* | ||
* @var string | ||
*/ | ||
public $encoding = null; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function applyFilter($value) | ||
{ | ||
if ($this->useEncoding()) { | ||
return mb_strtolower((string) $value, $this->encoding); | ||
} | ||
|
||
return strtolower((string) $value); | ||
} | ||
|
||
/** | ||
* Verify is encoding is set and if we have the proper | ||
* function to use it | ||
* | ||
* @return boolean | ||
*/ | ||
public function useEncoding() | ||
{ | ||
if ($this->encoding === null) { | ||
return false; | ||
} | ||
|
||
if (!function_exists('mb_strtolower')) { | ||
throw new FilterException( | ||
'mbstring is required to use ToLower with an encoding.'); | ||
} | ||
|
||
$this->encoding = (string) $this->encoding; | ||
$encodings = array_map('strtolower', mb_list_encodings()); | ||
|
||
if (!in_array(strtolower($this->encoding), $encodings)) { | ||
throw new FilterException( | ||
"mbstring does not support the '".$this->encoding."' encoding" | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
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,65 @@ | ||
<?php | ||
|
||
namespace DMS\Filter\Rules; | ||
|
||
use DMS\Filter\Exception\FilterException; | ||
|
||
/** | ||
* ToUpper Rule | ||
* | ||
* @package DMS | ||
* @subpackage Filter | ||
* | ||
* @Annotation | ||
*/ | ||
class ToUpper extends Rule | ||
{ | ||
/** | ||
* Encoding to be used | ||
* | ||
* @var string | ||
*/ | ||
public $encoding = null; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function applyFilter($value) | ||
{ | ||
if ($this->useEncoding()) { | ||
return mb_strtoupper((string) $value, $this->encoding); | ||
} | ||
|
||
return strtoupper((string) $value); | ||
} | ||
|
||
/** | ||
* Verify is encoding is set and if we have the proper | ||
* function to use it | ||
* | ||
* @return boolean | ||
*/ | ||
public function useEncoding() | ||
{ | ||
if ($this->encoding === null) { | ||
return false; | ||
} | ||
|
||
if (!function_exists('mb_strtoupper')) { | ||
throw new FilterException( | ||
'mbstring is required to use ToLower with an encoding.'); | ||
} | ||
|
||
$this->encoding = (string) $this->encoding; | ||
$encodings = array_map('strtolower', mb_list_encodings()); | ||
|
||
if (!in_array(strtolower($this->encoding), $encodings)) { | ||
throw new FilterException( | ||
"mbstring does not support the '".$this->encoding."' encoding" | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |