Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Added Decimal rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphametric committed Mar 29, 2019
1 parent 80ab219 commit e61e081
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following validation rules are currently available:
| MonetaryFigure | validation.monetary_figure | Requires the presence of a monetary figure e.g $72.33 - see class for details |
| DisposableEmail | validation.disposable_email | Requires the presence of an email address which is not disposable |
| DoesNotExist | validation.does_not_exist | Requires that the given value is not present in a given database table / column - see class for details |
| Decimal | validation.decimal | Requires that the given value is a decimal with the given precision / scale - see class for details |

The package will receive new rules over time, however since these updates will not be breaking changes, they will not receive major version numbers unless Laravel changes in such a way that the package requires a re-write.

Expand Down
61 changes: 61 additions & 0 deletions src/Decimal.php
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()
);
}

}
44 changes: 44 additions & 0 deletions tests/DecimalTest.php
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());
}

}

0 comments on commit e61e081

Please sign in to comment.