Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Json, Phone and Xml filter aliases #94

Merged
merged 3 commits into from Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 115 additions & 0 deletions README.md
Expand Up @@ -619,6 +619,121 @@ The following checks that `$value` is a timezone
$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York');
```

#### Json::validate
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
```php
$value = \TraderInteractive\Filter\Json::validate('{"foo": "bar"}');
```

#### Json::parse
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.
```php
$value = \TraderInteractive\Filter\Json::parse('{"foo": "bar"}');
assert($value === ['foo' => 'bar']);
```

#### PhoneFilter::filter
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
```php
$value = \TraderInteractive\Filter\PhoneFilter::filter('234.567.8901', false, '({area}) {exchange}-{station}');
assert($value === '(234) 567-8901');
```

#### XmlFilter::filter
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.
```php
$value = <<<XML
<?xml version="1.0"?>
<books>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developers Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies</description>
</book>
</books>
XML;
$xml = \TraderInteractive\Filter\XmlFilter::filter($value);
```

#### XmlFilter::extract
Aliased in the filter as `xml-extract`, this will ensure the given string value is valid XML then extract and return the element found at the given xpath.

The following ensures the given string is valid xml and returns the title element of the first book.
```php
$value = <<<XML
<?xml version="1.0"?>
<books>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developers Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies</description>
</book>
</books>
XML;
$xpath = "/books/book[@id='bk101']/title";
$titleXml = \TraderInteractive\Filter\XmlFilter::extract($value, $xpath);
assert($titleXml === '<title>XML Developers Guide</title>');
```

#### XmlFilter::validate
Aliased in the filter as `xml-validate`, this will ensure the given string value is valid XML and also confirms to the given XSD file. The original value is returned.

The following ensures the given string is valid xml and matches books.xsd.
```php
$value = <<<XML
<?xml version="1.0"?>
<books>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developers Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies</description>
</book>
</books>
XML;
$xml = \TraderInteractive\Filter\XmlFilter::validate($value, 'books.xsd');
```

## Contact
Developers may be contacted at:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -36,7 +36,7 @@
"traderinteractive/filter-dates": "^3.0",
"traderinteractive/filter-floats": "^3.0",
"traderinteractive/filter-ints": "^3.0",
"traderinteractive/filter-strings": "^3.3.2"
"traderinteractive/filter-strings": "^3.5"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
Expand Down
9 changes: 9 additions & 0 deletions src/Filterer.php
Expand Up @@ -6,6 +6,9 @@
use InvalidArgumentException;
use Throwable;
use TraderInteractive\Exceptions\FilterException;
use TraderInteractive\Filter\Json;
use TraderInteractive\Filter\PhoneFilter;
use TraderInteractive\Filter\XmlFilter;

/**
* Class to filter an array of input.
Expand All @@ -30,16 +33,22 @@ final class Filterer implements FiltererInterface
'float' => '\\TraderInteractive\\Filter\\Floats::filter',
'in' => '\\TraderInteractive\\Filter\\Arrays::in',
'int' => '\\TraderInteractive\\Filter\\Ints::filter',
'json' => Json::class . '::validate',
'json-decode' => Json::class . '::parse',
'ofArray' => '\\TraderInteractive\\Filterer::ofArray',
'ofArrays' => '\\TraderInteractive\\Filterer::ofArrays',
'ofScalars' => '\\TraderInteractive\\Filterer::ofScalars',
'phone' => PhoneFilter::class . '::filter',
'redact' => '\\TraderInteractive\\Filter\\Strings::redact',
'string' => '\\TraderInteractive\\Filter\\Strings::filter',
'strip-tags' => '\\TraderInteractive\\Filter\\Strings::stripTags',
'timezone' => '\\TraderInteractive\\Filter\\DateTimeZone::filter',
'translate' => '\\TraderInteractive\\Filter\\Strings::translate',
'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter',
'url' => '\\TraderInteractive\\Filter\\Url::filter',
'xml' => XmlFilter::class . '::filter',
'xml-extract' => XmlFilter::class . '::extract',
'xml-validate' => XmlFilter::class . '::validate',
];

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/FiltererTest.php
Expand Up @@ -18,6 +18,31 @@
*/
final class FiltererTest extends TestCase
{
/**
* @var string
*/
const FULL_XML = (''
. "<?xml version=\"1.0\"?>\n"
. '<books>'
. '<book id="bk101">'
. '<author>Gambardella, Matthew</author>'
. "<title>XML Developer's Guide</title>"
. '<genre>Computer</genre>'
. '<price>44.95</price>'
. '<publish_date>2000-10-01</publish_date>'
. '<description>An in-depth look at creating applications with XML.</description>'
. '</book>'
. '<book id="bk102">'
. '<author>Ralls, Kim</author>'
. '<title>Midnight Rain</title>'
. '<genre>Fantasy</genre>'
. '<price>5.95</price>'
. '<publish_date>2000-12-16</publish_date>'
. '<description>A former architect battles corporate zombies</description>'
. '</book>'
. "</books>\n"
);

public function setUp()
{
Filterer::setFilterAliases(Filterer::DEFAULT_FILTER_ALIASES);
Expand Down Expand Up @@ -384,6 +409,58 @@ function (int $input, int $fieldOneValue) : int {
'options' => [],
'result' => [true, ['field' => null], null, []],
],
'phone alias' => [
'spec' => ['field' => [['phone']]],
'input' => ['field' => '(234) 567 8901'],
'options' => [],
'result' => [true, ['field' => '2345678901'], null, []],
],
'json alias' => [
'spec' => ['field' => [['json']]],
'input' => ['field' => '{"foo": "bar"}'],
'options' => [],
'result' => [true, ['field' => '{"foo": "bar"}'], null, []],
],
'json-decode alias' => [
'spec' => ['field' => [['json-decode']]],
'input' => ['field' => '{"foo": "bar"}'],
'options' => [],
'result' => [true, ['field' => ['foo' => 'bar']], null, []],
],
'xml alias' => [
'spec' => ['field' => [['xml']]],
'input' => ['field' => self::FULL_XML],
'options' => [],
'result' => [true, ['field' => self::FULL_XML], null, []],
],
'xml-validate alias' => [
'spec' => ['field' => [['xml-validate', __DIR__ . '/_files/books.xsd']]],
'input' => ['field' => self::FULL_XML],
'options' => [],
'result' => [true, ['field' => self::FULL_XML], null, []],
],
'xml-extract alias' => [
'spec' => ['field' => [['xml-extract', "/books/book[@id='bk101']"]]],
'input' => ['field' => self::FULL_XML],
'options' => [],
'result' => [
true,
[
'field' => (''
. '<book id="bk101">'
. '<author>Gambardella, Matthew</author>'
. "<title>XML Developer's Guide</title>"
. '<genre>Computer</genre>'
. '<price>44.95</price>'
. '<publish_date>2000-10-01</publish_date>'
. '<description>An in-depth look at creating applications with XML.</description>'
. '</book>'
),
],
null,
[]
],
],
];
}

Expand Down
20 changes: 20 additions & 0 deletions tests/_files/books.xsd
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="BookType">
<xsd:sequence>
<xsd:element name="author" type="xsd:string" minOccurs="1"/>
<xsd:element name="title" type="xsd:string" minOccurs="1"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="publish_date" type="xsd:date" />
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="BooksType">
<xsd:sequence>
<xsd:element name="book" type="BookType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="books" type="BooksType"/>
</xsd:schema>