Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add CaseLower (lowercase) - Add CaseUpper (UPPERCASE) - Add CaseTitle (Title Case)
- Loading branch information
Showing
17 changed files
with
543 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
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,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)); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.