forked from Codeception/Codeception
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaybe.php
More file actions
236 lines (210 loc) · 5.74 KB
/
Copy pathMaybe.php
File metadata and controls
236 lines (210 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Author: davert
* Date: 30.07.12
*
* Class Maybe
* Represents either empty values or defined from results.
*
*/
namespace Codeception;
class Maybe implements \ArrayAccess, \Iterator, \JsonSerializable
{
protected $position = 0;
protected $val = null;
protected $assocArray = null;
function __construct($val = null)
{
$this->val = $val;
if (is_array($this->val)) {
$this->assocArray = $this->isAssocArray($this->val);
}
$this->position = 0;
}
private function isAssocArray($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
function __toString()
{
if ($this->val === null) {
return "?";
}
if (is_scalar($this->val)) {
return (string)$this->val;
}
if (is_object($this->val) && method_exists($this->val, '__toString')) {
return $this->val->__toString();
}
return $this->val;
}
function __get($key)
{
if ($this->val === null) {
return new Maybe();
}
if (is_object($this->val)) {
if (isset($this->val->{$key}) || property_exists($this->val, $key)) {
return $this->val->{$key};
}
}
return $this->val->key;
}
function __set($key, $val)
{
if ($this->val === null) {
return;
}
if (is_object($this->val)) {
$this->val->{$key} = $val;
return;
}
$this->val->key = $val;
}
function __call($method, $args)
{
if ($this->val === null) {
return new Maybe();
}
return call_user_func_array(array($this->val, $method), $args);
}
function __clone()
{
if (is_object($this->val)) {
$this->val = clone $this->val;
}
}
public function __unset($key)
{
if (is_object($this->val)) {
if (isset($this->val->{$key}) || property_exists($this->val, $key)) {
unset($this->val->{$key});
return;
}
}
}
public function offsetExists($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
return isset($this->val[$offset]);
}
return false;
}
public function offsetGet($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
return $this->val[$offset];
}
return new Maybe();
}
public function offsetSet($offset, $value)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
$this->val[$offset] = $value;
}
}
public function offsetUnset($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
unset($this->val[$offset]);
}
}
public function __value()
{
$val = $this->val;
if (is_array($val)) {
foreach ($val as $k => $v) {
if ($v instanceof self) {
$v = $v->__value();
}
$val[$k] = $v;
}
}
return $val;
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
if (! is_array($this->val)) {
return null;
}
if ($this->assocArray) {
$keys = array_keys($this->val);
return $this->val[$keys[$this->position]];
} else {
return $this->val[$this->position];
}
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
++$this->position;
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
if ($this->assocArray) {
$keys = array_keys($this->val);
return $keys[$this->position];
} else {
return $this->position;
}
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
if (! is_array($this->val)) {
return null;
}
if ($this->assocArray) {
$keys = array_keys($this->val);
return isset($keys[$this->position]);
} else {
return isset($this->val[$this->position]);
}
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
if (is_array($this->val)) {
$this->assocArray = $this->isAssocArray($this->val);
}
$this->position = 0;
}
/**
* (PHP 5 >= 5.4.0)
* Serializes the object to a value that can be serialized natively by json_encode().
* @link http://docs.php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed Returns data which can be serialized by json_encode(), which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return $this->__value();
}
}