Download the PHP package amsify42/php-typestruct without Composer
On this page you can find all versions of the php package amsify42/php-typestruct. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download amsify42/php-typestruct
More information about amsify42/php-typestruct
Files in amsify42/php-typestruct
Package php-typestruct
Short Description PHP package for validating data against the structure defined
License MIT
Informations about the package php-typestruct
PHP TypeStruct
PHP package for validating data against the structure defined.
Installation
Table of Contents
- Introduction
- Validation
- Data
- Class Validation
- Rules
- Custom Rules
- Complex Example
1. Introduction
The purpose of this php package is to make validation easy and the structure defined for validation should be readable. The data passed can be validated against the structure defined.
2. Validation
Let's say we have data in array format.
and we want this to be strictly validated. Now we can define the structure against which this data to be validated.
Notice that the structure we defined does not completely look like a PHP syntax but it will work as a structure against data.
Note we are creating new instance of Amsify42\TypeStruct\TypeStruct
, passing the full class name of typestruct App\TypeStruct\Simple
and pasing data to validate() method.
The validate method will return with the info whether the data passed is validated against the structure and it will return
The is_validated
will have true
or false
based on whether data is validated or not and messages
will have error messages in hierarchy based on elements which are not validated.
Helper method
We can also use helper method to get the Amsify42\TypeStruct\TypeStruct
new instance.
Autoloading
Autoloading of the typestruct file will be done automatically if its name and path is based on psr-4 standards else you need to use setPath()
method with typestruct instance which expects direct path of the typestruct file.
Options
With Typestruct instance we can set these options before calling validate()
method
3. Data
The data you can pass for validation are
As we have already seen the array example, lets see the examples for the rest
Object(stdClass)
Note: We are passing true
to method isDataObject()
to tell TypeStruct that the data we are passing is of type Object(stdClass).
Json
XML
Note: We are calling contentType() method to set its type for both Json
and XML
.
4. Class Validation
We can do the validation by creating class and extending it to the Amsify42\TypeStruct\Validator
Since we already set TypeStruct
class name and data
in protected properties. We can create instance of this class directly and validate
You can also set the data before validating against the typestruct like this
and we can also use these protected properties which extends Amsify42\TypeStruct\Validator
5. Rules
Basic
These are the basic types we can use for elements. It will check whether key exist and its type.
numeric
work just like php is_numeric()
method which allows numbers even in quotes. tinyInt
expects the value to be either 0
or 1
and type any
means that element value could be of any type.
Optional
To make the element optional, we simply prefix it with question mark ?
Optional can also be applied to child dictionary
Length
We can also set the limits to the length of these types like this
Array
These are the array types we can use
External as Child
We can also use the other external TypeStruct file as a element
Now we can use Category
as type like this
or as array of this type
More Rules
You can also attach more rules to the input like this
As you can see, we have added rule email
to the email element which will check for valid email address. You can add more rules to the element separated by dot .
like this
These are the pre defined rules you can use
6. Custom Rules
We can also write method to perform cutom validation but this can only be achieved when we create class and extends it to Amsify42\TypeStruct\Validator
Now we can write method checkName
in our validator class like this
We can use $this->name()
to get the name of current element and $this->value()
to get the value of the current element which is applicable to the rule. To get the other element value, we already have $this->data
accessible from these custom rule methods.
If you want to access data from custom method more easily, you can also use the method $this->path()
which will directly get the element from multi level path.
Note: $this->path
expects parameters to be key name separated by dot(if multiple keys) and will either return NULL
(if key does not exist) or the target key value.
7. Complex Example
The above complex and multi level typestruct example file will be validated with the data: