Skip to content

Commit

Permalink
[Filter] Adding ToLower and ToUpper filter rules, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
rdohms committed Jul 26, 2011
1 parent 49a5592 commit cb29ae0
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Rules/ToLower.php
@@ -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;
}

}
65 changes: 65 additions & 0 deletions Rules/ToUpper.php
@@ -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;
}

}

0 comments on commit cb29ae0

Please sign in to comment.