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

public static array $regexes
  • This property is static.

validate

protected static array $validate
  • This property is static.

required

protected static array $required
  • This property is static.

sanitize

protected static array $sanitize
  • This property is static.

errors

protected static array $errors
  • This property is static.

corrects

protected static array $corrects
  • This property is static.

fields

protected static array $fields
  • This property is static.

Methods

__construct

public __construct(array $validate = [], array $required = [], array $sanitize = []): mixed

Parameters:

Parameter Type Description
$validate array
$required array
$sanitize array

validateItems

Validates an array of items (if needed).

public static validateItems(array $items): bool
  • 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[].

public static items(array $items, string $context = 'save'): array

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.

protected static addError(mixed $field, mixed $type = 'string'): void
  • This method is static. Parameters:
Parameter Type Description
$field mixed
$type mixed

item

Sanitizes an item according to type.

public static item(mixed $item, string $type = 'string', string $context = 'save'): string|null
  • 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.

public static validateItem(mixed $item, string $type): bool
  • 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.

public static key(string $key): string

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.

public static username(string $username, bool $strict = false): string

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

public static removeAccents(string $string, mixed $encoding = 'UTF-8'): array|string|null
  • This method is static. Parameters:
Parameter Type Description
$string string
$encoding mixed