Set of validation rules
- All rules can be combined (all must be successful).
- Except array*
getmethods hasrulesargument. - You can create your own rules by extending
Wrkflow\GetValue\Contracts\RuleContract - You are free to add more rules to this package.
AlphaDashRule
Section titled “AlphaDashRule”Checks if the string contains only digits/alphabet/-/_.
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\AlphaDashRule()]);AlphaNumericRule
Section titled “AlphaNumericRule”Checks if the value is valid IP (using FILTER_VALIDATE_IP)
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\AlphaNumericRule()]);ArrayRule
Section titled “ArrayRule”Not needed if using
getArray*methods.
Checks if the given value is an array.
$getValue->getArray('test', rules: [new \Wrkflow\GetValue\Rules\ArrayRule()]);BetweenRule
Section titled “BetweenRule”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)]);BooleanRule
Section titled “BooleanRule”Not needed while using getBool
Checks if the value is a boolean.
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\BooleanRule()]);EmailRule
Section titled “EmailRule”Checks if the value is valid e-mail. Using FILTER_VALIDATE_EMAIL
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\EmailRule()]);EnumRule
Section titled “EnumRule”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)]);FloatRule
Section titled “FloatRule”Not needed if using
getFloat*methods.
Checks if the value is a float.
$getValue->getFloat('test', rules: [new \Wrkflow\GetValue\Rules\FloatRule()]);HexColorRule
Section titled “HexColorRule”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()]);IntegerRule
Section titled “IntegerRule”Not needed if using
getInt*methods.
Checks if the value is a integer.
$getValue->getInt('test', rules: [new \Wrkflow\GetValue\Rules\IntegerRule()]);IpRule
Section titled “IpRule”Checks if the value is valid IP (using FILTER_VALIDATE_IP)
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\IpRule()]);MaxRule
Section titled “MaxRule”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)]);MinRule
Section titled “MinRule”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)]);NumericRule
Section titled “NumericRule”Not needed if using
getInt*methods.
Checks if the value is a numeric.
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\NumericRule()]);RegexRule
Section titled “RegexRule”Checks if value is valid against given regex.
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\RegexRule('/[\d]+/')]);SizeRule
Section titled “SizeRule”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)]);StringRule
Section titled “StringRule”Not needed if using
getString*methods.
Checks if value is valid string.
$getValue->getString('test', rules: [new \Wrkflow\GetValue\Rules\StringRule()]);UrlRule
Section titled “UrlRule”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.