Skip to content

Commit

Permalink
Filter 'select' extended with path functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Nov 16, 2023
1 parent 93030ac commit 89d1768
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Components/Schemator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public function setPathDelimiter(string $value): void
$this->pathDelimiter = $value;
}

/**
* @inheritDoc
*/
public function getPathDelimiter(): string
{
return $this->pathDelimiter;
}

/**
* @inheritDoc
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Filters/BaseFiltersStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Smoren\Schemator\Helpers\RuleHelper;
use Smoren\Schemator\Interfaces\FilterContextInterface;
use Smoren\Schemator\Interfaces\FiltersStorageInterface;
use Smoren\Schemator\Structs\FilterContext;

/**
* Class BaseFiltersStorage
Expand Down Expand Up @@ -56,9 +57,11 @@ public static function select(FilterContextInterface $context, ...$args): array
}

$result = [];
$pathDelimiter = $context->getSchemator()->getPathDelimiter();

/** @var string $arg */
foreach ($args as $arg) {
$result[$arg] = $source[$arg] ?? null;
$result[str_replace($pathDelimiter, '_', $arg)] = $context->getSchemator()->getValue($source, $arg);
}

return $result;
Expand Down
6 changes: 6 additions & 0 deletions src/Interfaces/SchematorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public function getValue($source, $key);
*/
public function setPathDelimiter(string $value): void;

/**
* Getter for pathDelimiter property
* @return string
*/
public function getPathDelimiter(): string;

/**
* Setter for errorsLevelMask property
* @param BitmapInterface $value new value
Expand Down
56 changes: 53 additions & 3 deletions tests/unit/Schemator/SchematorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,16 @@ public function testGetValueWithFilters()
$this->assertNull($schemator->getValue($input, ['mynull', ['replace', [[0, '<=', 122]]]]));
}

public function testFilterSelect()
public function testFilterSelectStrict()
{
$source = [
'id' => 100,
'name' => 'Test',
'description' => 'A lot of text',
'city' => [
'id' => 10,
'name' => 'London',
],
];

$schemator = SchematorFactory::createBuilder()
Expand All @@ -840,8 +844,22 @@ public function testFilterSelect()
$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);
$result = $schemator->convert($source, ['test' => [null, ['select', 'id', 'name', 'city.name']]]);
$this->assertEquals(['test' => ['id' => 100, 'name' => 'Test', 'city_name' => 'London']], $result);

try {
$schemator->convert($source, ['test' => [null, ['select', 'id', 'unknown']]]);
$this->assertTrue(false);
} catch (SchematorException $e) {
$this->assertEquals(SchematorException::CANNOT_GET_VALUE, $e->getCode());
}

try {
$schemator->convert($source, ['test' => [null, ['select', 'id', 'city.unknown']]]);
$this->assertTrue(false);
} catch (SchematorException $e) {
$this->assertEquals(SchematorException::CANNOT_GET_VALUE, $e->getCode());
}

try {
$schemator->convert($source, ['test' => ['id', ['select', 'id', 'unknown']]]);
Expand All @@ -861,6 +879,38 @@ public function testFilterSelect()
$this->assertEquals('100: Test', $result);
}

public function testFilterSelectNonStrict()
{
$source = [
'id' => 100,
'name' => 'Test',
'description' => 'A lot of text',
];

$schemator = SchematorFactory::createBuilder()
->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);

$result = $schemator->convert($source, ['test' => [null, ['select', 'id', 'city.unknown']]]);
$this->assertEquals(['test' => ['id' => 100, 'city_unknown' => null]], $result);

$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 89d1768

Please sign in to comment.