Completed
Push — master ( 9d0a0b...952be3 )
by Raphaël
11:37 queued 09:42
created

CollectionSorterTrait::reindex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace TRex\Collection;
3
4
trait CollectionSorterTrait
5
{
6
    /**
7
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
8
     */
9
    public function reindex()
10
    {
11
        return new $this(array_values((array)$this));
12
    }
13
14
    /**
15
     * @param callable $callback
16
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
17
     */
18
    public function sort(callable $callback)
19
    {
20
        $collection = (array)$this;
21
        uasort($collection, $callback);
22
        return new $this($collection);
23
    }
24
25
    /**
26
     * @param callable $callback
27
     * @return $this[]
28
     */
29
    public function groupBy(callable $callback)
30
    {
31
        $results = [];
32
        $collection = new Collection($this);
33
        foreach ($collection->each($callback) as $key => $result) {
34
            if (!isset($results[$result])) {
35
                $results[$result] = new $this();
36
            }
37
            $results[$result][$key] = $this[$key];
38
        }
39
        return $results;
40
    }
41
}
42