From 93030ac4fc181041e6140cf811115134548a9864 Mon Sep 17 00:00:00 2001 From: Smoren Date: Thu, 16 Nov 2023 16:36:51 +0300 Subject: [PATCH] Filter 'select' added. --- src/Components/Schemator.php | 4 +-- src/Filters/BaseFiltersStorage.php | 25 +++++++++++++++++ tests/unit/Schemator/SchematorTest.php | 37 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/Components/Schemator.php b/src/Components/Schemator.php index 34594a3..56a0d70 100644 --- a/src/Components/Schemator.php +++ b/src/Components/Schemator.php @@ -149,7 +149,7 @@ protected function getValueByKey($source, string $key) /** * Returns value got by filters key * @param array|object $source source to get value from - * @param array> $filters filters config + * @param array|null> $filters filters config * @return mixed|null * @throws SchematorException */ @@ -157,7 +157,7 @@ protected function getValueByFilters($source, array $filters) { $result = $source; foreach ($filters as $filterConfig) { - if (is_string($filterConfig)) { + if (is_string($filterConfig) || $filterConfig === null) { $result = $this->getValue($result, $filterConfig); } elseif (is_array($filterConfig)) { $result = $this->runFilter($filterConfig, $result, $source); diff --git a/src/Filters/BaseFiltersStorage.php b/src/Filters/BaseFiltersStorage.php index 4a13905..eec3569 100644 --- a/src/Filters/BaseFiltersStorage.php +++ b/src/Filters/BaseFiltersStorage.php @@ -40,6 +40,30 @@ public static function format(FilterContextInterface $context, callable $formatt return $formatter($context->getSource(), ...$args); } + /** + * Selects values from array by keys list + * @param FilterContextInterface $context filter context + * @param mixed ...$args formatter callback's arguments + * @return array + * @throws SchematorException + */ + public static function select(FilterContextInterface $context, ...$args): array + { + $source = $context->getSource(); + + if (!is_array($source)) { + throw SchematorException::createAsBadFilterSource($context); + } + + $result = []; + + foreach ($args as $arg) { + $result[$arg] = $source[$arg] ?? null; + } + + return $result; + } + /** * Returns formatted date from timestamp * @param FilterContextInterface $context filter context @@ -319,6 +343,7 @@ protected function _get(): array return [ 'const' => [$this, 'const'], 'format' => [$this, 'format'], + 'select' => [$this, 'select'], 'date' => [$this, 'date'], 'implode' => [$this, 'implode'], 'explode' => [$this, 'explode'], diff --git a/tests/unit/Schemator/SchematorTest.php b/tests/unit/Schemator/SchematorTest.php index 8543cd1..cac7ea9 100644 --- a/tests/unit/Schemator/SchematorTest.php +++ b/tests/unit/Schemator/SchematorTest.php @@ -824,6 +824,43 @@ public function testGetValueWithFilters() $this->assertNull($schemator->getValue($input, ['mynull', ['replace', [[0, '<=', 122]]]])); } + public function testFilterSelect() + { + $source = [ + 'id' => 100, + 'name' => 'Test', + 'description' => 'A lot of text', + ]; + + $schemator = SchematorFactory::createBuilder() + ->withErrorsLevelMask(ErrorsLevelMask::all()) + ->withFilters(new BaseFiltersStorage()) + ->get(); + + $result = $schemator->convert($source, ['test' => [null, ['select', 'id', 'name']]]); + $this->assertEquals(['test' => ['id' => 100, 'name' => 'Test']], $result); + + $result = $schemator->convert($source, ['test' => [null, ['select', 'id', 'unknown']]]); + $this->assertEquals(['test' => ['id' => 100, 'unknown' => null]], $result); + + try { + $schemator->convert($source, ['test' => ['id', ['select', 'id', 'unknown']]]); + $this->assertTrue(false); + } catch (SchematorException $e) { + $this->assertEquals(SchematorException::BAD_FILTER_SOURCE, $e->getCode()); + } + + $result = $schemator->convert($source, [ + null => [ + null, + ['select', 'id', 'name'], + ['implode', ': '], + ] + ]); + + $this->assertEquals('100: Test', $result); + } + public function testErrorsLevel() { $schemator = SchematorFactory::create();