Skip to content

Commit

Permalink
Simplified HydratorFactory get
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Dec 13, 2023
1 parent c12b5d1 commit 0a5e7fd
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Laminas\Hydrator\Filter;
use Laminas\Hydrator\NamingStrategy\NamingStrategyEnabledInterface;
use Laminas\Hydrator\NamingStrategy\NamingStrategyInterface;
use Laminas\Hydrator\Strategy\StrategyEnabledInterface;
use Laminas\Hydrator\Strategy\StrategyInterface;

use function assert;
Expand Down Expand Up @@ -52,30 +51,26 @@ public function get(string $id): mixed
$hydrator = new DoctrineObject($this->entityManager, $config['byValue']);

// Create field strategy and assign to hydrator
if ($hydrator instanceof StrategyEnabledInterface) {
foreach ($config['fields'] as $fieldName => $fieldMetadata) {
assert(
in_array(StrategyInterface::class, class_implements($fieldMetadata['hydratorStrategy'])),
'Strategy must implement ' . StrategyInterface::class,
);

$hydrator->addStrategy($fieldName, $this->get($fieldMetadata['hydratorStrategy']));
}
foreach ($config['fields'] as $fieldName => $fieldMetadata) {
assert(
in_array(StrategyInterface::class, class_implements($fieldMetadata['hydratorStrategy'])),
'Strategy must implement ' . StrategyInterface::class,
);

$hydrator->addStrategy($fieldName, $this->get($fieldMetadata['hydratorStrategy']));
}

// Create filters and assign to hydrator
if ($hydrator instanceof Filter\FilterEnabledInterface) {
foreach ($config['hydratorFilters'] as $name => $filterConfig) {
// Default filters to AND
$condition = $filterConfig['condition'] ?? Filter\FilterComposite::CONDITION_AND;
$filterClass = $filterConfig['filter'];
assert(
in_array(Filter\FilterInterface::class, class_implements($filterClass)),
'Filter must implement ' . StrategyInterface::class,
);

$hydrator->addFilter($name, $this->get($filterClass), $condition);
}
foreach ($config['hydratorFilters'] as $name => $filterConfig) {
// Default filters to AND
$condition = $filterConfig['condition'] ?? Filter\FilterComposite::CONDITION_AND;
$filterClass = $filterConfig['filter'];
assert(
in_array(Filter\FilterInterface::class, class_implements($filterClass)),
'Filter must implement ' . StrategyInterface::class,
);

$hydrator->addFilter($name, $this->get($filterClass), $condition);
}

// Create naming strategy and assign to hydrator
Expand Down

0 comments on commit 0a5e7fd

Please sign in to comment.