To implement API in the HLEB2 framework, a set of traits is provided to simplify validation and data processing in controllers (where these traits are applied).
The use of traits in PHP is a matter of various opinions, which is why this module is provided as a separate library, which you may choose to use; there is quite a number of validators available for development in PHP, and this is just a simple working alternative.
Installation of the library github.com/phphleb/api-multitool using Composer:
$composer require phphleb/api-multitool
or download and unpack the library archive into the folder /vendor/phphleb/api-multitool.
First, you need to create a parent class BaseApiActions (or another name) for controllers with API:
All auxiliary traits are collected in BaseApiTrait as a set. Therefore, it is enough to connect it to the controller and get the full implementation. If a different set of these traits is required, then either use them as a group or combine them into your own set.
After this, in all controllers inherited from this class, methods from each trait in the set will appear:
The trait ApiHandlerTrait contains several methods that may be useful for processing returned API data. This does not mean that its methods 'present' and 'error' form the final response, they return named arrays, which can be used in your own more complex standard. An example in the controller method:
In the HLEB framework, when returning an array from a controller, it is automatically converted into JSON. When displaying the formatted array, a value 'error_cells' is added with a list of fields where validation errors occurred (if any).
Intercepts system errors and provides output to the 'error' method of the previous trait ApiHandlersTrait or another designated for this purpose (if the mentioned trait is not used). If a controller method is called, for proper error handling, you need to add the prefix 'action' in the controller, and in the route, leave it without the prefix. For example, for the previous controller example, the routing would be approximately like this:
Here it should be noted that originally the call goes to the controller method 'getOne', and in the controller itself, the method is 'actionGetOne'.
Implements the often necessary function of pagination for displayed data. Adds a method 'getPageInterval', which transforms pagination data into a more convenient format. This calculates the initial value of the selection, which is convenient for working with the database.
Adds a method 'check' that allows checking data in one array against conditions from another. Using this trait provides the ability to verify any incoming data that has been transformed into an array, whether they are POST request parameters or JSON Body. There is a list of possible conditions by which you can verify the data, composed by the developer. For example (Request::input() for the HLEB2 framework returns a JSON Body array):
required - a required field, always located at the beginning.
List of possible types ('type' - must be in the first position or directly after required):
string - checks for the presence of a string value, constraints can be minlength and maxlength.
float - checks for the float(double) type, constraints can be max and min.
int - checks for the int(integer) type, constraints can be max and min.
regex - checks against a regular expression, for example 'regex:[0-9]+'.
fullregex - checks against a full regular expression, similar to 'fullregex:/^[0-9]+$/i', must be enclosed with slashes and can contain the characters : and |, unlike the simpler regex.
bool - checks for a boolean value, only true or false.
null - checks for null as a valid value.
void - checks for an empty string as a valid value.
Type for enumerations:
enum - searches among possible values, for example 'enum:1,2,3,4,south,east,north,west'.
The check for equality is not strict, so both 4 and '4' are correct; for exact matching, it is better to accompany it with a type check.
You can add two or more types, and they will be checked against all common conditions inclusively, for example, 'type:string,null,void|minlen:5' - this means that the string should be checked, at least 5 characters long, or empty, or null value. In all other cases, it returns false as a result of a failed validation check.
You can also check an array of fields with a list of standard array fields (they will be checked according to a unified template):
To check values of nested arrays in the check array, the name is specified in square brackets.
The above condition will return a successful check considering the nesting.
The API traits were tested using github.com/phphleb/api-tests