PHPackages                             traderinteractive/filter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. traderinteractive/filter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

traderinteractive/filter
========================

A filtering implementation for verifying correct data and performing typical modifications to data

v4.4.0(1y ago)5100.3k↓31.1%15[2 issues](https://github.com/traderinteractive/filter-php/issues)MITPHPPHP ^7.3 || ^8.0CI passing

Since Oct 29Pushed 1y ago22 watchersCompare

[ Source](https://github.com/traderinteractive/filter-php)[ Packagist](https://packagist.org/packages/traderinteractive/filter)[ RSS](/packages/traderinteractive-filter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (42)Used By (0)

filter-php
==========

[](#filter-php)

A filtering implementation for verifying correct data and performing typical modifications to data.

[![Build Status](https://camo.githubusercontent.com/7adbc579606666c3e0c219b57319790983468a0dcc22070828b2302266412e8a/68747470733a2f2f7472617669732d63692e6f72672f747261646572696e7465726163746976652f66696c7465722d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/traderinteractive/filter-php)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/a2ed6f4a3512907d7dd15efdb90f2219289ab31cd67de995ac2cfa5b94fcffb0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f747261646572696e7465726163746976652f66696c7465722d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/traderinteractive/filter-php/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/ba730407d04f4a9faba56b50c2716eda400ad81067a26e15182db69cc7689045/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f747261646572696e7465726163746976652f66696c7465722d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/traderinteractive/filter-php?branch=master)

[![Latest Stable Version](https://camo.githubusercontent.com/26375bfb2fc924502661c4f58624c6fc1dcfdb6d545593355ff0eaf26eb0ac10/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f762f737461626c65)](https://packagist.org/packages/traderinteractive/filter)[![Latest Unstable Version](https://camo.githubusercontent.com/994a290271c96a1c61501832dad6519ae0b341fd348bc35dd531a135127646fb/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f762f756e737461626c65)](https://packagist.org/packages/traderinteractive/filter)[![License](https://camo.githubusercontent.com/875fbc97ce30f217a92faa8e226746fdb77a67f7b8e04d0e037ee9190f34a9bc/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f6c6963656e7365)](https://packagist.org/packages/traderinteractive/filter)

[![Total Downloads](https://camo.githubusercontent.com/8166bdb9459418f1e13b7300e53bcd0ee6dd1db1885099863771a606bdfb3259/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f646f776e6c6f616473)](https://packagist.org/packages/traderinteractive/filter)[![Daily Downloads](https://camo.githubusercontent.com/a0d9332a7c1671788e0470b2f3276859d8f3dbae2bf2c15d5978f1c3ab61d973/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f642f6461696c79)](https://packagist.org/packages/traderinteractive/filter)[![Monthly Downloads](https://camo.githubusercontent.com/8d22703b6a4a791a932ce528b0159907207b2168854db22b76ca411ef101aa45/68747470733a2f2f706f7365722e707567782e6f72672f747261646572696e7465726163746976652f66696c7465722f642f6d6f6e74686c79)](https://packagist.org/packages/traderinteractive/filter)

Features
--------

[](#features)

- Compact, readable specification
- Filter with any php callable such as
- Anonymous function
- Class function
- Built-in function
- Optional/Required support, field and global level
- Default support
- Chaining filters
- Optionally returns unknown fields
- Filter alias support

Components
----------

[](#components)

This package is a partial metapackage aggregating the following components:

- [traderinteractive/filter-arrays](https://github.com/traderinteractive/filter-arrays-php)
- [traderinteractive/filter-bools](https://github.com/traderinteractive/filter-bools-php)
- [traderinteractive/filter-dates](https://github.com/traderinteractive/filter-dates-php)
- [traderinteractive/filter-floats](https://github.com/traderinteractive/filter-floats-php)
- [traderinteractive/filter-ints](https://github.com/traderinteractive/filter-ints-php)
- [traderinteractive/filter-strings](https://github.com/traderinteractive/filter-strings-php)

Example
-------

[](#example)

```
class AppendFilter
{
    public function filter($value, $extraArg)
    {
        return $value . $extraArg;
    }
}
$appendFilter = new AppendFilter();

$trimFunc = function($val) { return trim($val); };

list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter(
    [
        'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']],
        'field two' => ['required' => true, ['floatval']],
        'field three' => ['required' => false, ['float']],
        'field four' => ['required' => true, 'default' => 1, ['uint']],
    ],
    ['field one' => ' abcd', 'field two' => '3.14']
);

var_dump($status);
var_dump($result);
var_dump($error);
var_dump($unknowns);
```

prints

```
bool(true)
array(3) {
  'field one' =>
  string(6) "abcboo"
  'field two' =>
  double(3.14)
  'field four' =>
  int(1)
}
NULL
array(0) {
}
```

Composer
--------

[](#composer)

To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on `traderinteractive/filter` to your project's `composer.json` file such as:

```
composer require traderinteractive/filter
```

Documentation
-------------

[](#documentation)

Found in the [source](src/Filterer.php) itself, take a look!

### Filterer

[](#filterer)

At the core of this library is a `Filterer` class that can validate the structure of an array and map the data through filters. This behavior is defined by a specification of the different filters to apply and some additional options.

#### Specification

[](#specification)

The specification is an array of key =&gt; filter specification pairs.

The keys define the known fields in the array. Any fields in the array that are not in the specification are treated as "unknown" fields and may cause validation to fail, depending on the value of the `allowUnknowns` option.

The filter specification for a single field is also an array. It can contain predefined [filter options](#filter-options).

The rest of the specification for the field are the filters to apply.

The first element in the filter is the filter to run. This can either be something that passes `is_callable` (e.g., `'trim'` or `[$object, 'method']`) or it can be one of our predefined aliases (e.g., `'float'`).

The rest of the elements in the filter are the extra arguments to the filter (the value being filtered is always the first argument).

A filter specification can contain any number of filters and the result of each filter is piped in as the input to the next filter. The result of the final filter is set in the result array.

The example above should help clarify all this.

Filterer Options
================

[](#filterer-options)

allowUnknowns
-------------

[](#allowunknowns)

#### Summary

[](#summary)

Flag to allow elements in unfiltered input that are not present in the filterer specification.

#### Types

[](#types)

- bool

#### Default

[](#default)

The default value is `false`

#### Constant

[](#constant)

```
TraderInteractive\FiltererOptions::ALLOW_UNKNOWNS
```

#### Example

[](#example-1)

```
$options = [
    TraderInteractive\FiltererOptions::ALLOW_UNKNOWNS => true,
];

$filterer = new TraderInteractive\Filterer($specification, $options);
```

defaultRequired
---------------

[](#defaultrequired)

#### Summary

[](#summary-1)

Flag for the default required behavior of all elements in the filterer specification. If `true` all elements in the specification are required unless they have the required filter option set.

#### Types

[](#types-1)

- bool

#### Default

[](#default-1)

The default value is `false`

#### Constant

[](#constant-1)

```
TraderInteractive\FiltererOptions::DEFAULT_REQUIRED
```

#### Example

[](#example-2)

```
$options = [
    TraderInteractive\FiltererOptions::DEFAULT_REQUIRED => true,
];

$filterer = new TraderInteractive\Filterer($specification, $options);
```

responseType
------------

[](#responsetype)

#### Summary

[](#summary-2)

Specifies the type of response which The Filterer::filter method will return. It can be `array` or `\TraderInteractive\FilterResponse`

#### Types

[](#types-2)

- string

#### Default

[](#default-2)

The default value is `array`

#### Constant

[](#constant-2)

```
TraderInteractive\FiltererOptions::RESPONSE_TYPE
```

#### Example

[](#example-3)

```
$options = [
    TraderInteractive\FiltererOptions::RESPONSE_TYPE => \TraderInteractive\FilterResponse::class,
];

$filterer = new TraderInteractive\Filterer($specification, $options);
```

Filter Options
==============

[](#filter-options)

required
--------

[](#required)

#### Summary

[](#summary-3)

Defines whether this field is a required element of the array. This value overrides the global filter specification's `defaultRequired` option.

#### Types

[](#types-3)

- bool

#### Default

[](#default-3)

The default value depends on the `defaultRequired` Filterer Option.

#### Constant

[](#constant-3)

```
TraderInteractive\FilterOptions::IS_REQUIRED
```

#### Example

[](#example-4)

```
$specificaton = [
    'id' => [TraderInteractive\FilterOptions::IS_REQUIRED => true, ['uint']],
];
```

default
-------

[](#default-4)

#### Summary

[](#summary-4)

Defines what the default value of this field is if none is given. A field with a default value will be guaranteed to be in the result. The `required` value does not affect `default` behavior.

#### Types

[](#types-4)

- string

#### Default

[](#default-5)

There is no default value for this option.

#### Constant

[](#constant-4)

```
TraderInteractive\FilterOptions::DEFAULT_VALUE
```

#### Example

[](#example-5)

```
$specificaton = [
    'subscribe' => [TraderInteractive\FilterOptions::DEFAULT_VALUE => true, ['bool']],
    'status' => [TraderInteractive\FilterOptions::DEFAULT_VALUE => 'A', ['string', false, 1, 1]],
];
```

error
-----

[](#error)

#### Summary

[](#summary-5)

Defines a custom error message to be returned if the value fails filtering. Within the error string, `{value}` can be used as a placeholder for the value that failed filtering.

#### Types

[](#types-5)

- string

#### Default

[](#default-6)

There is no default value for this option.

#### Constant

[](#constant-5)

```
TraderInteractive\FilterOptions::CUSTOM_ERROR
```

#### Example

[](#example-6)

```
$specificaton = [
    'price' => [
        TraderInteractive\FilterOptions::CUSTOM_ERROR => 'Price {value} was not between 0 and 100',
        ['uint', false, 0, 100],
    ],
];
```

conflictsWith
-------------

[](#conflictswith)

#### Summary

[](#summary-6)

Defines any input fields with which a given field will conflict. Used when one field can be given in input or another but not both.

#### Types

[](#types-6)

- string

#### Default

[](#default-7)

There is no default value for this option.

#### Constant

[](#constant-6)

```
TraderInteractive\FilterOptions::CONFLICTS_WITH
```

#### Example

[](#example-7)

```
$specification = [
    'id' => [
        TraderInteractive\FilterOptions::CONFLICTS_WITH => 'code',
        [['uint']],
    ],
    'code' => [
        TraderInteractive\FilterOptions::CONFLICTS_WITH => 'id',
        [['string']],
    ],
];
```

uses
----

[](#uses)

#### Summary

[](#summary-7)

Specifies an array of input values that should be used as part of a field's filter specification.

#### Types

[](#types-7)

- string\[\]

#### Default

[](#default-8)

The default value for this option is an empty array.

#### Constant

[](#constant-7)

```
TraderInteractive\FilterOptions::USES
```

#### Example

[](#example-8)

```
$specification = [
    'base' => [
        [['float']],
    ],
    'exponent' => [
        ['uint'],
        [
            TraderInteractive\FilterOptions::USES => 'base',
            'pow',
        ],
    ],
];
```

The exponent filter spec will call the PHP function `pow()` with the value provided and the result of the filtered `base`

throwOnError
------------

[](#throwonerror)

#### Summary

[](#summary-8)

If true the Filterer will throw any exception caught when filtering a value instead of returning the error in the filter response.

#### Types

[](#types-8)

- boolean

#### Default

[](#default-9)

The default value for this option is `false`

#### Constant

[](#constant-8)

```
TraderInteractive\FilterOptions::THROW_ON_ERROR
```

#### Example

[](#example-9)

```
$idFilter = function ($id) : int {
    if (!is_int($id)) {
       throw new NotFoundException("id '{$id}' was not found");
    }

    return $id;
};
$specification = [
    'id' => [
        \TraderInteractive\FilterOptions::THROW_ON_ERROR => true,
        [$idFilter],
    ],
];
```

If the `id` value given in the input is not an integer the Filterer::execute() will throw the `NotFoundException`

returnOnNull
------------

[](#returnonnull)

#### Summary

[](#summary-9)

Flag to break the filter chain if a resulting value is `null` Useful for nullable fields which require additional filtering if the value is not null.

#### Types

[](#types-9)

- boolean

#### Default

[](#default-10)

The default value for this option is `false`

#### Constant

[](#constant-9)

```
TraderInteractive\FilterOptions::RETURN_ON_NULL
```

#### Example

[](#example-10)

```
$validCodes = ['A', 'I', 'X'];
$specification = [
    'code' => [
        \TraderInteractive\FilterOptions::RETURN_ON_NULL => true,
        ['string', true],
        ['strtoupper'],
        ['in', $validCodes],
    ],
];
```

If the `code` value is `null` then the resulting filtered value will be null. Otherwise the value must be one of the `$validCode` values.

### Included Filters

[](#included-filters)

Of course, any function can potentially be used as a filter, but we include some useful filters with aliases for common circumstances.

#### Filterer::ofScalars

[](#filtererofscalars)

Aliased in the filterer as `ofScalars`, this filter verifies that the argument is an array (possibly empty) of scalar items that each pass the given filters (given in the same format as used by `Filterer::filter`.

The following checks that `$value` is an array of unsigned integers.

```
$value = \TraderInteractive\Filter\Filterer::ofScalars($value, [['uint']]);
```

#### Filterer::ofArrays

[](#filtererofarrays)

Aliased in the filterer as `ofArrays`, this filter verifies that the argument is an array (possibly empty) of arrays that each pass the given filters (given in the same format as used by `Filterer::filter`.

The following checks that `$value` is an array of items that each have an `id` key with a numeric value. No other keys would be allowed. For example, the following is valid input: `[['id' => '1'], ['id' => '2']]`.

```
$value = \TraderInteractive\Filter\Filterer::ofArrays($value, ['id' => [['uint']]]);
```

#### Filterer::ofArray

[](#filtererofarray)

Aliased in the filterer as `ofArray`, this filter verifies that the argument is an array that passes the given specification. This is essentially a flipped version of `Filterer::filter` that allows for testing nested associative arrays.

#### Arrays::copy

[](#arrayscopy)

Aliased in the filterer as `array-copy`, this filter copies values from the source array into a destination array using the provided destination key map.

Example Usage:

```
$specification = ['field' => [['array-copy', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
$filterer = new TraderInteractive\Filterer($specification);
$input = ['foo' => 123, 'bar' => 'abc'];
$result = $filterer->execute($input);
assert(['field' => ['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc']], $result->filteredValue);
```

#### Arrays::copyEach

[](#arrayscopyeach)

Aliased in the filterer as `array-copy-each`, this filter copies values from each array in the source array into a destination array using the provided destination key map.

Example Usage:

```
$specification = ['field' => [['array-copy-each', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
$filterer = new TraderInteractive\Filterer($specification);
$input = [
    ['foo' => 123, 'bar' => 'abc'],
    ['foo' => 456, 'bar' => 'def'],
];
$result = $filterer->execute($input);
assert(['field' => [['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc'], ['FOO_VALUE' => 456, 'BAR_VALUE' => 'def']]], $result->filteredValue);
```

#### Arrays::implode

[](#arraysimplode)

Aliased in the filterer as `implode`, this filter is a wrapper around `implode` to ensure proper argument order.

```
$sepcification = ['field' => [['array'],['implode', ',']]];
$filterer = new TraderInteractive\Filterer($specification);
$input = [
    'field' => ['lastname', 'email', 'phone'],
];
$result = $filterer->execute($input);
assert(['field' => 'lastname,email,phone'], $result->filteredValue);
```

#### Arrays::in

[](#arraysin)

Aliased in the filterer as `in`, this filter is a wrapper around `in_array` including support for strict equality testing.

The following does a strict check for `$value` against the 3 accepted values.

```
\TraderInteractive\Filter\Arrays::in($value, ['a', 'b', 'c']);
```

#### Arrays::arrayize

[](#arraysarrayize)

Aliased in the filterer as `arrayize`, this filter returns this original input if it is an array, otherwise returns input wrapped in an array. If the original input is null, an empty array is returned.

```
$value = \TraderInteractive\Filter\Arrays::arrayize('a string value');
assert($value === ['a string value']);
```

#### Arrays::filter

[](#arraysfilter)

Aliased in the filterer as `array`, this filter verifies that the argument is an array and checks the length of the array against bounds. The default bounds are 1+, so an empty array fails by default.

The following checks that the `$value` is an array with exactly 3 elements.

```
\TraderInteractive\Filter\Arrays::filter($value, 3, 3);
```

#### Arrays::flatten

[](#arraysflatten)

Aliased in the filterer as `flatten`, this filter flattens a multi-dimensional array to a single dimension. The order of values will be maintained, but the keys themselves will not. For example:

```
$value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
assert($value === [1, 2, 3, 4, 5]);
```

#### Arrays::pad

[](#arrayspad)

Aliased in the filterer as `array-pad`, this filter pads an array to the specified length with a value. Padding optionally to the front or end of the array.

Example Usage:

```
$specification = ['field' => [['array-pad', 5, 0, Arrays::ARRAY_PAD_LEFT]]],
$filterer = new TraderInteractive\Filterer($specification);
$input = [2, 4, 6];
$result = $filterer->execute($input);
assert(['field' => [0, 0, 2, 4, 6]], $result->filteredValue);
```

#### Booleans::filter

[](#booleansfilter)

Aliased in the filterer as `bool`, this filter verifies that the argument is a boolean value or a string that maps to one. The second parameter can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The last parameters are lists of strings for true values and false values. By default, the strings "true" and "false" map to their boolean counterparts.

The following example converts `$value` to a boolean allowing the strings "on" and "of".

```
$enabled = \TraderInteractive\Filter\Booleans::filter($value, false, ['on'], ['off']);
```

#### Booleans::convert

[](#booleansconvert)

Aliased in the filterer as `bool-convert`, this filter will convert a given boolean value into the provided true or false conditions. By default the return values are the strings 'true' and 'false'

The following converts the boolean `$value` to either 'yes' or 'no'

```
$answer = \TraderInteractive\Filter\Booleans::convert($value, 'yes', 'no');
```

#### Floats/Ints/UnsignedInt::filter

[](#floatsintsunsignedintfilter)

Aliased in the filterer as `float`, `int`, and `uint`, respectively, these filters verify that the arguments are of the proper numeric type and allow for bounds checking. The second parameter to each of them can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The next two parameters are the min and max bounds and can be used to limit the domain of allowed numbers.

Non-numeric strings will fail validation, and numeric strings will be cast.

The float parameter has an additional parameter that can be set to `true` to cast integers to floats. Without this, integers will fail validation.

The following checks that `$value` is an integer between 1 and 100 inclusive, and returns the integer (after casting it if it was a string).

```
$value = \TraderInteractive\Filter\UnsignedInt::filter($value, false, 1, 100);
```

#### Strings::filter

[](#stringsfilter)

Aliased in the filterer as `string`, this filter verifies that the argument is a string. The second parameter can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The last parameters specify the length bounds of the string. The default bounds are 1+, so an empty string fails by default.

The following checks that `$value` is a non-empty string.

```
\TraderInteractive\Filter\Strings::filter($value);
```

#### Strings::concat

[](#stringsconcat)

Aliased in the filterer as `concat`, this filter concatenates the given $value, $prefix and $suffix and returns the resulting string.

```
$value = \TraderInteractive\Filter\Strings::concat('middle', 'begining_', '_end');
assert($value === 'begining_middle_end');
```

#### Strings::explode

[](#stringsexplode)

Aliased in the filterer as `explode`, this filter is essentially a wrapper around the built-in [`explode`](http://www.php.net/explode) method with the value first in order to work with the `Filterer`. It also defaults to using `,` as a delimiter. For example:

```
$value = \TraderInteractive\Filter\Strings::explode('abc,def,ghi');
assert($value === ['abc', 'def', 'ghi']);
```

#### Strings::stripEmoji

[](#stringsstripemoji)

Aliased in the filterer as `strip-emoji`, this filter removes emoji characters from a given string optionally replacing them with the given replacement string

```
$value = \TraderInteractive\Filter\Strings::stripEmoji('this is ridiculous🙄', '!');
assert($value === 'this is ridiculous!');
```

#### Strings::stripTags

[](#stringsstriptags)

Aliased in the filterer as `strip-tags`, this filter is essentially a wrapper around the built-in [`strip_tags`](http://php.net/manual/en/function.strip-tags.php) function. However, unlike the native function the stripTags method will return null when given a null value.

```
$value = \TraderInteractive\Filter\Strings::stripTags('A string with tags');
assert($value === 'a string with tags');
```

#### Strings::translate

[](#stringstranslate)

Aliased in the filterer as `translate`, this filter will accept a string value and return its translated value found in the given $valueMap.

```
$value = \TraderInteractive\Filter\Strings::tranlsate('bar', ['foo' => 'translated to bar', 'bar' => 'translated to foo']);
assert($value === 'translated to foo');
```

#### Url::filter

[](#urlfilter)

Aliased in the filterer as `url`, this filter verifies that the argument is a URL string according to [RFC2396](http://www.faqs.org/rfcs/rfc2396). The second parameter can be set to `true` to allow null values through without an error (they will stay null and not get converted to false).

The following checks that `$value` is a URL.

```
\TraderInteractive\Filter\Url::filter($value);
```

#### Closures::filter

[](#closuresfilter)

Aliased in the filterer as `closure`, this filter verifies that the argument is a closure function.

The following checks that `$closureMethod` is a closure.

```
$closureMethod = function () {
    doSomething();
};
\TraderInteractive\Filter\Closures::filter($closureMethod);
```

#### Email::filter

[](#emailfilter)

Aliased in the filterer as `email`, this filter verifies that the argument is an email.

The following checks that `$value` is an email.

```
\TraderInteractive\Filter\Email::filter($value);
```

#### DateTime::filter

[](#datetimefilter)

Aliased in the filterer as `date`, this will filter the value as a `\DateTime` object. The value can be any string that conforms to [PHP's valid date/time formats](http://php.net/manual/en/datetime.formats.php)

The following checks that `$value` is a date/time.

```
$dateTime = \TraderInteractive\Filter\DateTime::filter('2014-02-04T11:55:00-0500');
```

#### DateTime::format

[](#datetimeformat)

Aliased in the filterer as `date-format`, this will filter a given `\\DateTime' value to a string based on the given format.

The following returns formatted string for a given `\DateTime` `$value`

```
$formatted = \TraderInteractive\Filter\DateTime::format($value, 'Y-m-d H:i:s');
```

#### DateTimeZone::filter

[](#datetimezonefilter)

Aliased in the filterer as `date`, this will filter the value as a `\DateTimeZone` object. The value can be any [supported timezone name](http://php.net/manual/en/timezones.php)

The following checks that `$value` is a timezone

```
$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York');
```

#### Json::validate

[](#jsonvalidate)

Aliased in the filter as `json`, checks that the JSON is valid and returns the original value.

The following ensures that `$value` is valid JSON

```
$value = \TraderInteractive\Filter\Json::validate('{"foo": "bar"}');
```

#### Json::parse

[](#jsonparse)

Aliased in the filter as `json-decode`, checks that the JSON is valid and returns the decoded result.

The following decodes the given value and returns the result.

```
$value = \TraderInteractive\Filter\Json::parse('{"foo": "bar"}');
assert($value === ['foo' => 'bar']);
```

#### PhoneFilter::filter

[](#phonefilterfilter)

Aliased in the filter as `phone`, this will filter a given value as a phone. Returning the phone in the specified format.

The following filters the given string into a formatted phone string

```
$value = \TraderInteractive\Filter\PhoneFilter::filter('234.567.8901', false, '({area}) {exchange}-{station}');
assert($value === '(234) 567-8901');
```

#### TimeOfDayFilter::filter

[](#timeofdayfilterfilter)

Aliased in the filterer as `time-of-day`, this will filter a given string value as a time of day in `HH:MM:SS` format.

The following ensures that `$value` is a valid `HH:MM:SS` formatted string.

```
$value = \TraderInteractive\Filter\TimeOfDayFilter::filter('12:15:23');
assert($value === '12:15:23');
```

#### UuidFilter::filter

[](#uuidfilterfilter)

Aliased in the filterer as `uuid`, this will filter a given string values as a valid UUID.

The following ensures the `$value` is a valid UUID v4 formatted string. Disallowing null values, nil UUIDs and UUID version other than v4

```
$value = \TraderInteractive\Filter\UuidFilter::filter('2c02b87a-97ec-4de0-8c50-6721a29c150f', false, false, [4]);
assert($value === '2c02b87a-97ec-4de0-8c50-6721a29c150f');
```

#### XmlFilter::filter

[](#xmlfilterfilter)

Aliased in the filter as `xml`, this will ensure the given string value is valid XML, returning the original value.

The following ensures the given string is valid xml.

```
$value =
