Skip to content

Commit ccf5e8b

Browse files
authored
Release/1.13.1 (#35)
1 parent 8edeea4 commit ccf5e8b

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ These methods enable adding, removing, and modifying elements in the Collection.
9191
```
9292

9393
```php
94-
$collection ->add('X', 'Y', 'Z');
94+
$collection->add('X', 'Y', 'Z');
9595
```
9696

9797
#### Removing elements

src/Internal/Operations/Order/Sort.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public function apply(iterable $elements): Generator
3535
: $this->predicate;
3636

3737
$ascendingPredicate = static fn(mixed $first, mixed $second): int => $predicate($first, $second);
38-
$descendingPredicate = is_null($this->predicate)
39-
? static fn(mixed $first, mixed $second): int => $predicate($second, $first)
40-
: $predicate;
38+
$descendingPredicate = static fn(mixed $first, mixed $second): int => $predicate($second, $first);
4139

4240
match ($this->order) {
4341
Order::ASCENDING_KEY => ksort($temporaryElements),

src/Internal/Operations/Transform/JoinToString.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ public static function from(iterable $elements): JoinToString
1919

2020
public function joinTo(string $separator): string
2121
{
22-
return implode($separator, iterator_to_array($this->elements));
22+
$result = '';
23+
$first = true;
24+
25+
foreach ($this->elements as $element) {
26+
if ($first) {
27+
$result = $element;
28+
$first = false;
29+
continue;
30+
}
31+
32+
$result .= sprintf('%s%s', $separator, $element);
33+
}
34+
35+
return $result;
2336
}
2437
}

tests/Operations/Order/CollectionSortOperationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ public function testSortDescendingByValue(iterable $elements, iterable $expected
7777
self::assertSame($expected, $actual->toArray());
7878
}
7979

80+
public function testSortDescendingByValueWithPredicate(): void
81+
{
82+
/** @Given a collection with unordered Amount objects */
83+
$collection = Collection::createFrom(elements: [
84+
new Amount(value: 100.50, currency: Currency::USD),
85+
new Amount(value: 150.75, currency: Currency::EUR),
86+
new Amount(value: 200.00, currency: Currency::USD)
87+
]);
88+
89+
/** @When sorting the collection in descending order by value with a custom predicate */
90+
$actual = $collection->sort(
91+
order: Order::DESCENDING_VALUE,
92+
predicate: static fn(Amount $first, Amount $second): int => $first->value <=> $second->value
93+
);
94+
95+
/** @Then the collection should be sorted by value in descending order */
96+
self::assertSame([
97+
2 => ['value' => 200.00, 'currency' => Currency::USD->name],
98+
1 => ['value' => 150.75, 'currency' => Currency::EUR->name],
99+
0 => ['value' => 100.50, 'currency' => Currency::USD->name]
100+
], $actual->toArray());
101+
}
102+
80103
public static function ascendingKeySortDataProvider(): iterable
81104
{
82105
yield 'Floats ascending by key' => [

0 commit comments

Comments
 (0)