Documentation
DataFilter helper
Table of Contents ▼
Presentation
Helper used to validate data, to verify that it respects a contract. This can be useful to verify that the input data of an API is as expected.
Contract examples
Pass-through:
null
Scalar types:
'null'
'int'
'string'
'float'
'bool'
Scalar types with a default value:
[
'type' => 'string',
'default' => 'abc',
]
[
'type' => 'bool',
'default' => false,
]
Number (int or float) with a minimum and/or maximum value:
[
'type' => 'int',
'min' => 1,
]
[
'type' => 'float',
'min' => -8.12,
'max' => 8.12,
]
String with a minimum and/or maximum length, or a regular expression mask:
[
'type' => 'string',
'minlen' => 1,
'maxlen' => 12,
]
[
'type' => 'string',
'mask' => '^[Bb][Oo0]..[Oo0].r$',
]
Enum type with an optional default value:
[
'type' => 'enum',
'values' => ['red', 'green', 'blue'],
'default' => 'red',
]
List type with a contract definition used to filter its values as integers:
[
'type' => 'list',
'contract' => 'int',
]
Associative array with the definition of its keys (some of them with a defined type):
[
'type' => 'assoc',
'keys' => [
'id' => 'int',
'name',
'dateCreation',
]
]
List type with a contract definition used to defined its values as associative arrays:
[
'type' => 'list',
'contract' => [
'type' => 'assoc',
'keys' => ['id', 'name'],
]
]
Associative array with keys definition (all of them with a type, one is not mandatory):
[
'type' => 'assoc',
'keys' => [
'id' => int,
'name' => [
'type' => 'string',
'mandatory' => false,
]
]
]
Complex example:
[
'type' => 'assoc',
'keys' => [
'id' => 'int',
'isCreated' => 'bool',
'name' => [
'type' => 'string',
'default' => 'abc',
],
'color' => [
'type' => 'enum',
'values' => ['red', 'green', 'blue'],
'default' => 'red',
'mandatory' => false,
],
'creator' => [
'type' => 'assoc',
'keys' => [
'id' => 'int',
'name',
'dateCreation',
],
],
'children' => [
'type' => 'list',
'mandatory' => false,
'contract' => [
'type' => 'assoc',
'keys' => [
'id' => 'int',
'name',
]
],
],
'identifiers' => [
'type' => 'list',
'contract' => 'int',
],
],
]
Usage
The \Temma\Utils\DataFilter object offers a static method process(). This method takes two parameters, the data to filter and the contract to use, and returns the filtered data. If the contract's syntax is not correct, the method raises a \Temma\Exceptions\IO exception. If the data doesn't validate the contract, the method raises a \Temma\Exceptions\Application exception.
Example:
use \Temma\Utils\Datafilter as TµDataFilter;
use \Temma\Exceptions\IO as TµIOException;
use \Temma\Exceptions\Application AS TµApplicationException;
use \Temma\Base\Log as TµLog;
$contract = [
'type' => 'enum',
'values' => ['admin', 'member', 'guest'],
];
try {
$data = TµDatafilter($data, $contract);
} catch (TµIOException $ie) {
TµLog::log('myapp', 'WARN', "Invalid contract.");
throw $ie;
} catch (TµApplicationException $ae) {
TµLog::log('myapp', 'WARN', "Invalid data.");
throw $ae;
}
Previous: | BaseConvert helper |
Next: | Email helper |
Table of Contents
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- External libraries : How to use external function libraries
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependency injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Data sources : Unified data access management
- Model : How to use DAOs to access databases
- Execution flow : How to manage the execution flow, between plugins and the controller
- Plugins : How to use plugins, and create your own plugins to modularize your code
- Attributes : How to filter access to controllers and actions
- Command-line interface : To create executable scripts on the command line, automatically initialized by Temma
-
Helpers :
Items offered by Temma to help you in various circumstances
- Command-line scripts
-
Controller + plugin
- Auth : Controller and plugin to manage user authentication
- Plugins
- Attributes
-
Utility objects
- ANSI : To format texts written to standard output
- BaseConvert : To do digital base conversions
- DataFilter : To filter and validate data
- Email : To send emails
- HTMLCleaner : To clean up an HTML stream coming from a WYSIWYG editor
- IniExport : To export data in INI format
- Json : To read JSON streams that may contain comments
- Lock : To lock access to a file, or the execution of the current PHP script
- Registry : To properly store global variables
- Smarty : To process Smarty templates outside the view
- Term : Terminal management (TTY)
- Text : Different treatments on character strings
- Timer : To manage stopwatches