Download the PHP package browserfs/runtime without Composer
On this page you can find all versions of the php package browserfs/runtime. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download browserfs/runtime
More information about browserfs/runtime
Files in browserfs/runtime
Package runtime
Short Description A library for complex data type checking and validating
License MIT
Homepage https://github.com/browserfs/runtime
Informations about the package runtime
Runtime Types
A PHP library used to test at runtime if a value matches a pattern ( or a type ).
Why?
How many times, and how many lines of code you had to write in order to check the type of data and the values in data provided from webservices, user input, forms, or parsed files?
Example 1:
Example 2:
How?
Example 1 could be wrote, in a decoupled manner, as:
defs/Webservices.defs
SomeClassFile.php
In the above example, we tested if the $result variable is validatable by the validator called "WebserviceResponse". If any errors are encountered by the validator, in optional argument $errors, the testing system is storing all the errors occured.
Example 2 could be wrote as follows:
defs/SampleRequest.type
SampleClass.php
Data types and validators
There are built-in data types, and user defined data types. User defined data types are wrote in a definition file ( extension ".types" ). The library creates a environment ( called runtime ), where it store the data types and the validators parsed from the ".types" files you wrote.
After that, the flow is easy, you can test:
- if a value of any type respects a data type you previously defined ( or a built-in type )
- if a value of any type can be validated with the help of a validator you previously defined
Built-in types
This library defines the following primitives built in types:
- int - native php int type
- float - native php float type
- number - any value that is either native php int, either php float
- sint - any decimal string that can be parsed as an integer
- sfloat - any decimal string that can be parsed as a float
- snumber - any decimal string that can be parsed either as an integer, either as a float
- string - native php string type
- boolean - native php boolean type
- any - any type of data
User defined types
Having the built-in types, we can create user-defined types ( complex types ) in a .types file, by respecting the following syntax:
TypeName, OtherTypeName, key_name_1, key_name_2, index_key must be valid identifier names.
Examples:
Validators
After we defined our data types, we need a mechanism to validate the values we store in them. For this case, we implemented Validators.
The syntax of a validator is as follows:
Operators of a validator:
@min -> used to test if a value is >= than the argument
@max -> used to test if a value is <= than the argument
@minlength -> used to test if the length of a value is >= than the argument
@maxlength -> used to test if the length of a value is <= than the argument
@length -> used to test if the length of a value is = with the argument
@is -> used to test if a value is === with the argument
@isnot -> used to test if a value is !== with the argument
@in -> used to test if a value is member of a set of values ( in this case the argument is a set of values )
@nin -> used to test if a value is not member of a set of values ( in this case the argument is a set of values )
@match -> used to test if a value matches against a regular expression
@require -> used to require other validators at current position
@instanceof -> used to test if current object or property is of type of the argument of the operator
@index -> used to specify aditional operators on a validator
@oneof -> used to create variant validators