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

Commit

Permalink
Added EndsWith, EvenNumber, OddNumber, ISBN, Lowercase, Titlecase & U…
Browse files Browse the repository at this point in the history
…ppercase rules
  • Loading branch information
Alphametric committed Apr 10, 2019
1 parent d33c6f5 commit b449439
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ The following validation rules are currently available:
| FileExists | validation.file_exists | Requires that the given value is a path to an existing file - see class for details |
| Equals | validation.equals | Requires that the given value is equal to another given value |
| MacAddress | validation.mac_address | Requires that the given value is a valid MAC address |
| ISBN | validation.isbn | Requires that the given value is a valid ISBN-10 or ISBN-13 number |
| EndsWith | validation.ends_with | Requires that the given value ends with a given string - see class for details |
| EvenNumber | validation.even_number | Requires that the given value is an even number (decimals are first converted using intval) |
| OddNumber | validation.odd_number | Requires that the given value is an odd number (decimals are first converted using intval) |
| Lowercase | validation.lowercase | Requires that the given value is lowercase string |
| Uppercase | validation.uppercase | Requires that the given value is uppercase string |
| Titlecase | validation.titlecase | Requires that the given value is titlecase string |

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
46 changes: 46 additions & 0 deletions src/EndsWith.php
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] . '"'
);
}

}
40 changes: 40 additions & 0 deletions src/EvenNumber.php
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'
);
}

}
42 changes: 42 additions & 0 deletions src/ISBN.php
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'
);
}

}
40 changes: 40 additions & 0 deletions src/Lowercase.php
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'
);
}

}
40 changes: 40 additions & 0 deletions src/OddNumber.php
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'
);
}

}
40 changes: 40 additions & 0 deletions src/Titlecase.php
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'
);
}

}
40 changes: 40 additions & 0 deletions src/Uppercase.php
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'
);
}

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

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

}
Loading

0 comments on commit b449439

Please sign in to comment.