|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Collections\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Collections\AbstractCollection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class OperationsTrait |
|
9
|
|
|
* @package Nip\Collections\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait OperationsTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the number of parameters. |
|
15
|
|
|
* |
|
16
|
|
|
* @return int The number of parameters |
|
17
|
33 |
|
*/ |
|
18
|
|
|
public function count() |
|
19
|
33 |
|
{ |
|
20
|
|
|
return count($this->items); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns number of items in $collection. |
|
25
|
|
|
* |
|
26
|
|
|
* @return int |
|
27
|
|
|
*/ |
|
28
|
|
|
public function size() |
|
29
|
|
|
{ |
|
30
|
|
|
$result = 0; |
|
31
|
|
|
foreach ($this as $value) { |
|
32
|
|
|
$result++; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $result; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function isEmpty(): bool |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->count() < 1; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function isNotEmpty(): bool |
|
50
|
|
|
{ |
|
51
|
|
|
return !$this->isEmpty(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function clear(): void |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->rewind(); |
|
57
|
|
|
$this->items = []; |
|
|
|
|
|
|
58
|
1 |
|
} |
|
59
|
1 |
|
|
|
60
|
|
|
/** |
|
61
|
1 |
|
* Key an associative array by a field or using a callback. |
|
62
|
|
|
* |
|
63
|
|
|
* @param callable|string $keyBy |
|
64
|
|
|
* @return static |
|
65
|
|
|
*/ |
|
66
|
|
|
public function keyBy($keyBy) |
|
67
|
|
|
{ |
|
68
|
|
|
$results = []; |
|
69
|
|
|
foreach ($this->items as $key => $item) { |
|
70
|
1 |
|
$resolvedKey = is_object($item) ? $item->{$keyBy} : $item[$keyBy]; |
|
71
|
|
|
$results[$resolvedKey] = $item; |
|
72
|
1 |
|
} |
|
73
|
1 |
|
return new static($results); |
|
|
|
|
|
|
74
|
1 |
|
} |
|
75
|
1 |
|
|
|
76
|
|
|
/** |
|
77
|
1 |
|
* Group an associative array by a field or using a callback. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array|callable|string $groupBy |
|
80
|
|
|
* @param bool $preserveKeys |
|
81
|
|
|
* @return static |
|
82
|
|
|
*/ |
|
83
|
|
|
public function groupBy($groupBy, $preserveKeys = false): AbstractCollection |
|
84
|
|
|
{ |
|
85
|
|
|
$results = []; |
|
86
|
|
|
|
|
87
|
|
|
$groupBy = $this->valueRetriever($groupBy); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
foreach ($this->items as $key => $value) { |
|
90
|
|
|
$groupKeys = $groupBy($value, $key); |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if (!is_array($groupKeys)) { |
|
94
|
|
|
$groupKeys = [$groupKeys]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
foreach ($groupKeys as $groupKey) { |
|
98
|
|
|
$groupKey = is_bool($groupKey) ? (int)$groupKey : $groupKey; |
|
99
|
|
|
|
|
100
|
|
|
if (!array_key_exists($groupKey, $results)) { |
|
101
|
|
|
$results[$groupKey] = new static; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$result = new static($results); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
return $result; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
abstract public function rewind(); |
|
117
|
|
|
} |
|
118
|
|
|
|