|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Graze DataStructure |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @see http://github.com/graze/data-structure/blob/master/LICENSE |
|
11
|
|
|
* @link http://github.com/graze/data-structure |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataStructure\Collection; |
|
15
|
|
|
|
|
16
|
|
|
use ArrayIterator; |
|
17
|
|
|
use Graze\Sort; |
|
18
|
|
|
use Serializable; |
|
19
|
|
|
|
|
20
|
|
|
class Collection implements CollectionInterface, Serializable |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var mixed[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $items = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param mixed[] $items |
|
29
|
|
|
*/ |
|
30
|
32 |
|
public function __construct(array $items = []) |
|
31
|
|
|
{ |
|
32
|
32 |
|
foreach ($items as $item) { |
|
33
|
14 |
|
$this->add($item); |
|
34
|
|
|
} |
|
35
|
32 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param mixed $value |
|
39
|
|
|
* |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
28 |
|
public function add($value) |
|
43
|
|
|
{ |
|
44
|
28 |
|
$this->items[] = $value; |
|
45
|
|
|
|
|
46
|
28 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param mixed $value |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public function contains($value) |
|
55
|
|
|
{ |
|
56
|
6 |
|
return in_array($value, $this->items, true); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function count() |
|
63
|
|
|
{ |
|
64
|
2 |
|
return count($this->items); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param callable $fn |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function filter(callable $fn) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->items = array_values(array_filter($this->items, $fn)); |
|
75
|
|
|
|
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return mixed[] |
|
81
|
|
|
*/ |
|
82
|
16 |
|
public function getAll() |
|
83
|
|
|
{ |
|
84
|
16 |
|
return $this->items; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return ArrayIterator |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getIterator() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return new ArrayIterator($this->items); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param callable $fn |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function map(callable $fn) |
|
101
|
|
|
{ |
|
102
|
2 |
|
return array_map($fn, $this->items); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param callable $fn |
|
107
|
|
|
* @param mixed|null $initial |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function reduce(callable $fn, $initial = null) |
|
112
|
|
|
{ |
|
113
|
2 |
|
return array_reduce($this->items, $fn, $initial); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param callable $fn |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function sort(callable $fn) |
|
122
|
|
|
{ |
|
123
|
1 |
|
usort($this->items, $fn); |
|
124
|
|
|
|
|
125
|
1 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param callable $fn |
|
130
|
|
|
* @param int $order |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function sortOn(callable $fn, $order = Sort\ASC) |
|
135
|
|
|
{ |
|
136
|
2 |
|
$this->items = Sort\comparison($this->items, $fn, $order); |
|
137
|
|
|
|
|
138
|
2 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function serialize() |
|
145
|
|
|
{ |
|
146
|
2 |
|
return serialize($this->items); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $data |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function unserialize($data) |
|
153
|
|
|
{ |
|
154
|
2 |
|
$this->items = unserialize($data); |
|
155
|
2 |
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|