Download the PHP package mcustiel/php-simple-request without Composer
On this page you can find all versions of the php package mcustiel/php-simple-request. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mcustiel/php-simple-request
More information about mcustiel/php-simple-request
Files in mcustiel/php-simple-request
Package php-simple-request
Short Description php-simple-request is a minimalist library designed to simplify requests validation and filtering
License GPL-3.0+
Informations about the package php-simple-request
php-simple-request
php-simple-request is a library designed to simplify requests validation and filtering, that generates an object representation from the request data.
The idea behind this library is to design objects that represent the requests that the application receives, and use php-simple-request to map the request data to those objects and sanitize the received data. To do this, the library provides a set of annotations to specify the filters and validations to execute over the request data.
This library is optimized to be performant by using a cache system to save the parser classes generated by reading the annotations.
Table of contents
- Installation
- Composer
- How to use it?
- Define objects to represent the requests
- Parse the request and get an object representation
- Sub-objets
- Caching
- Filters
- Capitalize
- CustomFilter
- DefaultValue
- Lowercase
- RegexReplace
- StringReplace
- ToFloat
- ToInteger
- Trim
- Uppercase
- Validators
- AllOf
- Alpha
- AlphaNumeric
- AnyOf
- CustomValidator
- DateTime
- DateTimeFormat
- Definition
- Enum
- ExclusiveMaximum
- ExclusiveMinimum
- Hexa
- HostName
- IPV4
- IPV6
- Items
- MacAddress
- Maximum
- MaxItems
- MaxLength
- MaxProperties
- Minimum
- MinItems
- MinLength
- MinProperties
- MultipleOf
- Not
- NotEmpty
- NotNull
- OneOf
- Pattern
- Properties
- RegExp
- Required
- TwitterAccount
- Type
- TypeFloat
- TypeInteger
- UniqueItems
- Uri
Installation
Composer:
This project is published in packagist, so you just need to add it as a dependency in your composer.json:
If you want to access directly to this repo, adding this to your composer.json should be enough:
Or just download the release and include it in your path.
How to use it?
Define objects to represent the requests
First of all, you have to define the classes that represent the requests you expect the application to receive. The setter methods of this class will be used by php-simple-request to write the values obtained from the request into the object.
Then, you can specify the filters you want to apply on each field:
And also the validations you want to run for each property value:
Note: php-simple-request executes the filters first and then it executes the validations.
Parse the request and get an object representation
To parse the request and convert it to your object representation, just receive the request using the RequestBuilder object (the field names in the request must have the same name to the fields in the class you defined). You must call the parseRequest method with an array or an object of type \stdClass. See an example:
If your request is received as a subarray of POST, just specify the key:
Also it can be used for some REST json request:
The previous behaviour throws an exception when it finds an error in the validation.
There is an alternative behaviour in which you can obtain a list of validation errors, one for each invalid field. To activate this alternative behavior, you have to specify it in the call to RequestBuilder::parseRequest
like this:
Note: InvalidRequestException::getErrors() is available for default behaviour too, it returns an array with only one error.
Sub-objects
php-simple-request also allows you to specify the class to which parse a property's value using the annotation ParseAs. It's better to see it in an example:
Let's say we want to create a Couple class that contains two objects of type Person, which represent the members of the couple. To specify that the properties must be parsed as Persons we use ParseAs.
php-simple-request will automatically convert the value received in the fields person1 and person2 from the request into the type PersonRequest.
Note: If a property has the ParseAs annotation and also validations and filters, php-simple-request will first execute parseAs and then filters and validations as usual.
Array of objects:
If you plan to parse not an object, but an array of objects, you can tell php-simple-request to do this using the array notation by putting the class name as the first element of an array:
Caching:
As the request class definition uses annotations to specify filters and validators, it generates a lot of overhead when parsing all those annotations and using reflection. To avoid this overhead, php-simple-request supports the use of PSR-6 Cache. Just pass the implementation as the first argument to create the RequestBuilder
object:
You can pass a NullObject implementation for testing purposes.
Filters
Capitalize
This filter sets all the string characters to lowercase but its first character, which is converted to uppercase. This annotation accepts a boolean specifier value to define wether to capitalize just the first letter of the first word or the first letter of all words in the string.
CustomFilter
This is a special filter annotation that allows you to specify your own filter class and use it to filter the value in the field. It accepts two parameters:
- value: which is the specifier.
- class: which is your custom filter class (it must implement Mcustiel\SimpleRequest\Interfaces\FilterInterface
DefaultValue
Sets a default value when the received value is null or an empty string.
LowerCase
LowerCase filter converts all characters in the given string to lowercase.
RegexReplace
Executes a replace in the string, using a regular expression pattern as a search. It accepts two parameters:
- pattern: The regular expression to search for.
- replacement: The replacement text for the matches.
StringReplace
Executes a replace in the string, using a string pattern as a search. It accepts two parameters:
- pattern: The string to search for.
- replacement: The replacement text for the matches.
NOTE: This method uses str_replace internally, you can take advantage of it by setting pattern to array, etc. See str_replace specification.
ToFloat
This filters just forces a cast to float of the received value.
ToInteger
Analog to Float, forces a cast to integer to the received value.
Trim
Trims the string from both ends.
UpperCase
Converts all characters in the given string to uppercase.
Validators
The validators marked with an *()** behave similar to json-schema defined validators. Please see: JSON Schema definition and understanding JSON Schema.
AllOf*
This validator receives a list of validators as parameter and checks that all of them matches the value. You can obtain the same behavior just by adding multiple Valitor annotations for that property.
Example:
Alpha
This validator checks that all characters in a string are in the range A-Z or a-z.
Example:
AlphaNumeric
This validator checks that all characters in a string are in the range A-Z or a-z or 0-9.
Example:
AnyOf*
This validator receives a list of validators as parameter and checks if at least one of them validates a given value.
Example:
CustomValidator
This is a special validator annotation that allows you to specify your own validator class and use it to validate the value in the field. It accepts two parameters:
- value: which is the specifier.
- class: which is your custom filter class (it must implement Mcustiel\SimpleRequest\Interfaces\ValidatorInterface
Example:
DateTime*
This validator checks that the given string is a date and its format is compatible with \DateTime::RFC3339 (Y-m-d\TH:i:sP)
Example:
Default specifier value: \DateTime::ISO8601
DateTimeFormat
This validator checks that the given string is a date and its format is compatible with the specified date format. The format to specify as the annotation value must be compatible with the php method \DateTime::createFromFormat.
Example:
Default specifier value: \DateTime::ISO8601
Definition*
This validator is an alias for CustomValidator.
This validator checks that the given value is a string containing an email. This annotation expects no value specifier.
Example:
Enum*
This validator checks that the given value is in a specified collection of values.
Example:
ExclusiveMaximum*
This validator checks that the given value is lower than the specified one.
Example:
ExclusiveMinimum*
This validator checks that the given value is greater than the specified one.
Example:
Hexa
This validator checks that the given value is a string containing only hexadecimal characters (ranges 0-9, a-f, A-F).
Example:
Hostname
Checks if the value is a hostname.
Example:
TypeFloat
This validator checks that the given value is a float. A boolean modifier can be specified in this annotation, indicating if the value must be a strict float or if integers can be validated as floats too.
Example:
Default specifier value: false, indicating that integers are validated as floats
TypeInteger
This validator checks that the given value is numeric and it's an integer. It expects a boolean modifier, strict, that indicates if integers in float format like 1.0, 2.0, etc. should be validated as integers. Default strict value: true, meaning that no float format accepted.
Examples:
IPV4
This validator checks that the given value is a valid IPv4. It does not expect any modifier.
Example:
IPV6
This validator checks that the given value is a valid IPv6. It does not expect any modifier.
Example:
Items*
This validator checks that each element of a given array matches a specified set of validators in its corresponding index. It expects two parameters: items and additionalItems. Items contains the validations for the array, it can be a validator (in which case every element must match it) or an array of validators (in which case each element must match the validator in the same position); its default value is an empty array. AdditionalItems can be a boolean or a validator; if it's a boolean true, items without a validator in the same position will not be checked, if it false it will not accept values without validator in the same position and if it's a validator, all items without a validator in the same position must match it. For a detailed and good description please see the aforementioned documents about JSON Schema. Default specifier values:
- items = []
- additionalItems = true
MacAddress*
Validates that the value is a string specifying a MAC address.
Example:
Maximum*
This validator checks that the given value is lower than or equal to the specified one.
Example:
MaxItems*
This validator checks that the field's is an array and it has an amount of items equal to or less than the specification. The specification value must be an integer greater than 0. The field must be an array.
Example:
MaxLength*
This validator checks that the field's length is equal to or less than the specification. The specification value must be an integer. The field must be a string.
Example:
Default specifier value: 255
MaxProperties*
This annotation validates that a given array or stdClass contains, at most, the specified number of items or properties. It's analog to MaxItems.
Example:
Minimum*
This validator checks that the given value is greater than or equal to the specified one.
Example:
MinItems*
This validator checks that the field's is an array and it has an amount of items equal to or greater than the specification. The specification value must be an integer greater than 0. The field must be an array.
Example:
MinLength*
This validator checks that the field's length is equal to or greater than the specification. The specification value must be an integer. The field can be a string or an array.
Example:
Default specifier value: 0
MinProperties*
This validator checks that the field's is an array or a stdClass and it has an amount of items equal to or greater than the specification. The specification value must be an integer greater than 0.
Example:
MultipleOf*
Validates that the value received is multiple of the specified number.
Example:
Not*
Checks that the value is not valid against a specified validator.
Example:
NotEmpty
This validator checks that the field's is not empty. Internally, this validator uses php's empty so the functionality is exactly the same. It does not expect any modifier.
Example:
Default specifier value: 0
NotNull
This validator checks that the field's is not null, it can be used to check if the field is present in the request also. Use this function only if you want to check the value is present and you accept empty values in the field; if you will not accept empty values, just use NotEmpty validator which also checks values is not null. It does not expect any modifier.
Example:
OneOf*
This validator receives a list of validators as parameter and checks that exactly one of them validates against a given value.
Example:
Pattern*
Alias for RegExp validator.
Properties*
Analog to items, but works with objects of type stdClass or associative arrays. It runs a list of validators through the properties of a stdclass or the items in an associative array, using the properties names or patterns. Please see the aforementioned documents about json-schema.
Default specifier values:
- properties = []
- patternProperties = []
- additionalProperties = true
Example:
Note: This annotation was added as a json-schema-like annotation. Please have in mind that it could be better to use sub-object creation through ParseAs annotation in most of the cases.
RegExp
This validator checks the field against a given regular expression.
Example:
Required*
This validator checks that an object of type stdClass or an associative array contains the list of keys specified.
Example:
TwitterAccount
This validator checks that the field contains a twitter account.
Example:
Type*
Validates that the received value is of the type specified. The specified type must be one of the types defined by json-schema: 'array', 'object', 'integer', 'number', 'string', 'boolean', 'null'.
Example:
UniqueItems*
Validates that the received value is an array containing all unique values.
Example:
Uri*
This validator checks that the field contains a valid URL.