|
1
|
|
|
<?php |
|
2
|
|
|
namespace TRex\Collection; |
|
3
|
|
|
|
|
4
|
|
|
trait CollectionSorterTrait |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* Reindex the keys |
|
8
|
|
|
* |
|
9
|
|
|
* @return $this |
|
|
|
|
|
|
10
|
|
|
*/ |
|
11
|
1 |
|
public function reindex() |
|
12
|
|
|
{ |
|
13
|
1 |
|
return new $this(array_values((array)$this)); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param callable $callback |
|
18
|
|
|
* @return $this |
|
|
|
|
|
|
19
|
|
|
* @deprecated |
|
20
|
|
|
*/ |
|
21
|
|
|
public function sort(callable $callback) |
|
22
|
|
|
{ |
|
23
|
|
|
$collection = (array)$this; |
|
24
|
|
|
uasort($collection, $callback); |
|
25
|
|
|
return new $this($collection); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Reverses the values |
|
30
|
|
|
* |
|
31
|
|
|
* @param bool $areKeysPreserved |
|
32
|
|
|
* @return $this |
|
|
|
|
|
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function reverse($areKeysPreserved = true) |
|
35
|
|
|
{ |
|
36
|
1 |
|
return new $this(array_reverse((array)$this, $areKeysPreserved)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Shuffles the values |
|
41
|
|
|
* |
|
42
|
|
|
* @param bool $areKeysPreserved |
|
43
|
|
|
* @return $this |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function shuffle($areKeysPreserved = true) |
|
46
|
|
|
{ |
|
47
|
2 |
|
if($areKeysPreserved){ |
|
48
|
1 |
|
return $this->sort(function(){ |
|
|
|
|
|
|
49
|
1 |
|
return mt_rand(-1, 1); |
|
50
|
1 |
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$collection = (array)$this; |
|
54
|
1 |
|
shuffle($collection); |
|
55
|
1 |
|
return new $this($collection); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Splits the values into every possible response returned by the callback. |
|
60
|
|
|
* |
|
61
|
|
|
* For example, if the callback juste return the value: |
|
62
|
|
|
* ['a', 'b', 'c'] |
|
63
|
|
|
* will become: |
|
64
|
|
|
* ['a' => ['a'], 'b' => ['b'], 'c' => ['c']] |
|
65
|
|
|
* |
|
66
|
|
|
* If the callback return a boolean, it will be return an array with two collections. |
|
67
|
|
|
* [0 => $nonMatchedCollection, 1 => $matchedCollection] |
|
68
|
|
|
* This seems to be quite the same as Doctrine's ARrayCollection. But be careful because keys are inverted. |
|
69
|
|
|
* |
|
70
|
|
|
* @param callable $callback |
|
71
|
|
|
* @return $this[] |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function groupBy(callable $callback) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$results = []; |
|
76
|
1 |
|
$collection = new Collection($this); |
|
77
|
1 |
|
foreach ($collection->each($callback) as $key => $result) { |
|
78
|
1 |
|
if (!isset($results[$result])) { |
|
79
|
1 |
|
$results[$result] = new $this(); |
|
80
|
1 |
|
} |
|
81
|
1 |
|
$results[$result][$key] = $this[$key]; |
|
82
|
1 |
|
} |
|
83
|
1 |
|
return $results; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.