Skip to content

Commit

Permalink
fix cross matching on an aggregate or entity property
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Oct 31, 2024
1 parent bade44f commit cdaebd6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- Cross matching on an aggregate or entity property.

## 3.4.1 - 2024-10-26

### Changed
Expand Down
29 changes: 29 additions & 0 deletions properties/CrossAggregateMatching.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,35 @@ static function() use ($repository, $child1, $child2, $parent1, $parent2) {

$assert->count(0, $found);

// this allows to check the cross match on aggregate properties
$found = $repository
->matching(Comparator\Property::of(
'id',
Sign::in,
$repository->matching(
Comparator\Property::of('id', Sign::equality, $child1->id())->or(
Comparator\Property::of('id', Sign::equality, $parent2->id()),
),
),
))
->map(static fn($user) => $user->id()->toString())
->toList();

$assert
->expected($child1->id()->toString())
->in($found);
$assert
->expected($parent2->id()->toString())
->in($found);
$assert
->expected($child2->id()->toString())
->not()
->in($found);
$assert
->expected($parent1->id()->toString())
->not()
->in($found);

return $manager;
}
}
22 changes: 22 additions & 0 deletions src/Adapter/SQL/MainTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Specification\Child,
Specification\Just,
Specification\Has,
Specification\CrossMatch,
};
use Formal\AccessLayer\{
Table,
Expand All @@ -27,6 +28,7 @@
use Innmind\Specification\{
Specification,
Not,
Comparator,
Composite,
Operator,
Sign,
Expand Down Expand Up @@ -404,6 +406,14 @@ private function where(Specification $specification): Specification
);
}

if ($specification instanceof CrossMatch) {
return Comparator\Property::of(
'entity.'.$specification->property(),
$specification->sign(),
$specification->value(),
);
}

if (!($specification instanceof Property)) {
$class = $specification::class;

Expand Down Expand Up @@ -446,6 +456,18 @@ private function whereEntity(Entity $specification): Specification
};
}

if ($underlying instanceof CrossMatch) {
return Comparator\Property::of(
\sprintf(
'%s.%s',
$specification->entity(),
$underlying->property(),
),
$underlying->sign(),
$underlying->value(),
);
}

if (!($underlying instanceof Property)) {
$class = $underlying::class;

Expand Down

0 comments on commit cdaebd6

Please sign in to comment.