diff --git a/README.md b/README.md index d946533..f8f817b 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ These methods enable adding, removing, and modifying elements in the Collection. ``` ```php - $collection ->add('X', 'Y', 'Z'); + $collection->add('X', 'Y', 'Z'); ``` #### Removing elements diff --git a/src/Internal/Operations/Order/Sort.php b/src/Internal/Operations/Order/Sort.php index 0eab8f3..d310dc9 100644 --- a/src/Internal/Operations/Order/Sort.php +++ b/src/Internal/Operations/Order/Sort.php @@ -35,9 +35,7 @@ public function apply(iterable $elements): Generator : $this->predicate; $ascendingPredicate = static fn(mixed $first, mixed $second): int => $predicate($first, $second); - $descendingPredicate = is_null($this->predicate) - ? static fn(mixed $first, mixed $second): int => $predicate($second, $first) - : $predicate; + $descendingPredicate = static fn(mixed $first, mixed $second): int => $predicate($second, $first); match ($this->order) { Order::ASCENDING_KEY => ksort($temporaryElements), diff --git a/src/Internal/Operations/Transform/JoinToString.php b/src/Internal/Operations/Transform/JoinToString.php index 68acb45..441aeff 100644 --- a/src/Internal/Operations/Transform/JoinToString.php +++ b/src/Internal/Operations/Transform/JoinToString.php @@ -19,6 +19,19 @@ public static function from(iterable $elements): JoinToString public function joinTo(string $separator): string { - return implode($separator, iterator_to_array($this->elements)); + $result = ''; + $first = true; + + foreach ($this->elements as $element) { + if ($first) { + $result = $element; + $first = false; + continue; + } + + $result .= sprintf('%s%s', $separator, $element); + } + + return $result; } } diff --git a/tests/Operations/Order/CollectionSortOperationTest.php b/tests/Operations/Order/CollectionSortOperationTest.php index 11c3089..141e8d4 100644 --- a/tests/Operations/Order/CollectionSortOperationTest.php +++ b/tests/Operations/Order/CollectionSortOperationTest.php @@ -77,6 +77,29 @@ public function testSortDescendingByValue(iterable $elements, iterable $expected self::assertSame($expected, $actual->toArray()); } + public function testSortDescendingByValueWithPredicate(): void + { + /** @Given a collection with unordered Amount objects */ + $collection = Collection::createFrom(elements: [ + new Amount(value: 100.50, currency: Currency::USD), + new Amount(value: 150.75, currency: Currency::EUR), + new Amount(value: 200.00, currency: Currency::USD) + ]); + + /** @When sorting the collection in descending order by value with a custom predicate */ + $actual = $collection->sort( + order: Order::DESCENDING_VALUE, + predicate: static fn(Amount $first, Amount $second): int => $first->value <=> $second->value + ); + + /** @Then the collection should be sorted by value in descending order */ + self::assertSame([ + 2 => ['value' => 200.00, 'currency' => Currency::USD->name], + 1 => ['value' => 150.75, 'currency' => Currency::EUR->name], + 0 => ['value' => 100.50, 'currency' => Currency::USD->name] + ], $actual->toArray()); + } + public static function ascendingKeySortDataProvider(): iterable { yield 'Floats ascending by key' => [