Sanitizes different types of data.
Examples:
$validations = [ 'name' => 'anything', 'email' => 'email', 'alias' => 'anything', 'pass' => 'anything', 'phone' => 'phone', 'birthdate' => 'date' ];
$required = ['name', 'email', 'alias', 'pass'];
$sanitize = ['alias'];
$validate = new Sanitizer($validations, $required, $sanitize); if($validate->validateItems($_POST)) { $content = $validate->items($_POST); // now do what you need, $_POST has been sanitized. }
Validate one item: $validate = new Sanitizer()->validateItem('email@gmail.com', 'email');
Sanitize one item: $sanitize = new Sanitizer()->item('word', 'string');
- Full name:
\App\Shared\Services\Sanitizer - This class is marked as final and can't be subclassed
- This class is a Final class
Properties
regexes
- This property is static.
validate
- This property is static.
required
- This property is static.
sanitize
- This property is static.
errors
- This property is static.
corrects
- This property is static.
fields
- This property is static.
Methods
__construct
Parameters:
| Parameter | Type | Description |
|---|---|---|
$validate |
array | |
$required |
array | |
$sanitize |
array |
validateItems
Validates an array of items (if needed).
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$items |
array | Items to validate. |
Return Value:
True if valid, false otherwise.
items
Sanitizes an array of items according to the self::$sanitize[].
Sanitize will be standard of type string, but can also be specified. For ease of use, this syntax is accepted:
$sanitize = ['fieldname', 'otherfieldname' => 'float'];
$this->items($sanitize);
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$items |
array | Items to sanitize. |
$context |
string | The context for which the string is being sanitized. |
Return Value:
Sanitized items.
Throws:
addError
Adds an error to the errors array.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$field |
mixed | |
$type |
mixed |
item
Sanitizes an item according to type.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$item |
mixed | Item to sanitize. |
$type |
string | Item type (i.e. string, float, int, etc.). |
$context |
string | The context for which the string is being sanitized. |
Return Value:
Sanitized string or null if item is empty.
Throws:
validateItem
Validates a single item according to $type.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$item |
mixed | Item to validate. |
$type |
string | Item type (i.e. string, float, int, etc.). |
Return Value:
True if valid, false otherwise.
key
Sanitizes a string key.
Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
Uses sanitize_key filter hook.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$key |
string | String key |
Return Value:
Sanitized key
Throws:
username
Sanitizes a username, stripping out unsafe characters.
Removes tags, octets, entities, and if strict is enabled, will only keep
alphanumeric, _, space, ., -, @. After sanitizing, it passes the username,
raw username (the username in the parameter), and the value of $strict as
parameters for the sanitize_user filter.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$username |
string | The username to be sanitized. |
$strict |
bool | If set, limits $username to specific characters. Default false. |
Return Value:
The sanitized username, after passing through filters.
Throws:
removeAccents
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$string |
string | |
$encoding |
mixed |