1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Weew\Collections; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Weew\Contracts\IArrayable; |
7
|
|
|
|
8
|
|
|
class Collection implements ICollection { |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $items; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param array $items |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $items = []) { |
18
|
|
|
$this->setItems($items); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function getItems() { |
25
|
|
|
return $this->items; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $items |
30
|
|
|
*/ |
31
|
|
|
public function setItems(array $items) { |
32
|
|
|
$this->items = []; |
33
|
|
|
|
34
|
|
|
foreach ($items as $item) { |
35
|
|
|
$this->add($item); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $items |
41
|
|
|
*/ |
42
|
|
|
public function merge(array $items) { |
43
|
|
|
foreach ($items as $item) { |
44
|
|
|
$this->add($item); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ICollection $items |
50
|
|
|
*/ |
51
|
|
|
public function extend(ICollection $items) { |
52
|
|
|
$this->merge($items->getItems()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
public function count() { |
59
|
|
|
return count($this->items); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $item |
64
|
|
|
*/ |
65
|
|
|
public function add($item) { |
66
|
|
|
$this->items[] = $item; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param null $default |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function first($default = null) { |
75
|
|
|
return array_first($this->items, $default); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param null $default |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function last($default = null) { |
84
|
|
|
return array_last($this->items, $default); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function toArray() { |
|
|
|
|
91
|
|
|
$items = []; |
92
|
|
|
|
93
|
|
|
foreach ($this->getItems() as $key => $value) { |
94
|
|
|
if ($value instanceof IArrayable) { |
95
|
|
|
$value = $value->toArray(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$items[$key] = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $items; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $key |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function offsetGet($key) { |
110
|
|
|
return $this->items[$key]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $key |
115
|
|
|
* @param mixed $value |
116
|
|
|
*/ |
117
|
|
|
public function offsetSet($key, $value) { |
118
|
|
|
$this->items[$key] = $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $key |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function offsetExists($key) { |
127
|
|
|
return array_key_exists($key, $this->items); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param mixed $key |
132
|
|
|
*/ |
133
|
|
|
public function offsetUnset($key) { |
134
|
|
|
if ($this->offsetExists($key)) { |
135
|
|
|
unset($this->items[$key]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return ArrayIterator |
141
|
|
|
*/ |
142
|
|
|
public function getIterator() { |
143
|
|
|
return new ArrayIterator($this->items); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.