diff --git a/composer.lock b/composer.lock index 8c127ac..c656af2 100644 --- a/composer.lock +++ b/composer.lock @@ -4345,16 +4345,16 @@ }, { "name": "symfony/property-access", - "version": "v7.0.0", + "version": "v7.0.8", "source": { "type": "git", "url": "https://github.com/symfony/property-access.git", - "reference": "740e8cb8c54a4f16c82179e8558c29d9fc49901d" + "reference": "ca11e9661ea88664873dba66412525fe301dd744" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/property-access/zipball/740e8cb8c54a4f16c82179e8558c29d9fc49901d", - "reference": "740e8cb8c54a4f16c82179e8558c29d9fc49901d", + "url": "https://api.github.com/repos/symfony/property-access/zipball/ca11e9661ea88664873dba66412525fe301dd744", + "reference": "ca11e9661ea88664873dba66412525fe301dd744", "shasum": "" }, "require": { @@ -4401,7 +4401,7 @@ "reflection" ], "support": { - "source": "https://github.com/symfony/property-access/tree/v7.0.0" + "source": "https://github.com/symfony/property-access/tree/v7.0.8" }, "funding": [ { @@ -4417,7 +4417,7 @@ "type": "tidelift" } ], - "time": "2023-09-27T14:05:33+00:00" + "time": "2024-05-31T14:55:39+00:00" }, { "name": "symfony/property-info", @@ -9840,7 +9840,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { @@ -9848,6 +9848,6 @@ "ext-ctype": "*", "ext-iconv": "*" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php b/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php index 9afacd5..2b81596 100644 --- a/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php +++ b/src/BookStore/Infrastructure/InMemory/InMemoryBookRepository.php @@ -37,9 +37,6 @@ public function withAuthor(Author $author): static public function withCheapestsFirst(): static { - $cloned = clone $this; - uasort($cloned->entities, fn (Book $a, Book $b) => $a->price() <=> $b->price()); - - return $cloned; + return $this->sort(['price' => 'asc']); } } diff --git a/src/Shared/Infrastructure/InMemory/InMemoryRepository.php b/src/Shared/Infrastructure/InMemory/InMemoryRepository.php index 094ca63..1f55042 100644 --- a/src/Shared/Infrastructure/InMemory/InMemoryRepository.php +++ b/src/Shared/Infrastructure/InMemory/InMemoryRepository.php @@ -6,6 +6,7 @@ use App\Shared\Domain\Repository\PaginatorInterface; use App\Shared\Domain\Repository\RepositoryInterface; +use Symfony\Component\PropertyAccess\PropertyAccess; use Webmozart\Assert\Assert; /** @@ -90,4 +91,31 @@ protected function filter(callable $filter): static return $cloned; } + + /** + * @param array $criteria + * @return $this + */ + protected function sort(array $criteria): static + { + $cloned = clone $this; + + $accessor = PropertyAccess::createPropertyAccessor(); + + uasort($cloned->entities, function (object $a, object $b) use ($criteria, $accessor): int { + foreach ($criteria as $field => $direction) { + $aValue = $accessor->getValue($a, $field); + $bValue = $accessor->getValue($b, $field); + + $comparison = $aValue <=> $bValue; + if (0 !== $comparison) { + return 'asc' === $direction ? $comparison : -$comparison; + } + } + + return 0; + }); + + return $cloned; + } } diff --git a/symfony.lock b/symfony.lock index 6506093..858ed17 100644 --- a/symfony.lock +++ b/symfony.lock @@ -12,6 +12,15 @@ "src/ApiResource/.gitignore" ] }, + "doctrine/deprecations": { + "version": "1.1", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "1.0", + "ref": "87424683adc81d7dc305eefec1fced883084aab9" + } + }, "doctrine/doctrine-bundle": { "version": "2.9", "recipe": {