-
Notifications
You must be signed in to change notification settings - Fork 3
Replace Flat hydrator with PrestyledAssoc for alias-keyed rows #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Data\Hydrators; | ||
|
|
||
| use DomainException; | ||
| use Respect\Data\CollectionIterator; | ||
| use Respect\Data\Collections\Collection; | ||
| use Respect\Data\Collections\Composite; | ||
| use Respect\Data\Collections\Filtered; | ||
| use Respect\Data\EntityFactory; | ||
| use SplObjectStorage; | ||
|
|
||
| use function array_keys; | ||
| use function explode; | ||
| use function is_array; | ||
|
|
||
| /** | ||
| * Hydrates associative rows whose keys are pre-styled as `specifier__styledProp`. | ||
| * | ||
| * This hydrator groups columns by their specifier prefix and | ||
| * maps them directly to entities — no reverse iteration, boundary detection, | ||
| * or entity stack needed. | ||
| */ | ||
| final class PrestyledAssoc extends Base | ||
| { | ||
| /** @var array<string, Collection> */ | ||
| private array $collMap = []; | ||
|
|
||
| private Collection|null $cachedCollection = null; | ||
|
|
||
| /** @return SplObjectStorage<object, Collection>|false */ | ||
| public function hydrate( | ||
| mixed $raw, | ||
| Collection $collection, | ||
| EntityFactory $entityFactory, | ||
| ): SplObjectStorage|false { | ||
| if (!$raw || !is_array($raw)) { | ||
| return false; | ||
| } | ||
|
|
||
| $collMap = $this->buildCollMap($collection); | ||
|
|
||
| /** @var array<string, array<string, mixed>> $grouped */ | ||
| $grouped = []; | ||
| foreach ($raw as $alias => $value) { | ||
| [$prefix, $prop] = explode('__', $alias, 2); | ||
| $grouped[$prefix][$prop] = $value; | ||
| } | ||
|
|
||
| /** @var SplObjectStorage<object, Collection> $entities */ | ||
| $entities = new SplObjectStorage(); | ||
| /** @var array<string, object> $instances */ | ||
| $instances = []; | ||
|
|
||
| foreach ($grouped as $prefix => $props) { | ||
| $basePrefix = $this->resolveCompositionBase($prefix, $collMap); | ||
|
|
||
| if (!isset($instances[$basePrefix])) { | ||
| $coll = $collMap[$basePrefix]; | ||
| $class = $this->resolveEntityClass($coll, $entityFactory, $props); | ||
| $instances[$basePrefix] = $entityFactory->create($class); | ||
| $entities[$instances[$basePrefix]] = $coll; | ||
| } | ||
|
|
||
| $entity = $instances[$basePrefix]; | ||
| foreach ($props as $prop => $value) { | ||
| $entityFactory->set($entity, $prop, $value, styled: true); | ||
| } | ||
| } | ||
|
|
||
| if ($entities->count() > 1) { | ||
| $this->wireRelationships($entities, $entityFactory); | ||
| } | ||
|
|
||
| return $entities; | ||
| } | ||
|
|
||
| /** @return array<string, Collection> */ | ||
| private function buildCollMap(Collection $collection): array | ||
| { | ||
| if ($this->cachedCollection === $collection) { | ||
| return $this->collMap; | ||
| } | ||
|
|
||
| $this->collMap = []; | ||
| foreach (CollectionIterator::recursive($collection) as $spec => $c) { | ||
| if ($c->name === null || ($c instanceof Filtered && !$c->filters)) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->collMap[$spec] = $c; | ||
| } | ||
|
|
||
| $this->cachedCollection = $collection; | ||
|
|
||
| return $this->collMap; | ||
alganet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Resolve a composition prefix back to its base entity specifier. | ||
| * | ||
| * Composition columns use prefixes like "post_WITH_comment" (see Composite::COMPOSITION_MARKER). | ||
| * This returns "post" so properties are merged into the parent entity. | ||
| * | ||
| * @param array<string, Collection> $collMap | ||
| */ | ||
| private function resolveCompositionBase(string $prefix, array $collMap): string | ||
| { | ||
| if (isset($collMap[$prefix])) { | ||
| return $prefix; | ||
| } | ||
|
|
||
| // Look for a base specifier where this prefix is a composition alias | ||
| foreach ($collMap as $spec => $coll) { | ||
| if (!$coll instanceof Composite) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach (array_keys($coll->compositions) as $compName) { | ||
| if ($prefix === $spec . Composite::COMPOSITION_MARKER . $compName) { | ||
| return $spec; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new DomainException('Unknown column prefix "' . $prefix . '" in hydration row'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.