1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sergiors\Functional; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Sérgio Rafael Siqueira <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class Collection implements \ArrayAccess, \IteratorAggregate, \Countable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $ls; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $ls |
17
|
|
|
*/ |
18
|
4 |
|
public function __construct(array $ls) |
19
|
|
|
{ |
20
|
4 |
|
$this->ls = $ls; |
21
|
4 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \Closure $fn |
25
|
|
|
* |
26
|
|
|
* @return Collection |
27
|
|
|
*/ |
28
|
1 |
|
public function filter(\Closure $fn) |
29
|
|
|
{ |
30
|
1 |
|
return new self(filter($fn, $this->ls)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Closure $fn |
35
|
|
|
* |
36
|
|
|
* @return Collection |
37
|
|
|
*/ |
38
|
1 |
|
public function map(\Closure $fn) |
39
|
|
|
{ |
40
|
1 |
|
return new self(map($fn, $this->ls)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \Closure $fn |
45
|
|
|
* |
46
|
|
|
* @return Collection |
47
|
|
|
*/ |
48
|
1 |
|
public function each(\Closure $fn) |
49
|
|
|
{ |
50
|
1 |
|
return new self(each($fn, $this->ls)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \Closure $fn |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
1 |
|
public function reduce(\Closure $fn) |
59
|
|
|
{ |
60
|
1 |
|
return reduce($fn, $this->ls); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $value |
65
|
|
|
* |
66
|
|
|
* @return Collection |
67
|
|
|
*/ |
68
|
1 |
|
public function prepend($value) |
69
|
|
|
{ |
70
|
1 |
|
return new self(prepend($value, $this->ls)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $value |
75
|
|
|
* |
76
|
|
|
* @return Collection |
77
|
|
|
*/ |
78
|
1 |
|
public function append($value) |
79
|
|
|
{ |
80
|
1 |
|
return new self(append($value, $this->ls)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
1 |
|
public function count() |
87
|
|
|
{ |
88
|
1 |
|
return count($this->ls); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \ArrayIterator |
93
|
|
|
*/ |
94
|
1 |
|
public function getIterator() |
95
|
|
|
{ |
96
|
1 |
|
return new \ArrayIterator($this->ls); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
1 |
|
public function toArray() |
103
|
|
|
{ |
104
|
1 |
|
return $this->ls; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $offset |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
1 |
|
public function offsetExists($offset) |
113
|
|
|
{ |
114
|
1 |
|
return has($offset, $this->ls); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $offset |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
1 |
|
public function offsetGet($offset) |
123
|
|
|
{ |
124
|
1 |
|
return prop($offset, $this->ls) ?: null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $offset |
129
|
|
|
* |
130
|
|
|
* @param mixed $value |
131
|
|
|
*/ |
132
|
1 |
|
public function offsetSet($offset, $value) |
133
|
|
|
{ |
134
|
1 |
|
if (null === $offset) { |
135
|
1 |
|
$this->ls[] = $value; |
136
|
1 |
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$this->ls[$offset] = $value; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param mixed $offset |
144
|
|
|
*/ |
145
|
1 |
|
public function offsetUnset($offset) |
146
|
|
|
{ |
147
|
1 |
|
if ($this->offsetExists($offset)) { |
148
|
1 |
|
unset($this->ls[$offset]); |
149
|
1 |
|
} |
150
|
1 |
|
} |
151
|
|
|
} |
152
|
|
|
|