Skip to content

Commit 2252d5e

Browse files
authored
Merge pull request #94 from chadicus/master
Add Json, Phone and Xml filter aliases
2 parents 3590bf1 + b03696b commit 2252d5e

File tree

5 files changed

+222
-1
lines changed

5 files changed

+222
-1
lines changed

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,121 @@ The following checks that `$value` is a timezone
619619
$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York');
620620
```
621621

622+
#### Json::validate
623+
Aliased in the filter as `json`, checks that the JSON is valid and returns the original value.
624+
625+
The following ensures that `$value` is valid JSON
626+
```php
627+
$value = \TraderInteractive\Filter\Json::validate('{"foo": "bar"}');
628+
```
629+
630+
#### Json::parse
631+
Aliased in the filter as `json-decode`, checks that the JSON is valid and returns the decoded result.
632+
633+
The following decodes the given value and returns the result.
634+
```php
635+
$value = \TraderInteractive\Filter\Json::parse('{"foo": "bar"}');
636+
assert($value === ['foo' => 'bar']);
637+
```
638+
639+
#### PhoneFilter::filter
640+
Aliased in the filter as `phone`, this will filter a given value as a phone. Returning the phone in the specified format.
641+
642+
The following filters the given string into a formatted phone string
643+
```php
644+
$value = \TraderInteractive\Filter\PhoneFilter::filter('234.567.8901', false, '({area}) {exchange}-{station}');
645+
assert($value === '(234) 567-8901');
646+
```
647+
648+
#### XmlFilter::filter
649+
Aliased in the filter as `xml`, this will ensure the given string value is valid XML, returning the original value.
650+
651+
The following ensures the given string is valid xml.
652+
```php
653+
$value = <<<XML
654+
<?xml version="1.0"?>
655+
<books>
656+
<book id="bk101">
657+
<author>Gambardella, Matthew</author>
658+
<title>XML Developers Guide</title>
659+
<genre>Computer</genre>
660+
<price>44.95</price>
661+
<publish_date>2000-10-01</publish_date>
662+
<description>An in-depth look at creating applications with XML.</description>
663+
</book>
664+
<book id="bk102">
665+
<author>Ralls, Kim</author>
666+
<title>Midnight Rain</title>
667+
<genre>Fantasy</genre>
668+
<price>5.95</price>
669+
<publish_date>2000-12-16</publish_date>
670+
<description>A former architect battles corporate zombies</description>
671+
</book>
672+
</books>
673+
XML;
674+
$xml = \TraderInteractive\Filter\XmlFilter::filter($value);
675+
```
676+
677+
#### XmlFilter::extract
678+
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.
679+
680+
The following ensures the given string is valid xml and returns the title element of the first book.
681+
```php
682+
$value = <<<XML
683+
<?xml version="1.0"?>
684+
<books>
685+
<book id="bk101">
686+
<author>Gambardella, Matthew</author>
687+
<title>XML Developers Guide</title>
688+
<genre>Computer</genre>
689+
<price>44.95</price>
690+
<publish_date>2000-10-01</publish_date>
691+
<description>An in-depth look at creating applications with XML.</description>
692+
</book>
693+
<book id="bk102">
694+
<author>Ralls, Kim</author>
695+
<title>Midnight Rain</title>
696+
<genre>Fantasy</genre>
697+
<price>5.95</price>
698+
<publish_date>2000-12-16</publish_date>
699+
<description>A former architect battles corporate zombies</description>
700+
</book>
701+
</books>
702+
XML;
703+
$xpath = "/books/book[@id='bk101']/title";
704+
$titleXml = \TraderInteractive\Filter\XmlFilter::extract($value, $xpath);
705+
assert($titleXml === '<title>XML Developers Guide</title>');
706+
```
707+
708+
#### XmlFilter::validate
709+
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.
710+
711+
The following ensures the given string is valid xml and matches books.xsd.
712+
```php
713+
$value = <<<XML
714+
<?xml version="1.0"?>
715+
<books>
716+
<book id="bk101">
717+
<author>Gambardella, Matthew</author>
718+
<title>XML Developers Guide</title>
719+
<genre>Computer</genre>
720+
<price>44.95</price>
721+
<publish_date>2000-10-01</publish_date>
722+
<description>An in-depth look at creating applications with XML.</description>
723+
</book>
724+
<book id="bk102">
725+
<author>Ralls, Kim</author>
726+
<title>Midnight Rain</title>
727+
<genre>Fantasy</genre>
728+
<price>5.95</price>
729+
<publish_date>2000-12-16</publish_date>
730+
<description>A former architect battles corporate zombies</description>
731+
</book>
732+
</books>
733+
XML;
734+
$xml = \TraderInteractive\Filter\XmlFilter::validate($value, 'books.xsd');
735+
```
736+
622737
## Contact
623738
Developers may be contacted at:
624739

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"traderinteractive/filter-dates": "^3.0",
3737
"traderinteractive/filter-floats": "^3.0",
3838
"traderinteractive/filter-ints": "^3.0",
39-
"traderinteractive/filter-strings": "^3.3.2"
39+
"traderinteractive/filter-strings": "^3.5"
4040
},
4141
"require-dev": {
4242
"php-coveralls/php-coveralls": "^2.0",

src/Filterer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use InvalidArgumentException;
77
use Throwable;
88
use TraderInteractive\Exceptions\FilterException;
9+
use TraderInteractive\Filter\Json;
10+
use TraderInteractive\Filter\PhoneFilter;
11+
use TraderInteractive\Filter\XmlFilter;
912

1013
/**
1114
* Class to filter an array of input.
@@ -30,16 +33,22 @@ final class Filterer implements FiltererInterface
3033
'float' => '\\TraderInteractive\\Filter\\Floats::filter',
3134
'in' => '\\TraderInteractive\\Filter\\Arrays::in',
3235
'int' => '\\TraderInteractive\\Filter\\Ints::filter',
36+
'json' => Json::class . '::validate',
37+
'json-decode' => Json::class . '::parse',
3338
'ofArray' => '\\TraderInteractive\\Filterer::ofArray',
3439
'ofArrays' => '\\TraderInteractive\\Filterer::ofArrays',
3540
'ofScalars' => '\\TraderInteractive\\Filterer::ofScalars',
41+
'phone' => PhoneFilter::class . '::filter',
3642
'redact' => '\\TraderInteractive\\Filter\\Strings::redact',
3743
'string' => '\\TraderInteractive\\Filter\\Strings::filter',
3844
'strip-tags' => '\\TraderInteractive\\Filter\\Strings::stripTags',
3945
'timezone' => '\\TraderInteractive\\Filter\\DateTimeZone::filter',
4046
'translate' => '\\TraderInteractive\\Filter\\Strings::translate',
4147
'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter',
4248
'url' => '\\TraderInteractive\\Filter\\Url::filter',
49+
'xml' => XmlFilter::class . '::filter',
50+
'xml-extract' => XmlFilter::class . '::extract',
51+
'xml-validate' => XmlFilter::class . '::validate',
4352
];
4453

4554
/**

tests/FiltererTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@
1818
*/
1919
final class FiltererTest extends TestCase
2020
{
21+
/**
22+
* @var string
23+
*/
24+
const FULL_XML = (''
25+
. "<?xml version=\"1.0\"?>\n"
26+
. '<books>'
27+
. '<book id="bk101">'
28+
. '<author>Gambardella, Matthew</author>'
29+
. "<title>XML Developer's Guide</title>"
30+
. '<genre>Computer</genre>'
31+
. '<price>44.95</price>'
32+
. '<publish_date>2000-10-01</publish_date>'
33+
. '<description>An in-depth look at creating applications with XML.</description>'
34+
. '</book>'
35+
. '<book id="bk102">'
36+
. '<author>Ralls, Kim</author>'
37+
. '<title>Midnight Rain</title>'
38+
. '<genre>Fantasy</genre>'
39+
. '<price>5.95</price>'
40+
. '<publish_date>2000-12-16</publish_date>'
41+
. '<description>A former architect battles corporate zombies</description>'
42+
. '</book>'
43+
. "</books>\n"
44+
);
45+
2146
public function setUp()
2247
{
2348
Filterer::setFilterAliases(Filterer::DEFAULT_FILTER_ALIASES);
@@ -384,6 +409,58 @@ function (int $input, int $fieldOneValue) : int {
384409
'options' => [],
385410
'result' => [true, ['field' => null], null, []],
386411
],
412+
'phone alias' => [
413+
'spec' => ['field' => [['phone']]],
414+
'input' => ['field' => '(234) 567 8901'],
415+
'options' => [],
416+
'result' => [true, ['field' => '2345678901'], null, []],
417+
],
418+
'json alias' => [
419+
'spec' => ['field' => [['json']]],
420+
'input' => ['field' => '{"foo": "bar"}'],
421+
'options' => [],
422+
'result' => [true, ['field' => '{"foo": "bar"}'], null, []],
423+
],
424+
'json-decode alias' => [
425+
'spec' => ['field' => [['json-decode']]],
426+
'input' => ['field' => '{"foo": "bar"}'],
427+
'options' => [],
428+
'result' => [true, ['field' => ['foo' => 'bar']], null, []],
429+
],
430+
'xml alias' => [
431+
'spec' => ['field' => [['xml']]],
432+
'input' => ['field' => self::FULL_XML],
433+
'options' => [],
434+
'result' => [true, ['field' => self::FULL_XML], null, []],
435+
],
436+
'xml-validate alias' => [
437+
'spec' => ['field' => [['xml-validate', __DIR__ . '/_files/books.xsd']]],
438+
'input' => ['field' => self::FULL_XML],
439+
'options' => [],
440+
'result' => [true, ['field' => self::FULL_XML], null, []],
441+
],
442+
'xml-extract alias' => [
443+
'spec' => ['field' => [['xml-extract', "/books/book[@id='bk101']"]]],
444+
'input' => ['field' => self::FULL_XML],
445+
'options' => [],
446+
'result' => [
447+
true,
448+
[
449+
'field' => (''
450+
. '<book id="bk101">'
451+
. '<author>Gambardella, Matthew</author>'
452+
. "<title>XML Developer's Guide</title>"
453+
. '<genre>Computer</genre>'
454+
. '<price>44.95</price>'
455+
. '<publish_date>2000-10-01</publish_date>'
456+
. '<description>An in-depth look at creating applications with XML.</description>'
457+
. '</book>'
458+
),
459+
],
460+
null,
461+
[]
462+
],
463+
],
387464
];
388465
}
389466

tests/_files/books.xsd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<xsd:complexType name="BookType">
4+
<xsd:sequence>
5+
<xsd:element name="author" type="xsd:string" minOccurs="1"/>
6+
<xsd:element name="title" type="xsd:string" minOccurs="1"/>
7+
<xsd:element name="genre" type="xsd:string"/>
8+
<xsd:element name="price" type="xsd:float" />
9+
<xsd:element name="publish_date" type="xsd:date" />
10+
<xsd:element name="description" type="xsd:string"/>
11+
</xsd:sequence>
12+
<xsd:attribute name="id" type="xsd:string" use="required"/>
13+
</xsd:complexType>
14+
<xsd:complexType name="BooksType">
15+
<xsd:sequence>
16+
<xsd:element name="book" type="BookType" minOccurs="0" maxOccurs="unbounded"/>
17+
</xsd:sequence>
18+
</xsd:complexType>
19+
<xsd:element name="books" type="BooksType"/>
20+
</xsd:schema>

0 commit comments

Comments
 (0)