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.
- Loading branch information
Alphametric
committed
Mar 29, 2019
1 parent
80ab219
commit e61e081
Showing
3 changed files
with
106 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,61 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Decimal rule | ||
class Decimal extends Rule | ||
{ | ||
|
||
/** | ||
* Generate an example value that satisifies the validation rule. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function example() | ||
{ | ||
return mt_rand(1, (int) str_repeat('9', $this->parameters[0])) . '.' . | ||
mt_rand(1, (int) str_repeat('9', $this->parameters[1])); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The decimal requires two parameters: | ||
* 2. The maximum number of digits before the decimal point. | ||
* 3. The maximum number of digits after the decimal point. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return preg_match( | ||
"/^[0-9]{1,{$this->parameters[0]}}(\.[0-9]{1,{$this->parameters[1]}})$/", $value | ||
); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'decimal', | ||
'The :attribute must be a decimal of a set precision / scale e.g. ' . $this->example() | ||
); | ||
} | ||
|
||
} |
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,44 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules\Tests; | ||
|
||
// Using directives | ||
use Alphametric\Validation\Rules\Decimal; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
// Decimal test | ||
class DecimalTest extends Orchestra | ||
{ | ||
|
||
/** @test */ | ||
public function a_decimal_can_be_validated() | ||
{ | ||
// Define the validation rule | ||
$rule = ['figure' => [new Decimal(4, 2)]]; | ||
|
||
// Execute the tests | ||
$this->assertFalse(validator(['figure' => '1'], $rule)->passes()); | ||
$this->assertFalse(validator(['figure' => '123'], $rule)->passes()); | ||
$this->assertFalse(validator(['figure' => '1234'], $rule)->passes()); | ||
$this->assertFalse(validator(['figure' => '1234.'], $rule)->passes()); | ||
$this->assertTrue(validator(['figure' => '1234.5'], $rule)->passes()); | ||
$this->assertTrue(validator(['figure' => '1234.56'], $rule)->passes()); | ||
$this->assertFalse(validator(['figure' => '1234.567'], $rule)->passes()); | ||
$this->assertTrue(validator(['figure' => '1234.0'], $rule)->passes()); | ||
$this->assertTrue(validator(['figure' => '1234.00'], $rule)->passes()); | ||
} | ||
|
||
|
||
|
||
/** @test */ | ||
public function a_decimal_example_is_valid() | ||
{ | ||
// Define the validation rule | ||
$rule = ['figure' => [$class = new Decimal(4, 2)]]; | ||
|
||
// Execute the tests | ||
$this->assertTrue(validator(['figure' => $class -> example()], $rule)->passes()); | ||
} | ||
|
||
} |