This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EndsWith, EvenNumber, OddNumber, ISBN, Lowercase, Titlecase & U…
…ppercase rules
- Loading branch information
Alphametric
committed
Apr 10, 2019
1 parent
d33c6f5
commit b449439
Showing
15 changed files
with
485 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Using directives | ||
use Str; | ||
|
||
// Ends with rule | ||
class EndsWith extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The rule has one parameter: | ||
* 1. The string the value must end with. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return Str::endsWith($value, $this->parameters[0]); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'ends_with', | ||
'The :attribute must end with the text "' . $this->parameters[0] . '"' | ||
); | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Even number rule | ||
class EvenNumber extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return intval($value) % 2 === 0; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'even_number', | ||
'The :attribute must be an even number' | ||
); | ||
} | ||
|
||
} |
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 | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// ISBN rule | ||
class ISBN extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return preg_match( | ||
"/^(?:ISBN(-1(?:(0)|3))?:?\ )?(?(1)(?(2)(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$)[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]|(?=[0-9]{13}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)97[89][- ]?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9])|(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X])$/", $value | ||
); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'ISBN', | ||
'The :attribute must be a valid ISBN 10 or ISBN 13 number' | ||
); | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Lowercase rule | ||
class Lowercase extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return mb_strtolower($value) === $value; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'lowercase', | ||
'The :attribute must be entirely lowercase text' | ||
); | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Odd number rule | ||
class OddNumber extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return intval($value) % 2 !== 0; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'odd_number', | ||
'The :attribute must be an odd number' | ||
); | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Titlecase rule | ||
class Titlecase extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return ucwords($value) === $value; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'titlecase', | ||
'Each word in :attribute must begin with a capital letter' | ||
); | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Uppercase rule | ||
class Uppercase extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return mb_strtoupper($value) === $value; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'uppercase', | ||
'The :attribute must be entirely uppercase text' | ||
); | ||
} | ||
|
||
} |
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,26 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules\Tests; | ||
|
||
// Using directives | ||
use Alphametric\Validation\Rules\EndsWith; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
// Ends with test | ||
class EndsWithTest extends Orchestra | ||
{ | ||
|
||
/** @test */ | ||
public function the_ends_with_rule_can_be_validated() | ||
{ | ||
// Define the validation rule | ||
$rule = ['text' => [new EndsWith('world')]]; | ||
|
||
// Execute the tests | ||
$this->assertFalse(validator(['text' => 'hello'], $rule)->passes()); | ||
$this->assertTrue(validator(['text' => 'hello world'], $rule)->passes()); | ||
$this->assertFalse(validator(['text' => 'hello worldy'], $rule)->passes()); | ||
} | ||
|
||
} |
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,27 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules\Tests; | ||
|
||
// Using directives | ||
use Alphametric\Validation\Rules\EvenNumber; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
// Even number test | ||
class EvenNumberTest extends Orchestra | ||
{ | ||
|
||
/** @test */ | ||
public function the_even_number_rule_can_be_validated() | ||
{ | ||
// Define the validation rule | ||
$rule = ['number' => [new EvenNumber]]; | ||
|
||
// Execute the tests | ||
$this->assertFalse(validator(['number' => '1'], $rule)->passes()); | ||
$this->assertTrue(validator(['number' => '2'], $rule)->passes()); | ||
$this->assertFalse(validator(['number' => '3'], $rule)->passes()); | ||
$this->assertTrue(validator(['number' => '4'], $rule)->passes()); | ||
} | ||
|
||
} |
Oops, something went wrong.