Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TraitCollectionTransformation
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 slice
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 take
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 reverse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unique
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Domain\Collection\Trait;
11
12trait TraitCollectionTransformation
13{
14    public function slice(int $offset, ?int $length = null): static
15    {
16        return static::fromArray(array_slice($this->collection, $offset, $length, preserve_keys: true));
17    }
18
19    public function take(int $limit): static
20    {
21        return $this->slice(0, $limit);
22    }
23
24    public function sort(callable $comparator): static
25    {
26        $sorted = $this->collection;
27
28        uasort($sorted, $comparator);
29
30        return static::fromArray($sorted);
31    }
32
33    public function reverse(): static
34    {
35        return static::fromArray(array_reverse($this->collection, preserve_keys: true));
36    }
37
38    public function unique(): static
39    {
40        return static::fromArray(array_unique($this->collection, SORT_REGULAR));
41    }
42}