diff --git a/src/Locator/SanitizeLocator.php b/src/Locator/SanitizeLocator.php index 22ba36c9..60f9c738 100644 --- a/src/Locator/SanitizeLocator.php +++ b/src/Locator/SanitizeLocator.php @@ -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(); }, diff --git a/src/Locator/ValidateLocator.php b/src/Locator/ValidateLocator.php index 8bd5430c..f6fdce5c 100644 --- a/src/Locator/ValidateLocator.php +++ b/src/Locator/ValidateLocator.php @@ -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(); }, diff --git a/src/Rule/AbstractCharCase.php b/src/Rule/AbstractCharCase.php new file mode 100644 index 00000000..5c792de5 --- /dev/null +++ b/src/Rule/AbstractCharCase.php @@ -0,0 +1,91 @@ +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)); + } +} diff --git a/src/Rule/Sanitize/CaseLower.php b/src/Rule/Sanitize/CaseLower.php new file mode 100644 index 00000000..3ca9a664 --- /dev/null +++ b/src/Rule/Sanitize/CaseLower.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + $subject->$field = $this->strtolower($value); + return true; + } +} diff --git a/src/Rule/Sanitize/CaseTitle.php b/src/Rule/Sanitize/CaseTitle.php new file mode 100644 index 00000000..eda84a5f --- /dev/null +++ b/src/Rule/Sanitize/CaseTitle.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + $subject->$field = $this->ucwords($value); + return true; + } +} diff --git a/src/Rule/Sanitize/CaseUpper.php b/src/Rule/Sanitize/CaseUpper.php new file mode 100644 index 00000000..b881fb74 --- /dev/null +++ b/src/Rule/Sanitize/CaseUpper.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + $subject->$field = $this->strtoupper($value); + return true; + } +} diff --git a/src/Rule/Validate/CaseLower.php b/src/Rule/Validate/CaseLower.php new file mode 100644 index 00000000..c5f1ff24 --- /dev/null +++ b/src/Rule/Validate/CaseLower.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + + return $this->strtolower($value) == $value; + } +} diff --git a/src/Rule/Validate/CaseTitle.php b/src/Rule/Validate/CaseTitle.php new file mode 100644 index 00000000..8d485c97 --- /dev/null +++ b/src/Rule/Validate/CaseTitle.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + + return $this->ucwords($value) == $value; + } +} diff --git a/src/Rule/Validate/CaseUpper.php b/src/Rule/Validate/CaseUpper.php new file mode 100644 index 00000000..5d177de4 --- /dev/null +++ b/src/Rule/Validate/CaseUpper.php @@ -0,0 +1,42 @@ +$field; + if (! is_scalar($value)) { + return false; + } + + return $this->strtoupper($value) == $value; + } +} diff --git a/tests/Rule/CharCaseTest.php b/tests/Rule/CharCaseTest.php new file mode 100644 index 00000000..5e05ba78 --- /dev/null +++ b/tests/Rule/CharCaseTest.php @@ -0,0 +1,33 @@ +assertSame( + 'abcdef', + $fake->strtolower('ABCDEF') + ); + } + + public function testUpper() + { + $fake = new FakeCharCase(); + $this->assertSame( + 'ABCDEF', + $fake->strtoupper('abcdef') + ); + } + + public function testUcWords() + { + $fake = new FakeCharCase(); + $this->assertSame( + 'Abc Def', + $fake->ucwords('abc def') + ); + } + +} diff --git a/tests/Rule/FakeCharCase.php b/tests/Rule/FakeCharCase.php new file mode 100644 index 00000000..fe689936 --- /dev/null +++ b/tests/Rule/FakeCharCase.php @@ -0,0 +1,25 @@ +