Skip to content

Commit

Permalink
Filter 'select' added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Nov 16, 2023
1 parent c29b543 commit 93030ac
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Components/Schemator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ protected function getValueByKey($source, string $key)
/**
* Returns value got by filters key
* @param array<string, mixed>|object $source source to get value from
* @param array<int, string|array<int, mixed>> $filters filters config
* @param array<int, string|array<int, mixed>|null> $filters filters config
* @return mixed|null
* @throws SchematorException
*/
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);
Expand Down
25 changes: 25 additions & 0 deletions src/Filters/BaseFiltersStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
* @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
Expand Down Expand Up @@ -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'],
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/Schemator/SchematorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 93030ac

Please sign in to comment.