Skip to content

Commit

Permalink
Apply setters to the filter data (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Mar 14, 2023
1 parent 2f4db84 commit ffb8000
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Model/FilterProvider.php
Expand Up @@ -33,7 +33,7 @@ public function createFilter(string $name, InputInterface $input): FilterInterfa
\assert($attributeMapper instanceof AttributeMapper);

$filter = $this->createFilterInstance($name);
[$mappingSchema, $errors] = $attributeMapper->map($filter, $input);
[$mappingSchema, $errors, $setters] = $attributeMapper->map($filter, $input);

if ($filter instanceof HasFilterDefinition) {
$mappingSchema = \array_merge(
Expand All @@ -50,7 +50,7 @@ public function createFilter(string $name, InputInterface $input): FilterInterfa

$schema = $schemaBuilder->makeSchema($name, $mappingSchema);

[$data, $inputErrors] = $inputMapper->map($schema, $input);
[$data, $inputErrors] = $inputMapper->map($schema, $input, $setters);
$errors = \array_merge($errors, $inputErrors);

$entity = new SchematicEntity($data, $schema);
Expand Down
7 changes: 5 additions & 2 deletions src/Model/Schema/AttributeMapper.php
Expand Up @@ -28,12 +28,13 @@ public function __construct(
/**
* Map input data into filter properties with attributes
*
* @return array{0: array, 1: array}
* @return array{0: array, 1: array, 2: array}
*/
public function map(FilterInterface $filter, InputInterface $input): array
{
$errors = [];
$schema = [];
$setters = [];
$class = new \ReflectionClass($filter);

foreach ($class->getProperties() as $property) {
Expand Down Expand Up @@ -76,11 +77,13 @@ public function map(FilterInterface $filter, InputInterface $input): array

$this->setValue($filter, $property, $propertyValues);
$schema[$property->getName()] = [$attribute->class, $prefix . '.*'];
} elseif ($attribute instanceof Setter) {
$setters[$property->getName()][] = $attribute;
}
}
}

return [$schema, $errors];
return [$schema, $errors, $setters];
}

private function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void
Expand Down
8 changes: 7 additions & 1 deletion src/Model/Schema/InputMapper.php
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Filters\Model\Schema;

use Spiral\Filters\Attribute\Setter;
use Spiral\Filters\Model\FilterProviderInterface;
use Spiral\Filters\Exception\ValidationException;
use Spiral\Filters\InputInterface;
Expand All @@ -15,7 +16,7 @@ public function __construct(
) {
}

public function map(array $mappingSchema, InputInterface $input): array
public function map(array $mappingSchema, InputInterface $input, array $setters = []): array
{
$errors = [];
$result = [];
Expand All @@ -25,6 +26,11 @@ public function map(array $mappingSchema, InputInterface $input): array
$value = $input->getValue($map[Builder::SCHEMA_SOURCE], $map[Builder::SCHEMA_ORIGIN]);

if ($value !== null) {
/** @var Setter $setter */
foreach ($setters[$field] ?? [] as $setter) {
$value = $setter->updateValue($value);
}

$result[$field] = $value;
}
continue;
Expand Down

0 comments on commit ffb8000

Please sign in to comment.