From 437ca1e8bc4086c5e1732e0878f13e8f2c530c3f Mon Sep 17 00:00:00 2001 From: SandroMiguel Date: Thu, 14 Jul 2022 15:16:22 +0100 Subject: [PATCH] feat: new rule: integer --- README.md | 29 +++++++ src/Enum/RuleEnum.php | 1 + src/I18n/en.php | 4 + src/I18n/nl-nl.php | 70 ++++++++++------- src/I18n/pt-br.php | 4 + src/I18n/pt-pt.php | 4 + src/Rules/Integer.php | 69 ++++++++++++++++ tests/IntegerTest.php | 178 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 331 insertions(+), 28 deletions(-) create mode 100644 src/Rules/Integer.php create mode 100644 tests/IntegerTest.php diff --git a/README.md b/README.md index d822c05..3deb9d1 100644 --- a/README.md +++ b/README.md @@ -410,6 +410,7 @@ if ($myCustomValidationFail) { 1. [image_max_width](#image_max_width) 1. [image_min_height](#image_min_height) 1. [image_min_width](#image_min_width) +1. [integer](#integer) 1. [ip](#ip) 1. [ipv4](#ipv4) 1. [ipv6](#ipv6) @@ -868,6 +869,34 @@ $rules = [ | 400px | :x: | :x: | | 600px | :heavy_check_mark: | :heavy_check_mark: | +### integer + +Checks whether the value is integer. + +``` +$rules = [ + 'distance' => [ + 'label' => 'Distance', + 'rules' => [ + RuleEnum::INTEGER, + ], + ], +]; +``` + +| Value | numeric | numeric + required | +| -------- | :----------------: | :----------------: | +| `null` | :heavy_check_mark: | :x: | +| `''` | :heavy_check_mark: | :x: | +| `'0'` | :x: | :x: | +| `0` | :heavy_check_mark: | :heavy_check_mark: | +| `false` | :x: | :x: | +| `[]` | :x: | :x: | +| `-1` | :heavy_check_mark: | :heavy_check_mark: | +| `1` | :heavy_check_mark: | :heavy_check_mark: | +| `true` | :x: | :x: | +| `'text'` | :x: | :x: | + ### ip Checks whether the value is a valid IP address. diff --git a/src/Enum/RuleEnum.php b/src/Enum/RuleEnum.php index a9ba42a..cff6215 100644 --- a/src/Enum/RuleEnum.php +++ b/src/Enum/RuleEnum.php @@ -39,6 +39,7 @@ abstract class RuleEnum public const IMAGE_MAX_WIDTH = 'image_max_width'; public const IMAGE_MIN_HEIGHT = 'image_min_height'; public const IMAGE_MIN_WIDTH = 'image_min_width'; + public const INTEGER = 'integer'; public const IP = 'ip'; public const IPV4 = 'ipv4'; public const IPV6 = 'ipv6'; diff --git a/src/I18n/en.php b/src/I18n/en.php index 059fe01..24da7a3 100644 --- a/src/I18n/en.php +++ b/src/I18n/en.php @@ -83,6 +83,10 @@ 'withLabel' => 'The uploaded image width of the "{param:3}" field is {param:1}px. The minimum width is {param:2}px.', 'withoutLabel' => 'The uploaded image width of this field is {param:1}px. The minimum width is {param:2}px.', ], + RuleEnum::INTEGER => [ + 'withLabel' => 'The "{param:1}" field must be integer.', + 'withoutLabel' => 'This field must be integer.', + ], RuleEnum::IP => [ 'withLabel' => 'The "{param:1}" field must be a valid IP address.', 'withoutLabel' => 'This field must be a valid IP address.', diff --git a/src/I18n/nl-nl.php b/src/I18n/nl-nl.php index f29d5ca..015a98b 100644 --- a/src/I18n/nl-nl.php +++ b/src/I18n/nl-nl.php @@ -16,116 +16,130 @@ declare(strict_types=1); +use Verum\Enum\RuleEnum; + return [ - 'alpha' => [ + RuleEnum::ALPHA => [ 'withLabel' => 'Het veld "{param:1}" moet letters (a-z) bevatten.', 'withoutLabel' => 'Dit veld moet letters (a-z) bevatten.', ], - 'alpha_numeric' => [ + RuleEnum::ALPHA_NUMERIC => [ 'withLabel' => 'Het veld "{param:1}" moet letters (a-z) en/of cijfers (0-9) bevatten.', 'withoutLabel' => 'Dit veld moet letters (a-z) en/of cijfers (0-9) bevatten.', ], - 'between' => [ + RuleEnum::BETWEEN => [ 'withLabel' => 'De waarde van het veld "{param:3}" moet tussen {param:1} en {param:2} liggen.', 'withoutLabel' => 'De waarde van dit veld moet tussen {param:1} en {param:2} liggen.', ], - 'between_length' => [ + RuleEnum::BETWEEN_LENGTH => [ 'withLabel' => 'De waarde van het veld "{param:3}" moet tussen {param:1} en {param:2} tekens liggen.', 'withoutLabel' => 'De waarde van dit veld moet tussen {param:1} en {param:2} tekens liggen.', ], - 'boolean_value' => [ + RuleEnum::BOOLEAN_VALUE => [ 'withLabel' => 'Het veld "{param:1}" moet een boolean bevatten.', 'withoutLabel' => 'Dit veld moet een boolean bevatten.', ], - 'contains' => [ + RuleEnum::CONTAINS => [ 'withLabel' => 'Het veld "{param:3}" moet de waarde "{param:1}" bevatten. Geldige waardes zijn: {param:2}', 'withoutLabel' => 'Dit veld moet de waarde "{param:1}" bevatten. Geldige waardes zijn: {param:2}', ], - 'date' => [ + RuleEnum::DATE => [ 'withLabel' => 'Het veld "{param:2}" moet een geldige datum in dit formaat bevatten: "{param:1}"', 'withoutLabel' => 'Dit veld moet een geldige datum in dit formaat bevatten: "{param:1}"', ], - 'email' => [ + RuleEnum::EMAIL => [ 'withLabel' => 'Het veld "{param:1}" moet een geldig e-mail adres bevatten.', 'withoutLabel' => 'Dit veld moet een geldig e-mail adres bevatten.', ], - 'equals' => [ + RuleEnum::EQUALS => [ 'withLabel' => 'De velden {param:1} en {param:2} moeten hetzelfde zijn.', 'withoutLabel' => 'De velden moeten hetzelfde zijn.', ], - 'file_max_size' => [ + RuleEnum::FILE_MAX_SIZE => [ 'withLabel' => 'Het veld "{param:3}" bevat een bestandsgrootte van {param:1}. De waarde moet minder zijn dan {param:2}.', 'withoutLabel' => 'Dit veld bevat een bestandsgrootte van {param:1}. De waarde moet minder zijn dan {param:2}.', ], - 'file_mime_type' => [ + RuleEnum::FILE_MIME_TYPE => [ 'withLabel' => 'Het veld "{param:3}" bevat een bestand van het type {param:1}. Het type moet {param:2} zijn.', 'withoutLabel' => 'Dit veld bevat een bestand van het type {param:1}. Het type moet {param:2} zijn.', ], - 'float_number' => [ + RuleEnum::FLOAT_NUMBER => [ 'withLabel' => 'Het veld "{param:1}" moet een getal met een punt als komma bevatten.', 'withoutLabel' => 'Dit veld moet een getal met een punt als komma bevatten.', ], - 'image_max_height' => [ + RuleEnum::IMAGE_MAX_HEIGHT => [ 'withLabel' => 'De hoogte van de geüploade afbeelding in het veld "{param:3}" is {param:1}px. De maximale hoogte is {param:2} px.', 'withoutLabel' => 'De hoogte van de geüploade afbeelding is {param:1}px. De maximale hoogte is {param:2} px.', ], - 'image_max_width' => [ + RuleEnum::IMAGE_MAX_WIDTH => [ 'withLabel' => 'De hoogte van de geüploade afbeelding in het veld "{param:3}" is {param:1}px. De maximale breedte is {param:2} px.', 'withoutLabel' => 'De hoogte van de geüploade afbeelding is {param:1}px. De maximale breedte is {param:2} px.', ], - 'image_min_height' => [ + RuleEnum::IMAGE_MIN_HEIGHT => [ 'withLabel' => 'De hoogte van de geüploade afbeelding in het veld "{param:3}" is {param:1}px. De minimale hoogte is {param:2} px.', 'withoutLabel' => 'De hoogte van de geüploade afbeelding is {param:1}px. De minimale hoogte is {param:2} px.', ], - 'image_min_width' => [ + RuleEnum::IMAGE_MIN_WIDTH => [ 'withLabel' => 'De breedte van de geüploade afbeelding in het veld "{param:3}" is {param:1}px. De minimale breedte is {param:2} px.', 'withoutLabel' => 'De breedte van de geüploade afbeelding is {param:1}px. De minimale breedte is {param:2} px.', ], - 'ip' => [ + RuleEnum::INTEGER => [ + 'withLabel' => 'Het veld "{param:1}" moet een geheel getal zijn.', + 'withoutLabel' => 'Dit veld moet een geheel getal zijn.', + ], + RuleEnum::IP => [ 'withLabel' => 'Het veld "{param:1}" moet een geldig IP-adres bevatten.', 'withoutLabel' => 'Dit veld moet een geldig IP-adres bevatten.', ], - 'ipv4' => [ + RuleEnum::IPV4 => [ 'withLabel' => 'Het veld "{param:1}" moet een geldig IPv4-adres bevatten.', 'withoutLabel' => 'Dit veld moet een geldig IPv4-adres bevatten.', ], - 'ipv6' => [ + RuleEnum::IPV6 => [ 'withLabel' => 'Het veld "{param:1}" moet een geldig IPv6-adres bevatten.', 'withoutLabel' => 'Dit veld moet een geldig IPv6-adres bevatten.', ], - 'max' => [ + RuleEnum::MAX => [ 'withLabel' => 'Het veld "{param:2}" mag niet hoger dan {param:1} zijn.', 'withoutLabel' => 'Dit veld mag niet hoger dan {param:1} zijn.', ], - 'max_length' => [ + RuleEnum::MAX_LENGTH => [ 'withLabel' => 'Het veld "{param:2}" mag niet groter zijn dan {param:1} tekens.', 'withoutLabel' => 'Dit veld mag niet groter zijn dan {param:1} tekens.', ], - 'min' => [ + RuleEnum::MIN => [ 'withLabel' => 'Het veld "{param:2}" moet minstens {param:1} zijn.', 'withoutLabel' => 'De waarde van dit veld moet minstens {param:1} zijn.', ], - 'min_length' => [ + RuleEnum::MIN_LENGTH => [ 'withLabel' => 'Het veld "{param:2}" moet minimaal {param:1} tekens lang zijn.', 'withoutLabel' => 'Dit veld moet minimaal {param:1} tekens lang zijn.', ], - 'numeric' => [ + RuleEnum::NUMERIC => [ 'withLabel' => 'Het veld "{param:1}" moet een numerieke waarde hebben.', 'withoutLabel' => 'Het veld moet een numerieke waarde hebben.', ], - 'regex' => [ + RuleEnum::REGEX => [ 'withLabel' => 'Het veld "{param:2}" voldoet niet aan het juiste patroon {param:1}.', 'withoutLabel' => 'Dit veld voldoet niet aan het juiste patroon {param:1}.', ], - 'required' => [ + RuleEnum::REQUIRED => [ + 'withLabel' => 'Het veld "{param:1}" is verplicht.', + 'withoutLabel' => 'Dit veld is verplicht.', + ], + RuleEnum::REQUIRED_IF => [ + 'withLabel' => 'Het veld "{param:1}" is verplicht.', + 'withoutLabel' => 'Dit veld is verplicht.', + ], + RuleEnum::REQUIRED_IF_NOT => [ 'withLabel' => 'Het veld "{param:1}" is verplicht.', 'withoutLabel' => 'Dit veld is verplicht.', ], - 'slug' => [ + RuleEnum::SLUG => [ 'withLabel' => 'Het veld "{param:1}" moet een geldige slug bevatten (bijv. hallo-wereld_123).', 'withoutLabel' => 'Dit veld moet een geldige slug bevatten (bijv. hallo-wereld_123).', ], - 'url' => [ + RuleEnum::URL => [ 'withLabel' => 'Het veld "{param:1}" moet een geldige URL bevatten.', 'withoutLabel' => 'Dit veld moet een geldige URL bevatten.', ], diff --git a/src/I18n/pt-br.php b/src/I18n/pt-br.php index 93bd780..ceda8ab 100644 --- a/src/I18n/pt-br.php +++ b/src/I18n/pt-br.php @@ -83,6 +83,10 @@ 'withLabel' => 'A largura da imagem enviada no campo "{param:3}" é de {param:1}px. A largura mínima é de {param:2}px.', 'withoutLabel' => 'A largura da imagem enviada neste campo é de {param:1}px. A largura mínima é de {param:2}px.', ], + RuleEnum::INTEGER => [ + 'withLabel' => 'O campo "{param:1}" deve ser um número inteiro.', + 'withoutLabel' => 'Este campo deve ser um número inteiro.', + ], RuleEnum::IP => [ 'withLabel' => 'O campo "{param:1}" deve ser um endereço IP válido.', 'withoutLabel' => 'Este campo deve ser um endereço IP válido.', diff --git a/src/I18n/pt-pt.php b/src/I18n/pt-pt.php index edce059..e768f49 100644 --- a/src/I18n/pt-pt.php +++ b/src/I18n/pt-pt.php @@ -83,6 +83,10 @@ 'withLabel' => 'A largura da imagem enviada no campo "{param:3}" é de {param:1}px. A largura mínima é de {param:2}px.', 'withoutLabel' => 'A largura da imagem enviada neste campo é de {param:1}px. A largura mínima é de {param:2}px.', ], + RuleEnum::INTEGER => [ + 'withLabel' => 'O campo "{param:1}" deve ser um número inteiro.', + 'withoutLabel' => 'Este campo deve ser um número inteiro.', + ], RuleEnum::IP => [ 'withLabel' => 'O campo "{param:1}" deve ser um endereço IP válido.', 'withoutLabel' => 'Este campo deve ser um endereço IP válido.', diff --git a/src/Rules/Integer.php b/src/Rules/Integer.php new file mode 100644 index 0000000..4954a6a --- /dev/null +++ b/src/Rules/Integer.php @@ -0,0 +1,69 @@ + + * @copyright 2022 Sandro + * @since Verum-PHP 2.1.0 + * @version 1.0.0 (2022/07/13) + * @link https://github.com/SandroMiguel/verum-php + */ + +declare(strict_types=1); + +namespace Verum\Rules; + +/** + * Class Integer | src/Rules/Integer.php + * Checks whether the value is integer. + */ +final class Integer extends Rule +{ + /** + * Integer constructor. + * + * @param mixed $fieldValue Field Value to validate. + * + * @version 1.0.0 (2022/07/13) + * @since Verum 2.1.0 + */ + public function __construct(mixed $fieldValue) + { + $this->fieldValue = $fieldValue; + } + + /** + * Validates the field value against the rule. + * + * @return bool Returns TRUE if it passes the validation, FALSE otherwise. + * + * @version 1.0.0 (2022/07/13) + * @since Verum 2.1.0 + */ + public function validate(): bool + { + if ($this->fieldValue === null || $this->fieldValue === '') { + return true; + } + + return is_int($this->fieldValue); + } + + /** + * Error Message Parameters. + * + * @return array Returns the parameters for the error message. + * + * @version 1.0.0 (2022/07/13) + * @since Verum 2.1.0 + */ + public function getErrorMessageParameters(): array + { + return [$this->fieldLabel]; + } +} diff --git a/tests/IntegerTest.php b/tests/IntegerTest.php new file mode 100644 index 0000000..43988a2 --- /dev/null +++ b/tests/IntegerTest.php @@ -0,0 +1,178 @@ + + * @copyright 2022 Sandro + * @since Verum-PHP 2.1.0 + * @version 1.0.0 (2022/07/13) + * @link https://github.com/SandroMiguel/verum-php + */ + +declare(strict_types=1); + +namespace Verum\Tests; + +use PHPUnit\Framework\TestCase; +use Verum\Rules\RuleFactory; +use Verum\Validator; + +/** + * Class IntegerTest | tests/IntegerTest.php | Test for Integer + */ +class IntegerTest extends TestCase +{ + /** + * Validates the field value against the rule. + * + * @param mixed $fieldValue Field Value to validate. + * + * @return bool Returns TRUE if it passes the validation, FALSE otherwise. + */ + private function validate(mixed $fieldValue): bool + { + $fieldName = 'some_field_name'; + $fieldLabel = 'Some Field Name'; + $ruleName = 'integer'; + $ruleValues = []; + $validator = new Validator( + [ + $fieldName => $fieldValue, + ], + [ + $fieldName => [ + 'label' => $fieldLabel, + 'rules' => [$ruleName => $ruleValues], + ], + ] + ); + $rule = RuleFactory::loadRule($validator, $fieldValue, $ruleValues, $fieldLabel, $ruleName, ''); + + return $rule->validate(); + } + + /** + * Null value should pass validation (ignored field). + * + * @return void + */ + public function testValidateNull(): void + { + $this->assertTrue($this->validate(null)); + } + + /** + * The String ('some text') value should not pass validation. + * + * @return void + */ + public function testValidateString(): void + { + $this->assertFalse($this->validate('some text')); + } + + /** + * An Empty String ('') value should pass validation (ignored field). + * + * @return void + */ + public function testValidateEmptyString(): void + { + $this->assertTrue($this->validate('')); + } + + /** + * A Zero String ('0') value should not pass validation. + * + * @return void + */ + public function testValidateZeroString(): void + { + $this->assertFalse($this->validate('0')); + } + + /** + * The Zero Number (0) value should pass validation. + * + * @return void + */ + public function testValidateZeroNumber(): void + { + $this->assertTrue($this->validate(0)); + } + + /** + * A Boolean (false) value should not pass validation. + * + * @return void + */ + public function testValidateFalse(): void + { + $this->assertFalse($this->validate(false)); + } + + /** + * An Empty Array ([]) value should not pass validation. + * + * @return void + */ + public function testValidateEmptyArray(): void + { + $this->assertFalse($this->validate([])); + } + + /** + * The Minus One (-1) value should pass validation. + * + * @return void + */ + public function testValidateMinusOne(): void + { + $this->assertTrue($this->validate(-1)); + } + + /** + * The String ('1') value should not pass validation. + * + * @return void + */ + public function testValidateStringNumber(): void + { + $this->assertFalse($this->validate('1')); + } + + /** + * The Numeric (1) value should pass validation. + * + * @return void + */ + public function testValidateNumber(): void + { + $this->assertTrue($this->validate(1)); + } + + /** + * A Boolean (true) value should not pass validation. + * + * @return void + */ + public function testValidateTrue(): void + { + $this->assertFalse($this->validate(true)); + } + + /** + * The String ('some-text-123') value should not pass validation. + * + * @return void + */ + public function testValidateSpecialChars(): void + { + $this->assertFalse($this->validate('some-text-123')); + } +}