diff --git a/README.md b/README.md index 77a5006..a6a4d34 100644 --- a/README.md +++ b/README.md @@ -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 = << + + + Gambardella, Matthew + XML Developers Guide + Computer + 44.95 + 2000-10-01 + An in-depth look at creating applications with XML. + + + Ralls, Kim + Midnight Rain + Fantasy + 5.95 + 2000-12-16 + A former architect battles corporate zombies + + +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 = << + + + Gambardella, Matthew + XML Developers Guide + Computer + 44.95 + 2000-10-01 + An in-depth look at creating applications with XML. + + + Ralls, Kim + Midnight Rain + Fantasy + 5.95 + 2000-12-16 + A former architect battles corporate zombies + + +XML; +$xpath = "/books/book[@id='bk101']/title"; +$titleXml = \TraderInteractive\Filter\XmlFilter::extract($value, $xpath); +assert($titleXml === 'XML Developers Guide'); +``` + +#### 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 = << + + + Gambardella, Matthew + XML Developers Guide + Computer + 44.95 + 2000-10-01 + An in-depth look at creating applications with XML. + + + Ralls, Kim + Midnight Rain + Fantasy + 5.95 + 2000-12-16 + A former architect battles corporate zombies + + +XML; +$xml = \TraderInteractive\Filter\XmlFilter::validate($value, 'books.xsd'); +``` + ## Contact Developers may be contacted at: diff --git a/composer.json b/composer.json index 3d1e8a2..7795df4 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Filterer.php b/src/Filterer.php index da625e9..e9ef499 100644 --- a/src/Filterer.php +++ b/src/Filterer.php @@ -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. @@ -30,9 +33,12 @@ 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', @@ -40,6 +46,9 @@ final class Filterer implements FiltererInterface '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', ]; /** diff --git a/tests/FiltererTest.php b/tests/FiltererTest.php index 25d4e3a..a0dba67 100644 --- a/tests/FiltererTest.php +++ b/tests/FiltererTest.php @@ -18,6 +18,31 @@ */ final class FiltererTest extends TestCase { + /** + * @var string + */ + const FULL_XML = ('' + . "\n" + . '' + . '' + . 'Gambardella, Matthew' + . "XML Developer's Guide" + . 'Computer' + . '44.95' + . '2000-10-01' + . 'An in-depth look at creating applications with XML.' + . '' + . '' + . 'Ralls, Kim' + . 'Midnight Rain' + . 'Fantasy' + . '5.95' + . '2000-12-16' + . 'A former architect battles corporate zombies' + . '' + . "\n" + ); + public function setUp() { Filterer::setFilterAliases(Filterer::DEFAULT_FILTER_ALIASES); @@ -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' => ('' + . '' + . 'Gambardella, Matthew' + . "XML Developer's Guide" + . 'Computer' + . '44.95' + . '2000-10-01' + . 'An in-depth look at creating applications with XML.' + . '' + ), + ], + null, + [] + ], + ], ]; } diff --git a/tests/_files/books.xsd b/tests/_files/books.xsd new file mode 100644 index 0000000..7234b86 --- /dev/null +++ b/tests/_files/books.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + +