|
1
|
|
|
<?php namespace BuildR\Collection\Collection; |
|
2
|
|
|
|
|
3
|
|
|
use BuildR\Collection\Exception\CollectionException; |
|
4
|
|
|
use BuildR\Collection\Collection\CollectionInterface; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Abstract collection implementation |
|
8
|
|
|
* |
|
9
|
|
|
* BuildR PHP Framework |
|
10
|
|
|
* |
|
11
|
|
|
* @author Zoltán Borsos <[email protected]> |
|
12
|
|
|
* @package Collection |
|
13
|
|
|
* @subpackage Collection |
|
14
|
|
|
* |
|
15
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
|
16
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
|
17
|
|
|
* @link https://github.com/Zolli/BuildR |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractCollection implements CollectionInterface { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @type array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $data = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
39 |
|
public function toArray() { |
|
30
|
39 |
|
return (array) $this->data; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function clear() { |
|
37
|
2 |
|
$this->data = []; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function isEmpty() { |
|
44
|
1 |
|
return empty($this->data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
59 |
|
public function size() { |
|
51
|
59 |
|
return (is_array($this->data)) ? count($this->data) : 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
* |
|
57
|
|
|
* @codeCoverageIgnore |
|
58
|
|
|
*/ |
|
59
|
|
|
public function current() { |
|
60
|
|
|
return current($this->data); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
* |
|
66
|
|
|
* @codeCoverageIgnore |
|
67
|
|
|
*/ |
|
68
|
|
|
public function next() { |
|
69
|
|
|
return next($this->data); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @codeCoverageIgnore |
|
76
|
|
|
*/ |
|
77
|
|
|
public function key() { |
|
78
|
|
|
return key($this->data); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
* |
|
84
|
|
|
* @codeCoverageIgnore |
|
85
|
|
|
*/ |
|
86
|
|
|
public function valid() { |
|
87
|
|
|
return key($this->data) !== NULL; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
* |
|
93
|
|
|
* @codeCoverageIgnore |
|
94
|
|
|
*/ |
|
95
|
|
|
public function rewind() { |
|
96
|
|
|
return reset($this->data); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
40 |
|
public function count() { |
|
103
|
40 |
|
return $this->size(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param array|\BuildR\Collection\Collection\CollectionInterface $elements |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
36 |
|
protected function collectionToArray($elements) { |
|
112
|
36 |
|
if($elements instanceof CollectionInterface) { |
|
113
|
1 |
|
$elements = $elements->toArray(); |
|
114
|
1 |
|
} |
|
115
|
|
|
|
|
116
|
36 |
|
return $elements; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|