|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
* |
|
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
* See the License for the specific language governing permissions and |
|
14
|
|
|
* limitations under the License. |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace KM\Saffron; |
|
17
|
|
|
|
|
18
|
|
|
use KM\Saffron\Exception\EmptyCollection; |
|
19
|
|
|
|
|
20
|
|
|
abstract class Collection extends \ArrayIterator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Returns first element in Collection |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
public function first() |
|
27
|
|
|
{ |
|
28
|
|
|
if (null === ($key = $this->getFirstKey())) { |
|
29
|
|
|
throw new EmptyCollection('You cannot fetch first element of empty collection.'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this[$key]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns first key in Collection |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function getFirstKey() |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($this as $key => $value) { |
|
42
|
|
|
return $key; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns keys of elements in Collection |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getKeys() |
|
52
|
|
|
{ |
|
53
|
|
|
$keys = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($this as $key => $value) { |
|
56
|
|
|
$keys[] = $key; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $keys; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Creates nested Collection for each group. |
|
64
|
|
|
* $func closure needs to return unique value for each group. |
|
65
|
|
|
* @param \Closure $func |
|
66
|
|
|
* @return Collection |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function groupBy(\Closure $func) |
|
69
|
|
|
{ |
|
70
|
|
|
$class = get_called_class(); |
|
71
|
|
|
|
|
72
|
|
|
$result = new $class; |
|
73
|
|
|
foreach ($this as $item) { |
|
74
|
|
|
$key = $func($item); |
|
75
|
|
|
|
|
76
|
|
|
if (!isset($result[$key])) { |
|
77
|
|
|
$result[$key] = new $class; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$result[$key]->append($item); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Checks whether Collection has something. |
|
88
|
|
|
* Based on value returned by $func Closure. |
|
89
|
|
|
* @param \Closure $func |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function has(\Closure $func) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this as $item) { |
|
95
|
|
|
if ($func($item)) { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|