|
1
|
|
|
<?php namespace Rde; |
|
2
|
|
|
|
|
3
|
|
|
use ArrayAccess; |
|
4
|
|
|
use Iterator; |
|
5
|
|
|
|
|
6
|
|
|
class Arr implements ArrayAccess, Iterator |
|
7
|
|
|
{ |
|
8
|
|
|
protected $resource; |
|
9
|
|
|
|
|
10
|
|
|
protected $resource_resolved; |
|
11
|
|
|
|
|
12
|
|
|
protected $is_resolved; |
|
13
|
|
|
|
|
14
|
|
|
protected $queues; |
|
15
|
|
|
|
|
16
|
5 |
|
public function __construct($resource) |
|
17
|
|
|
{ |
|
18
|
5 |
|
$this->resource = $resource; |
|
19
|
5 |
|
$this->init(); |
|
20
|
5 |
|
} |
|
21
|
|
|
|
|
22
|
4 |
|
public function filter($driver) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->queues[] = function($v, $k) use($driver) { |
|
25
|
|
|
return array( |
|
26
|
4 |
|
$driver($v, $k), |
|
27
|
4 |
|
'filter', |
|
28
|
4 |
|
); |
|
29
|
|
|
}; |
|
30
|
|
|
|
|
31
|
4 |
|
$this->setUnresolved(); |
|
32
|
|
|
|
|
33
|
4 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public function take($cnt) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->queues[] = function() use($cnt) { |
|
39
|
2 |
|
static $_cnt; |
|
40
|
2 |
|
(null === $_cnt) and $_cnt = $cnt; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
return array( |
|
43
|
2 |
|
0 < $_cnt--, |
|
44
|
2 |
|
'take', |
|
45
|
2 |
|
); |
|
46
|
|
|
}; |
|
47
|
|
|
|
|
48
|
2 |
|
$this->setUnresolved(); |
|
49
|
|
|
|
|
50
|
2 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
public function init() |
|
54
|
|
|
{ |
|
55
|
5 |
|
$this->is_resolved = true; |
|
56
|
5 |
|
$this->resource_resolved = $this->resource; |
|
57
|
5 |
|
$this->queues = array(); |
|
58
|
5 |
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public function each($callback) |
|
61
|
|
|
{ |
|
62
|
3 |
|
$this->isResolved() ? |
|
63
|
3 |
|
$this->eachResourceResolved($callback) : |
|
64
|
3 |
|
$this->resolve($callback); |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
protected function setUnresolved() |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->is_resolved = false; |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
5 |
|
protected function isResolved() |
|
73
|
|
|
{ |
|
74
|
5 |
|
return $this->is_resolved; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
protected function resolve($callback = null) |
|
78
|
|
|
{ |
|
79
|
5 |
|
$this->resource_resolved = array(); |
|
80
|
|
|
$callback = is_callable($callback) ? $callback : function(){}; |
|
81
|
|
|
|
|
82
|
5 |
|
foreach ($this->resource as $k => $v) { |
|
83
|
5 |
|
foreach ($this->queues as $driver) { |
|
84
|
5 |
|
$resolve = $driver($v, $k); |
|
85
|
5 |
|
if ( ! $resolve[0]) { |
|
86
|
5 |
|
if ('take' === $resolve[1]) { |
|
87
|
2 |
|
break 2; |
|
88
|
|
|
} |
|
89
|
4 |
|
continue 2; |
|
90
|
|
|
} |
|
91
|
5 |
|
} |
|
92
|
5 |
|
$this->resource_resolved[$k] = $v; |
|
93
|
5 |
|
$callback($v, $k); |
|
94
|
5 |
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
$this->is_resolved = true; |
|
97
|
5 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function eachResourceResolved($callback) |
|
100
|
|
|
{ |
|
101
|
|
|
foreach ($this->resource_resolved as $k => $v) { |
|
102
|
|
|
$callback($v, $k); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
public function offsetExists($key) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
1 |
|
return isset($this->resource_resolved[$key]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
public function offsetGet($key) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
1 |
|
return $this->resource_resolved[$key]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function offsetSet($key, $val) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
if (null === $key) { |
|
125
|
|
|
$this->resource_resolved[] = $val; |
|
126
|
|
|
} else { |
|
127
|
|
|
$this->resource_resolved[$key] = $val; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function offsetUnset($key) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
unset($this->resource_resolved[$key]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public function current() |
|
139
|
|
|
{ |
|
140
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
1 |
|
return current($this->resource_resolved); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
public function key() |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
1 |
|
return key($this->resource_resolved); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
public function next() |
|
153
|
|
|
{ |
|
154
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
1 |
|
return next($this->resource_resolved); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
public function rewind() |
|
160
|
|
|
{ |
|
161
|
1 |
|
$this->isResolved() or $this->resolve(); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
1 |
|
reset($this->resource_resolved); |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
public function valid() |
|
167
|
|
|
{ |
|
168
|
1 |
|
return null !== $this->key(); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.