diff --git a/README.md b/README.md index a85b0a3..31d5762 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ $schema = [ ]; $schemator = (new SchematorBuilder())->get(); -$output = $schemator->exec($input, $schema); +$output = $schemator->convert($input, $schema); print_r($output); /* Array @@ -118,6 +118,35 @@ print_r($output); */ ``` +#### Setting errors level + +```php +use Smoren\Schemator\Factories\SchematorBuilder; +use Smoren\Schemator\Structs\ErrorsLevelMask; +use Smoren\Schemator\Exceptions\SchematorException; + +$input = [ + 'some_key' => null, +]; +$schema = [ + 'my_value' => ['some_key', ['date', 'Y-m-d']], +]; + +$schemator = (new SchematorBuilder()) + ->withErrorsLevelMask( + ErrorsLevelMask::nothing() + ->add([SchematorException::FILTER_ERROR, SchematorException::CANNOT_GET_VALUE]) + ) + ->get(); + +try { + $schemator->convert($input, $schema); +} catch(SchematorException $e) { + echo $e->getMessage(); // filter error: 'date' +} + +``` + #### Using base filters ```php @@ -169,7 +198,7 @@ $schema = [ $schemator = (new SchematorBuilder()) ->withFilters(new BaseFiltersStorage()) ->get(); -$output = $schemator->exec($input, $schema); +$output = $schemator->convert($input, $schema); print_r($output); /* @@ -211,13 +240,13 @@ use Smoren\Schemator\Factories\SchematorBuilder; use Smoren\Schemator\Filters\BaseFiltersStorage; $schemator = (new SchematorBuilder()) - ->withFilters(BaseFiltersStorage()) + ->withFilters(new BaseFiltersStorage()) ->get(); $input = [ 'numbers' => [-1, 10, 5, 22, -10, 0, 35, 7, 8, 9, 0], ]; -$output = $schemator->exec($input, [ +$output = $schemator->convert($input, [ 'positive' => [ 'numbers', ['filter', [['>', 0]]], @@ -269,7 +298,7 @@ Array ) */ -$output = $schemator->exec($input, [ +$output = $schemator->convert($input, [ 'number_types' => ['numbers', [ 'replace', [ @@ -326,7 +355,7 @@ $schema = [ 'street_names' => ['streets', ['startsWith', 'T'], ['implode', ', ']], ]; -$output = $schemator->exec($input, $schema); +$output = $schemator->convert($input, $schema); print_r($output); /* @@ -340,10 +369,10 @@ Array #### Mass usage ```php -use Smoren\Schemator\Components\Schemator; +use Smoren\Schemator\Factories\SchematorBuilder; use Smoren\Schemator\Components\MassSchemator; -$massSchemator = new MassSchemator(new Schemator()); +$massSchemator = new MassSchemator((new SchematorBuilder())->get()); $cities = [ [ diff --git a/composer.json b/composer.json index 9f7abde..3868701 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "php": ">=7.4.0", "smoren/extended-exceptions": "1.0.0", "smoren/helpers": "^0.1.4", - "smoren/bitmap-tools": "^0.2.0" + "smoren/bitmap-tools": "^0.3.0" }, "require-dev": { "codeception/codeception": "^4.2.1", diff --git a/src/Components/Schemator.php b/src/Components/Schemator.php index a866195..a706c92 100644 --- a/src/Components/Schemator.php +++ b/src/Components/Schemator.php @@ -7,6 +7,7 @@ use Smoren\Schemator\Interfaces\NestedAccessorFactoryInterface; use Smoren\Schemator\Interfaces\SchematorInterface; use Smoren\Schemator\Factories\NestedAccessorFactory; +use Smoren\Schemator\Structs\ErrorsLevelMask; use Smoren\Schemator\Structs\FilterContext; use Smoren\Schemator\Exceptions\NestedAccessorException; use Smoren\Schemator\Exceptions\SchematorException; @@ -18,8 +19,6 @@ */ class Schemator implements SchematorInterface { - public const ERRORS_LEVEL_DEFAULT = 114; - /** * @var array filters map */ @@ -37,34 +36,25 @@ class Schemator implements SchematorInterface */ protected NestedAccessorFactoryInterface $nestedAccessorFactory; - /** - * Creates bitmap errors level mask - * @param array $errorCodes - * @return int - */ - public static function createErrorsLevelMask(array $errorCodes): int - { - return Bitmap::create($errorCodes)->getValue(); - } - /** * Schemator constructor. * @param non-empty-string $pathDelimiter delimiter for multilevel paths - * @param int $errorsLevelMask bitmap errors level mask + * @param BitmapInterface|null $errorsLevelMask bitmap errors level mask + * @param NestedAccessorFactoryInterface|null $nestedAccessorFactory nested accessor factory */ public function __construct( string $pathDelimiter = '.', - int $errorsLevelMask = self::ERRORS_LEVEL_DEFAULT, + ?BitmapInterface $errorsLevelMask = null, NestedAccessorFactoryInterface $nestedAccessorFactory = null ) { $this->pathDelimiter = $pathDelimiter; - $this->setErrorsLevelMask($errorsLevelMask); + $this->errorsLevelMask = $errorsLevelMask ?? ErrorsLevelMask::default(); $this->nestedAccessorFactory = $nestedAccessorFactory ?? new NestedAccessorFactory(); } /** * @inheritDoc - * @throws NestedAccessorException + * @throws SchematorException */ public function convert($source, array $schema) { @@ -83,6 +73,7 @@ public function convert($source, array $schema) /** * @inheritDoc + * @throws SchematorException */ public function getValue($source, $key) { @@ -116,9 +107,9 @@ public function setPathDelimiter(string $value): void /** * @inheritDoc */ - public function setErrorsLevelMask(int $value): void + public function setErrorsLevelMask(BitmapInterface $value): void { - $this->errorsLevelMask = Bitmap::create($value); + $this->errorsLevelMask = $value; } /** @@ -228,7 +219,7 @@ protected function runFilter(array $filterConfig, $source, $rootSource) try { return $this->filterMap[$filterName]( - new FilterContext($this, $source, $rootSource), + new FilterContext($this, $source, $rootSource, $filterConfig), ...$filterConfig ); } catch(SchematorException $e) { diff --git a/src/Exceptions/SchematorException.php b/src/Exceptions/SchematorException.php index e865cd1..0e03a4d 100644 --- a/src/Exceptions/SchematorException.php +++ b/src/Exceptions/SchematorException.php @@ -39,21 +39,21 @@ public static function createAsFilterNotFound(string $filterName): SchematorExce * @param string $filterName name of the filter * @param mixed $filterConfig arguments for filter * @param mixed $source source for filtering - * @param Throwable $previous exception thrown in the filter body + * @param ?Throwable $previous exception thrown in the filter body * @return SchematorException */ public static function createAsFilterError( string $filterName, $filterConfig, $source, - Throwable $previous + ?Throwable $previous = null ): SchematorException { return new SchematorException( "filter error: '{$filterName}'", SchematorException::FILTER_ERROR, $previous, [ - 'error' => $previous->getMessage(), + 'error' => $previous ? $previous->getMessage() : "filter error: '{$filterName}'", 'filter_name' => $filterName, 'config' => $filterConfig, 'source' => $source, diff --git a/src/Factories/SchematorBuilder.php b/src/Factories/SchematorBuilder.php index fdb4b2c..a18d809 100644 --- a/src/Factories/SchematorBuilder.php +++ b/src/Factories/SchematorBuilder.php @@ -2,6 +2,7 @@ namespace Smoren\Schemator\Factories; +use Smoren\BitmapTools\Interfaces\BitmapInterface; use Smoren\Schemator\Interfaces\SchematorBuilderInterface; use Smoren\Schemator\Interfaces\SchematorInterface; use Smoren\Schemator\Components\Schemator; @@ -46,7 +47,7 @@ public function withPathDelimiter(string $pathDelimiter): SchematorBuilderInterf /** * @inheritDoc */ - public function withErrorsLevelMask(int $errorsLevelMask): SchematorBuilderInterface + public function withErrorsLevelMask(BitmapInterface $errorsLevelMask): SchematorBuilderInterface { $this->schemator->setErrorsLevelMask($errorsLevelMask); return $this; diff --git a/src/Filters/BaseFiltersStorage.php b/src/Filters/BaseFiltersStorage.php index cd2dd17..5d27a4c 100644 --- a/src/Filters/BaseFiltersStorage.php +++ b/src/Filters/BaseFiltersStorage.php @@ -44,12 +44,13 @@ public static function format(FilterContextInterface $context, callable $formatt * @param string $format php date format * @param int|null $timezone timezone offset * @return false|string|null + * @throws SchematorException */ public static function date(FilterContextInterface $context, string $format, ?int $timezone = null) { $source = $context->getSource(); if($source === null) { - return null; + throw SchematorException::createAsFilterError('date', $context->getConfig(), $source); } if($timezone === null) { return date($format, intval($source)); @@ -62,12 +63,13 @@ public static function date(FilterContextInterface $context, string $format, ?in * @param FilterContextInterface $context filter context * @param string $delimiter separator * @return string|null + * @throws SchematorException */ public static function implode(FilterContextInterface $context, string $delimiter = ', '): ?string { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('implode', $context->getConfig(), $source); } return implode($delimiter, $source); } @@ -77,12 +79,13 @@ public static function implode(FilterContextInterface $context, string $delimite * @param FilterContextInterface $context filter context * @param non-empty-string $delimiter separator * @return false|string[]|null + * @throws SchematorException */ public static function explode(FilterContextInterface $context, string $delimiter = ', ') { $source = $context->getSource(); if($source === null || !is_scalar($source)) { - return null; + throw SchematorException::createAsFilterError('explode', $context->getConfig(), $source); } return explode($delimiter, (string)$source); } @@ -91,12 +94,13 @@ public static function explode(FilterContextInterface $context, string $delimite * Returns the sum of array items * @param FilterContextInterface $context filter context * @return float|int|null + * @throws SchematorException */ public static function sum(FilterContextInterface $context) { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('sum', $context->getConfig(), $source); } return array_sum($source); } @@ -105,12 +109,13 @@ public static function sum(FilterContextInterface $context) * Returns the average value of array items * @param FilterContextInterface $context filter context * @return float|int|null + * @throws SchematorException */ public static function average(FilterContextInterface $context) { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('average', $context->getConfig(), $source); } return array_sum($source)/count($source); } @@ -120,12 +125,13 @@ public static function average(FilterContextInterface $context) * @param FilterContextInterface $context filter context * @param array|callable $filterConfig filter rules config or filter callback * @return array|null + * @throws SchematorException */ public static function filter(FilterContextInterface $context, $filterConfig): ?array { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('filter', $context->getConfig(), $source); } if(is_callable($filterConfig)) { @@ -137,8 +143,7 @@ public static function filter(FilterContextInterface $context, $filterConfig): ? foreach($source as $item) { foreach($filterConfig as $args) { if(!is_array($args)) { - // TODO exception? - return null; + throw SchematorException::createAsFilterError('filter', $context->getConfig(), $source); } $rule = array_shift($args); @@ -158,12 +163,13 @@ public static function filter(FilterContextInterface $context, $filterConfig): ? * @param FilterContextInterface $context filter context * @param callable|null $sortCallback sort callback * @return array|null + * @throws SchematorException */ public static function sort(FilterContextInterface $context, ?callable $sortCallback = null): ?array { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('sort', $context->getConfig(), $source); } if($sortCallback !== null) { usort($source, $sortCallback); @@ -182,7 +188,7 @@ public static function rsort(FilterContextInterface $context): ?array { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('rsort', $context->getConfig(), $source); } rsort($source); @@ -199,7 +205,7 @@ public static function path(FilterContextInterface $context) { $source = $context->getSource(); if($source === null) { - return null; + throw SchematorException::createAsFilterError('path', $context->getConfig(), $source); } return $context->getSchemator()->getValue($context->getRootSource(), $source); } @@ -208,12 +214,13 @@ public static function path(FilterContextInterface $context) * Returns flattened array * @param FilterContextInterface $context filter context * @return array|null + * @throws SchematorException */ public static function flatten(FilterContextInterface $context): ?array { $source = $context->getSource(); if($source === null || !is_array($source)) { - return null; + throw SchematorException::createAsFilterError('flatten', $context->getConfig(), $source); } return ArrHelper::flatten($source); } @@ -223,12 +230,13 @@ public static function flatten(FilterContextInterface $context): ?array * @param FilterContextInterface $context filter context * @param array $rules smart filter replacements * @return array|mixed|null + * @throws SchematorException */ public static function replace(FilterContextInterface $context, array $rules) { $source = $context->getSource(); if($source === null) { - return null; + throw SchematorException::createAsFilterError('replace', $context->getConfig(), $source); } $isArray = is_array($source); @@ -245,8 +253,7 @@ public static function replace(FilterContextInterface $context, array $rules) foreach($rules as $args) { if(!is_array($args)) { - // TODO exception? - return null; + throw SchematorException::createAsFilterError('replace', $context->getConfig(), $source); } $value = array_shift($args); diff --git a/src/Interfaces/FilterContextInterface.php b/src/Interfaces/FilterContextInterface.php index 0f5c717..2e31502 100644 --- a/src/Interfaces/FilterContextInterface.php +++ b/src/Interfaces/FilterContextInterface.php @@ -25,4 +25,10 @@ public function getRootSource(); * @return SchematorInterface */ public function getSchemator(): SchematorInterface; + + /** + * Returns filter config + * @return mixed + */ + public function getConfig(); } diff --git a/src/Interfaces/SchematorBuilderInterface.php b/src/Interfaces/SchematorBuilderInterface.php index 9fcbb32..0a680ea 100644 --- a/src/Interfaces/SchematorBuilderInterface.php +++ b/src/Interfaces/SchematorBuilderInterface.php @@ -2,6 +2,8 @@ namespace Smoren\Schemator\Interfaces; +use Smoren\BitmapTools\Interfaces\BitmapInterface; + /** * Interface SchematorBuilderInterface * @author Smoren @@ -23,10 +25,10 @@ public function withPathDelimiter(string $pathDelimiter): SchematorBuilderInterf /** * Sets errors level mask - * @param int $errorsLevelMask errors level mask + * @param BitmapInterface $errorsLevelMask errors level mask * @return SchematorBuilderInterface */ - public function withErrorsLevelMask(int $errorsLevelMask): SchematorBuilderInterface; + public function withErrorsLevelMask(BitmapInterface $errorsLevelMask): SchematorBuilderInterface; /** * Adds filters to SchematorInterface object diff --git a/src/Interfaces/SchematorInterface.php b/src/Interfaces/SchematorInterface.php index 00d8947..57485b0 100644 --- a/src/Interfaces/SchematorInterface.php +++ b/src/Interfaces/SchematorInterface.php @@ -2,6 +2,7 @@ namespace Smoren\Schemator\Interfaces; +use Smoren\BitmapTools\Interfaces\BitmapInterface; use Smoren\Schemator\Exceptions\SchematorException; /** @@ -37,10 +38,10 @@ public function setPathDelimiter(string $value): void; /** * Setter for errorsLevelMask property - * @param int $value new value + * @param BitmapInterface $value new value * @return void */ - public function setErrorsLevelMask(int $value): void; + public function setErrorsLevelMask(BitmapInterface $value): void; /** * Adds new filter diff --git a/src/Structs/ErrorsLevelMask.php b/src/Structs/ErrorsLevelMask.php new file mode 100644 index 0000000..156f79a --- /dev/null +++ b/src/Structs/ErrorsLevelMask.php @@ -0,0 +1,53 @@ + + */ +class ErrorsLevelMask extends Bitmap +{ + /** + * Creates bitmap of all errors + * @return BitmapInterface + */ + public static function all(): BitmapInterface + { + return new static(static::create([ + SchematorException::FILTER_NOT_FOUND, + SchematorException::FILTER_ERROR, + SchematorException::CANNOT_GET_VALUE, + SchematorException::UNSUPPORTED_SOURCE_TYPE, + SchematorException::UNSUPPORTED_KEY_TYPE, + SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE, + ])->getValue()); + } + + /** + * Creates bitmap of default errors + * @return BitmapInterface + */ + public static function default(): BitmapInterface + { + return static::create([ + SchematorException::FILTER_NOT_FOUND, + SchematorException::UNSUPPORTED_SOURCE_TYPE, + SchematorException::UNSUPPORTED_KEY_TYPE, + SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE, + ]); + } + + /** + * Creates bitmap of no errors + * @return BitmapInterface + */ + public static function nothing(): BitmapInterface + { + return static::create([]); + } +} diff --git a/src/Structs/FilterContext.php b/src/Structs/FilterContext.php index f612c50..638ccf8 100644 --- a/src/Structs/FilterContext.php +++ b/src/Structs/FilterContext.php @@ -23,18 +23,24 @@ class FilterContext implements FilterContextInterface * @var mixed root source of Schemator conversion */ protected $rootSource; + /** + * @var mixed filter config + */ + protected $config; /** * FilterContext constructor. * @param SchematorInterface $schemator Schemator object * @param mixed $source source data to apply filter to * @param mixed $rootSource root source of Schemator conversion + * @param mixed $config filter config */ - public function __construct(SchematorInterface $schemator, $source, $rootSource) + public function __construct(SchematorInterface $schemator, $source, $rootSource, $config) { $this->schemator = $schemator; $this->source = $source; $this->rootSource = $rootSource; + $this->config = $config; } /** @@ -63,4 +69,13 @@ public function getRootSource() { return $this->rootSource; } + + /** + * Config getter + * @return mixed + */ + public function getConfig() + { + return $this->config; + } } diff --git a/tests/unit/SchematorTest.php b/tests/unit/SchematorTest.php index e6c0183..15bbdff 100644 --- a/tests/unit/SchematorTest.php +++ b/tests/unit/SchematorTest.php @@ -7,6 +7,7 @@ use Smoren\Schemator\Factories\SchematorBuilder; use Smoren\Schemator\Filters\BaseFiltersStorage; use Smoren\Schemator\Interfaces\FilterContextInterface; +use Smoren\Schemator\Structs\ErrorsLevelMask; class SchematorTest extends \Codeception\Test\Unit { @@ -153,7 +154,7 @@ function(FilterContextInterface $context) { */ public function testSpecificDelimiter() { - $schemator = new Schemator('/'); + $schemator = (new SchematorBuilder())->withPathDelimiter('/')->get(); $input = [ 'id' => 100, @@ -290,7 +291,7 @@ function(FilterContextInterface $context) { public function testBuilder() { $schemator = (new SchematorBuilder()) - ->withErrorsLevelMask(Schemator::ERRORS_LEVEL_DEFAULT) + ->withErrorsLevelMask(ErrorsLevelMask::default()) ->withFilters(new BaseFiltersStorage()) ->withFilters([ 'startsWith' => function(FilterContextInterface $context, string $start) { @@ -471,7 +472,7 @@ public function testFormat() $schemator = (new SchematorBuilder()) ->withErrorsLevelMask( - Schemator::createErrorsLevelMask([ + ErrorsLevelMask::create([ SchematorException::FILTER_ERROR, ]) ) @@ -620,7 +621,7 @@ public function testGetValue() $this->assertEquals(null, $schemator->getValue($input, 'a.b.c.d')); - $schemator = new Schemator('.', 0); + $schemator = new Schemator('.', ErrorsLevelMask::nothing()); // unsupported key type $this->assertEquals(null, $schemator->getValue($input, (object)[])); @@ -632,7 +633,7 @@ public function testGetValue() $schemator = (new SchematorBuilder()) ->withErrorsLevelMask( - Schemator::createErrorsLevelMask([ + ErrorsLevelMask::create([ SchematorException::CANNOT_GET_VALUE, SchematorException::UNSUPPORTED_SOURCE_TYPE, SchematorException::UNSUPPORTED_KEY_TYPE, @@ -743,7 +744,7 @@ public function testErrorsLevel() ->get(); { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::FILTER_NOT_FOUND, ])); @@ -761,7 +762,7 @@ public function testErrorsLevel() } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::FILTER_ERROR, ])); @@ -779,7 +780,7 @@ public function testErrorsLevel() } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::CANNOT_GET_VALUE, ])); @@ -809,7 +810,7 @@ public function testErrorsLevel() } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::UNSUPPORTED_SOURCE_TYPE, ])); @@ -823,7 +824,7 @@ public function testErrorsLevel() } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::UNSUPPORTED_KEY_TYPE, ])); @@ -841,7 +842,7 @@ public function testErrorsLevel() } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ + $schemator->setErrorsLevelMask(ErrorsLevelMask::create([ SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE, ])); @@ -859,22 +860,38 @@ public function testErrorsLevel() } { + $schemator->setErrorsLevelMask(ErrorsLevelMask::all()); -// $input = ['path_key' => 'unknown.path']; -// $schema = [['path_key', ['path']]]; -// $this->assertEquals(null, $schemator->getValue($input, )); + $input = ['date' => 1651161688]; + $schema = ['date' => ['date', ['unknown_filter']]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::FILTER_NOT_FOUND); + + $input = ['date' => 1651161688]; + $schema = ['date' => ['date', ['date', ['Y-m-d H:i'], 0]]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::FILTER_ERROR); + + $input = ['key' => 1]; + $schema = ['my_key' => 'unknown_key']; + $this->assertFailure($schemator, $input, $schema, SchematorException::CANNOT_GET_VALUE); + + $input = null; + $schema = ['my_key' => 'key']; + $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_SOURCE_TYPE); + + $input = ['key' => 1]; + $schema = ['my_key' => (object)[]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_KEY_TYPE); + $input = ['key' => 1]; + $schema = ['my_key' => ['key', (object)[]]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE); } { - $schemator->setErrorsLevelMask(Schemator::createErrorsLevelMask([ - SchematorException::FILTER_NOT_FOUND, - SchematorException::FILTER_ERROR, - SchematorException::CANNOT_GET_VALUE, - SchematorException::UNSUPPORTED_SOURCE_TYPE, - SchematorException::UNSUPPORTED_KEY_TYPE, - SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE, - ])); + $schemator->setErrorsLevelMask( + ErrorsLevelMask::default() + ->add([SchematorException::CANNOT_GET_VALUE]) + ); $input = ['date' => 1651161688]; $schema = ['date' => ['date', ['unknown_filter']]]; @@ -882,7 +899,7 @@ public function testErrorsLevel() $input = ['date' => 1651161688]; $schema = ['date' => ['date', ['date', ['Y-m-d H:i'], 0]]]; - $this->assertFailure($schemator, $input, $schema, SchematorException::FILTER_ERROR); + $this->assertSuccess($schemator, $input, $schema, ['date' => null]); $input = ['key' => 1]; $schema = ['my_key' => 'unknown_key']; @@ -900,6 +917,46 @@ public function testErrorsLevel() $schema = ['my_key' => ['key', (object)[]]]; $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE); } + + { + $schemator->setErrorsLevelMask( + ErrorsLevelMask::default() + ->add([SchematorException::CANNOT_GET_VALUE]) + ->sub([SchematorException::FILTER_NOT_FOUND, SchematorException::UNSUPPORTED_SOURCE_TYPE]) + ); + + $input = ['date' => 1651161688]; + $schema = ['date' => ['date', ['unknown_filter']]]; + $this->assertSuccess($schemator, $input, $schema, ['date' => null]); + + $input = ['date' => 1651161688]; + $schema = ['date' => ['date', ['date', ['Y-m-d H:i'], 0]]]; + $this->assertSuccess($schemator, $input, $schema, ['date' => null]); + + $input = ['key' => 1]; + $schema = ['my_key' => 'unknown_key']; + $this->assertFailure($schemator, $input, $schema, SchematorException::CANNOT_GET_VALUE); + + $input = null; + $schema = ['my_key' => 'key']; + $this->assertSuccess($schemator, $input, $schema, ['my_key' => null]); + + $input = ['key' => 1]; + $schema = ['my_key' => (object)[]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_KEY_TYPE); + + $input = ['key' => 1]; + $schema = ['my_key' => ['key', (object)[]]]; + $this->assertFailure($schemator, $input, $schema, SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE); + } + + { + $schemator->setErrorsLevelMask(ErrorsLevelMask::nothing()); + + $input = ['path_key' => 'unknown.path']; + $schema = ['path_value' => ['path_key', ['path']]]; + $this->assertSuccess($schemator, $input, $schema, ['path_value' => null]); + } } protected function assertSuccess(Schemator $schemator, $input, $schema, $value)