Download the PHP package affinity4/validate without Composer

On this page you can find all versions of the php package affinity4/validate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package validate

Affinity4/Validate

Affinity4

Affinity4 Validate is a trait which can be added to any class to enable protected/private property validation.

Once Affinity4 validate is added to a class, any private or protected property can have complex validation assiated by added the @validation docblock tag

Why only private and protected properties?

Affinity4 Validate use the __set magic method to validate each property which has a @validation annotation. This means only private/protected properties can be validated.

Validators

@validation strings have three parts:

type($type:$validator|$validator)

The $type validation will happen first. This will be a straight forward check of the type (e.g. is_int is_string etc)

The $validators are a pipe separated list of validations to happen after the $type validator, which the exception of cast, which is more like a before middleware to attempt to cast the value to the correct type before validation

String

Validates property value is a string. NOTE: Integers will not pass this validation. Castable types (e.g. objects with __toString) will not pass this validation unless 'cast' is passed as a validator. See Cast section below

Value Status
123 Fail
'123' Pass

Int

Validates property value is an integer. NOTE: Numeric strings will not pass this validation. Castable types (e.g. a callable returning an int) will not pass this validation unless 'cast' is passed as a validator. See Cast section below

Value Status
'123' Fail
123 Pass

Numeric

Valdiates a value is numeric

Value Status
"0x539" Fail
"123" Pass

Alpha

NOTE: Strings only
Validates a string is alphabet characters only (no numbers or symbols)

Value Status
"123" Fail
"alpha!" Fail
"abcDEF" Pass

Alnum/Alphanum/Alphanumeric

NOTE: Strings only
Validates a string contains only alphanumeric characters (numbers and letters only)

Value Status
"i-am-not-alnum" Fail
"iAmAlnum123" Pass

snakecase

NOTE: Strings only
Validates a string contains is snake_case

Value Status
"kebab-case" Fail
"snake_case" Pass

kebabcase

NOTE: Strings only
Validates a string is kebab-case

Value Status
"snake_case" Fail
"kebab-case" Pass

constantcase/uppersnakecase

NOTE: Strings only
Validates a string is CONSTANT_CASE (aka UPPER_SNAKE_CASE)

Value Status
"snake_case" Fail
"CONSTANT_CASE" Pass

camelcase

NOTE: Strings only
Validates a string is camelCase

Value Status
"snake_case" Fail
"PascalCase" Fail
"camelCase" Pass

pascalcase/camelcaps/studlycaps/capitalcase

NOTE: Strings only
Validates a string is PascalCase (aka CamelCaps, aka StudlyCaps, aka CapitalCase)

Value Status
"snake_case" Fail
"camelCase" Fail
"PascalCase" Pass

traincase

NOTE: Strings only
Validates a string is Train-Case

Value Status
"COBOL-CASE" Fail
"camelCase" Fail
"Train-Case" Pass

Unsiged

Validates an integer is a positive value, above 0

Value Status
-123 Fail
123 Pass

Cast

NOTE: Strings and Integers only
Will attempt to "cast" an invalid value to the correct type, if possible

This will check if an object has a __toString method, or will attempt to retreive return values of callables as the desired type.

NOTE: Cast happens before any validation occurs. You can think of it more like a before middleware.

Cast to int

type(int:cast)

From To
'123' 123

Cast to string

type(string:cast)

From To
false 'false'
123 '123'

Match

String only
Will match a vlaue based on the regex pattern provided

NOTES

  1. Do not wrap pattern in quotes
  2. Do not use regex delimiters. The default delimiter used internally is /. If you need to change this you should create a custom validation rule using the addValidationRule() method

Replace

Will attempt to replace a matched pattern with a replacement string.

Notes:

  1. Do not qoute pattern or replace strings
  2. Do not use regex delimiters. The default delimiter used internally is /. If you need to change this you should create a custom validation rule using the addValidationRule() method
  3. Uses preg_replace internally
  4. You CANNOT pass an array as the replacement value. Only strings are allowed
  5. You CAN use variable placeholders the same way as in preg_replace e.g. To encrypt a credit card number you would use replace((\d{4})\s(\d{4})\s(\d{4}), **** **** ${3}) // returns: **** **** 1234

ValidationErrors Class

Each group of errors is wrapped in the ValidationErrors class. This is to allow for easier accessing of specific errors and their keys than simply looping over the array of validation errors

For example imaginae a property which has multiple validations, like a password with custom validation added.

  1. Validates the length must be greater than 8 characters
  2. Validates there is at lease one capital letter
  3. Validates there is at least one number

Without the ValidationErrors class the validations would be grouped in an array like so:

You would then have to do a loop, or check how many errors there are to even see if a loop is necessary, and you would still have to use indexes for all scenarios:

With the ValidationErrors class containing the array, we have numerous helpful methods available to travers the array and get exactly what data we want without loops or array keys

You can also easily navigate through the validation errors using the first(), next(), prev() and last() methods. These will expose the array items via properties (in place of the keys)

IMPORTANT: We must store the ValidationError instance in a variable to use first, next, prev and last methods, since getValidationErrors($key) will return a new instance of ValidationErrors each time it is called

TODO

  1. Add type(string:traincase) Train-Case
  2. Add type(string:uppercase)
  3. Add type(string:lowercase)
  4. Add type(string:flatcase)
  5. Add type(string:upperflatcase)
  6. Add type(string:hex) validator. Validates string is a hexadecimal value. ctype_xdigit($value)
  7. Add type(string:no_whitespace) validator. Validates string has no whitespace (e.g. \r\n\t). !ctype_space($value) && ctype_print($value)
  8. Add to(snakecase)
  9. Add to(kebabcase)
  10. Add to(constantcase) aka macrocase/uppersnakecase
  11. Add to(macrocase) aka constantcase/uppersnakecase
  12. Add to(uppersnakecase) aka constantcase/macrocase
  13. Add to(cobolcase) aka upperkebabcase
  14. Add to(upperkebabcase) aka cobolcase
  15. Add to(camelcase)
  16. Add to(camelcaps) aka pascalcase
  17. Add to(pascalcase) aka camelcaps
  18. Add to(uppercase)
  19. Add to(lowercase)
  20. Add to(flatcase)
  21. Add to(upperflatcase)
  22. Allow multiple validations to pass e.g. type(string:any(kebabcase, snakecase)). NOTE: validation "functions" (e.g. regex($pattern)) are not allowed inside any. Custom validators should instead be created using addValidationRule() and the name should be used inside any()
  23. Allow any() to be used to allow multiple valid types e.g. type(any(string,int,null)). NOTE: No additioanl validators can be used in this case e.g. type(any(string,null):cast|kebabcase) since allowing multiple types could complex validation scenarios with potentially unexpected results
  24. Add chaining/fluent interface

All versions of validate with dependencies

PHP Build Version
Package Version
Requires nette/schema Version ^1.2
phpdocumentor/reflection-docblock Version ^5.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package affinity4/validate contains the following files

Loading the files please wait ....