Skip to content

Set of validation rules

  • All rules can be combined (all must be successful).
  • Except array* get methods has rules argument.
  • You can create your own rules by extending Wrkflow\GetValue\Contracts\RuleContract
  • You are free to add more rules to this package.

Checks if the string contains only digits/alphabet/-/_.

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\AlphaDashRule()]);

Checks if the value is valid IP (using FILTER_VALIDATE_IP)

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\AlphaNumericRule()]);

Not needed if using getArray* methods.

Checks if the given value is an array.

$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\ArrayRule()]);

Uses SizeRule

Checks if value is withing given range:

  • array - count of items equals to given value
  • float, int - equals to given value
  • bool - equals to given value 1 (true) or 0 (false)
  • string - length of string equals to given value
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\BetweenRule(2, 3)]);
$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\BetweenRule(2, 3)]);
$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\BetweenRule(1, 4)]);
$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\BetweenRule(1.0, 1.5)]);

Not needed while using getBool

Checks if the value is a boolean.

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\BooleanRule()]);

Checks if the value is valid e-mail. Using FILTER_VALIDATE_EMAIL

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\EmailRule()]);

Checks if given value is in enum. With default strategy empty string is treated as null (not set).

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\EnumRule(MyEnum::class)]);

Not needed if using getFloat* methods.

Checks if the value is a float.

$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\FloatRule()]);

Checks if the value is valid HEX color (alphanumeric string with 3 to 6 chars without or with #).

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\HexColorRule()]);

Not needed if using getInt* methods.

Checks if the value is a integer.

$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\IntegerRule()]);

Checks if the value is valid IP (using FILTER_VALIDATE_IP)

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\IpRule()]);

Uses SizeRule

Checks if value is equal or greater than given int/float value.

  • array - count of items equals to given value
  • float, int - equals to given value
  • bool - equals to given value 1 (true) or 0 (false)
  • string - length of string equals to given value
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\MaxRule(2)]);
$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\MaxRule(2)]);
$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\MaxRule(1)]);
$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\MaxRule(1.0)]);

Checks if value is equal or lower than given int/float value.

  • array - count of items equals to given value
  • float, int - equals to given value
  • bool - equals to given value 1 (true) or 0 (false)
  • string - length of string equals to given value
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\MinRule(2)]);
$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\MinRule(2)]);
$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\MinRule(1)]);
$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\MinRule(1.0)]);

Not needed if using getInt* methods.

Checks if the value is a numeric.

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\NumericRule()]);

Checks if value is valid against given regex.

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\RegexRule('/[\d]+/')]);

Checks if value is equal to given float/int value:

  • array - count of items equals to given value
  • float, int - equals to given value
  • bool - equals to given value 1 (true) or 0 (false)
  • string - length of string equals to given value
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\SizeRule(2)]);
$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\SizeRule(2)]);
$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\SizeRule(1)]);
$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\SizeRule(1.0)]);

Not needed if using getString* methods.

Checks if value is valid string.

$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\StringRule()]);

Checks if the value is valid URL (using FILTER_VALIDATE_URL)

  • Rules code was extracted from laurynasgadl/php-validator and PHPStan 8 ready. First, I wanted to re-use the existing package but did not find it reusable.
  • I’m thinking of that I will move rules to its own package for more reusability.