1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpEarth\Stats; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Collection. |
7
|
|
|
*/ |
8
|
|
|
class Collection implements \Countable, \Iterator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $data = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Adds object to collection with key. |
17
|
|
|
* |
18
|
|
|
* @param $obj |
19
|
|
|
* @param null $key |
|
|
|
|
20
|
|
|
* |
21
|
|
|
* @throws \Exception |
22
|
|
|
*/ |
23
|
|
|
public function add($obj, $key = null) |
24
|
|
|
{ |
25
|
|
|
if ($key === null) { |
26
|
|
|
$this->data[] = $obj; |
27
|
|
|
} else { |
28
|
|
|
unset($this->data[$key]); |
29
|
|
|
$this->data[$key] = $obj; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Deletes object from collection by key. |
35
|
|
|
* |
36
|
|
|
* @param $key |
37
|
|
|
* |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function delete($key) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (isset($this->data[$key])) { |
43
|
|
|
unset($this->data[$key]); |
44
|
|
|
} else { |
45
|
|
|
throw new \Exception("Invalid key $key."); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns object from collection. |
51
|
|
|
* |
52
|
|
|
* @param $key |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
* |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (isset($this->data[$key])) { |
61
|
|
|
return $this->data[$key]; |
62
|
|
|
} else { |
63
|
|
|
throw new \Exception("Invalid key $key."); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Checks if object with given key exists in collection. |
69
|
|
|
* |
70
|
|
|
* @param $key |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function keyExists($key) |
75
|
|
|
{ |
76
|
|
|
return isset($this->data[$key]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns number of objects in collection. |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function count() |
85
|
|
|
{ |
86
|
|
|
return count($this->data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Rewinds the internal pointer of an array to its first object of collection. |
91
|
|
|
*/ |
92
|
|
|
public function rewind() |
93
|
|
|
{ |
94
|
|
|
reset($this->data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the current object in a collection. |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function current() |
103
|
|
|
{ |
104
|
|
|
$data = current($this->data); |
105
|
|
|
|
106
|
|
|
return $data; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Fetch a key from an collection. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function key() |
115
|
|
|
{ |
116
|
|
|
$data = key($this->data); |
117
|
|
|
|
118
|
|
|
return $data; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Advance the internal array pointer of a collection. |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function next() |
127
|
|
|
{ |
128
|
|
|
$data = next($this->data); |
129
|
|
|
|
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Checks if key is set in the collection. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function valid() |
139
|
|
|
{ |
140
|
|
|
$key = key($this->data); |
141
|
|
|
$data = ($key !== null && $key !== false); |
142
|
|
|
|
143
|
|
|
return $data; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|