Skip to content

Commit

Permalink
Add Character Case Rules
Browse files Browse the repository at this point in the history
- Add CaseLower (lowercase)
- Add CaseUpper (UPPERCASE)
- Add CaseTitle (Title Case)
  • Loading branch information
jakejohns committed Mar 4, 2016
1 parent 0c3f5d8 commit 8b00447
Show file tree
Hide file tree
Showing 17 changed files with 543 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Locator/SanitizeLocator.php
Expand Up @@ -37,6 +37,9 @@ protected function initFactories(array $factories)
'between' => function () { return new Sanitize\Between(); },
'bool' => function () { return new Sanitize\Boolean(); },
'callback' => function () { return new Sanitize\Callback(); },
'caseLower' => function () { return new Sanitize\CaseLower(); },
'caseTitle' => function () { return new Sanitize\CaseTitle(); },
'caseUpper' => function () { return new Sanitize\CaseUpper(); },
'dateTime' => function () { return new Sanitize\DateTime(); },
'field' => function () { return new Sanitize\Field(); },
'float' => function () { return new Sanitize\Double(); },
Expand Down
3 changes: 3 additions & 0 deletions src/Locator/ValidateLocator.php
Expand Up @@ -37,6 +37,9 @@ protected function initFactories(array $factories)
'between' => function () { return new Validate\Between(); },
'bool' => function () { return new Validate\Boolean(); },
'callback' => function () { return new Validate\Callback(); },
'caseLower' => function () { return new Validate\CaseLower(); },
'caseTitle' => function () { return new Validate\CaseTitle(); },
'caseUpper' => function () { return new Validate\CaseUpper(); },
'creditCard' => function () { return new Validate\CreditCard(); },
'dateTime' => function () { return new Validate\DateTime(); },
'email' => function () { return new Validate\Email(); },
Expand Down
91 changes: 91 additions & 0 deletions src/Rule/AbstractCharCase.php
@@ -0,0 +1,91 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule;

use Aura\Filter\Exception;

/**
*
* Abstract rule for character case filters; supports the `mbstring`
* extension.
*
* @package Aura.Filter
*
*/
abstract class AbstractCharCase
{
/**
*
* Is the `mbstring` extension loaded?
*
* @return bool
*
*/
protected function mbstring()
{
return extension_loaded('mbstring');
}

/**
*
* Proxy to `mb_convert_case()` when available; fall back to
* `utf8_decode()` and `strtolower()` otherwise.
*
* @param string $str String to convert case.
*
* @return string
*
*/
protected function strtolower($str)
{
if ($this->mbstring()) {
return mb_convert_case($str, MB_CASE_LOWER, 'UTF-8');
}

return strtolower(utf8_decode($str));
}

/**
*
* Proxy to `mb_convert_case()` when available; fall back to
* `utf8_decode()` and `strtoupper()` otherwise.
*
* @param string $str String to convert case.
*
* @return string
*
*/
protected function strtoupper($str)
{
if ($this->mbstring()) {
return mb_convert_case($str, MB_CASE_UPPER, 'UTF-8');
}

return strtoupper(utf8_decode($str));
}

/**
*
* Proxy to `mb_convert_case()` when available; fall back to
* `utf8_decode()` and `ucwords()` otherwise.
*
* @param string $str String to convert case.
*
* @return int
*
*/
protected function ucwords($str)
{
if ($this->mbstring()) {
return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
}

return ucwords(utf8_decode($str));
}
}
42 changes: 42 additions & 0 deletions src/Rule/Sanitize/CaseLower.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Sanitize;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Sanitizes a string to lowercase.
*
* @package Aura.Filter
*
*/
class CaseLower extends AbstractCharCase
{
/**
*
* Sanitizes a string to lowercase.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if the value was sanitized, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}
$subject->$field = $this->strtolower($value);
return true;
}
}
42 changes: 42 additions & 0 deletions src/Rule/Sanitize/CaseTitle.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Sanitize;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Sanitizes a string to title case.
*
* @package Aura.Filter
*
*/
class CaseTitle extends AbstractCharCase
{
/**
*
* Sanitizes a string to title case.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if the value was sanitized, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}
$subject->$field = $this->ucwords($value);
return true;
}
}
42 changes: 42 additions & 0 deletions src/Rule/Sanitize/CaseUpper.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Sanitize;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Sanitizes a string to uppercase.
*
* @package Aura.Filter
*
*/
class CaseUpper extends AbstractCharCase
{
/**
*
* Sanitizes a string to uppercase.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if the value was sanitized, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}
$subject->$field = $this->strtoupper($value);
return true;
}
}
42 changes: 42 additions & 0 deletions src/Rule/Validate/CaseLower.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Validate;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Validates that the string is all lowercase.
*
* @package Aura.Filter
*
*/
class CaseLower extends AbstractCharCase
{
/**
*
* Validates that the string is lowercase.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if valid, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}

return $this->strtolower($value) == $value;
}
}
42 changes: 42 additions & 0 deletions src/Rule/Validate/CaseTitle.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Validate;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Validates that the string is all title case.
*
* @package Aura.Filter
*
*/
class CaseTitle extends AbstractCharCase
{
/**
*
* Validates that the string is title case.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if valid, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}

return $this->ucwords($value) == $value;
}
}
42 changes: 42 additions & 0 deletions src/Rule/Validate/CaseUpper.php
@@ -0,0 +1,42 @@
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Filter\Rule\Validate;

use Aura\Filter\Rule\AbstractCharCase;

/**
*
* Validates that the string is all uppercase.
*
* @package Aura.Filter
*
*/
class CaseUpper extends AbstractCharCase
{
/**
*
* Validates that the string is uppercase.
*
* @param object $subject The subject to be filtered.
*
* @param string $field The subject field name.
*
* @return bool True if valid, false if not.
*
*/
public function __invoke($subject, $field)
{
$value = $subject->$field;
if (! is_scalar($value)) {
return false;
}

return $this->strtoupper($value) == $value;
}
}

0 comments on commit 8b00447

Please sign in to comment.