ame-option.php
5.92 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
namespace YahnisElsts\AdminMenuEditor\Options;
/**
* A simplified PHP version of the Option class from Scala.
*
* It is also heavily inspired by the phpoption/phpoption package by Johannes M. Schmitt,
* though this version is designed to support PHP 5.6 and does not require PHP 7.0+.
*
* @template T
*/
abstract class Option implements \IteratorAggregate {
/**
* @return boolean
*/
abstract public function isDefined();
/**
* @return boolean
*/
public function isEmpty() {
return !$this->isDefined();
}
public function nonEmpty() {
return $this->isDefined();
}
/**
* @return T
* @throws \RuntimeException If the option is empty.
*/
abstract public function get();
/**
* @param T $default
* @return T
*/
abstract public function getOrElse($default);
/**
* @param callable():T $callable
* @return T
*/
abstract public function getOrCall($callable);
/**
* @param Option<T> $alternative
* @return Option<T>
*/
abstract public function orElse(self $alternative);
/**
* @template R
* @param callable(T):R $callable
* @return Option<R>
*/
abstract public function map($callable);
/**
* @template R
* @param callable(T):Option<R> $callable
* @return Option<R>
*/
abstract public function flatMap($callable);
/**
* Apply the given function to the option's value, if it's not empty.
*
* This is called "each" and not "forEach" because "foreach" is a keyword,
* which means it can't be used as a function name before PHP 7.0.
*
* @param callable(T):void $callable
* @return $this The same option instance.
*/
abstract public function each($callable);
/**
* Check if the option contains the specified value.
*
* @param mixed $value
* @return boolean
*/
abstract public function contains($value);
/**
* @template A
* @param A $value
* @param mixed $emptyValue
* @return Option<A>
*/
public static function fromValue($value, $emptyValue = null) {
if ( $value === $emptyValue ) {
return None::getInstance();
} else {
return new Some($value);
}
}
/**
* @template A
* @param callable():A $callable
* @return Option<A>
*/
public static function fromCallable($callable, $arguments = array()) {
return new LazyOption($callable, $arguments);
}
}
/**
* @template T
* @extends Option<T>
*/
final class Some extends Option {
/**
* @var mixed
*/
private $value;
public function __construct($value) {
$this->value = $value;
}
public function isDefined() {
return true;
}
public function get() {
return $this->value;
}
public function getOrElse($default) {
return $this->value;
}
public function getOrCall($callable) {
return $this->value;
}
public function orElse(Option $alternative) {
return $this;
}
public function map($callable) {
return new self($callable($this->value));
}
public function flatMap($callable) {
return $callable($this->value);
}
public function each($callable) {
$callable($this->value);
return $this;
}
public function contains($value) {
return ($this->value === $value);
}
#[\ReturnTypeWillChange]
public function getIterator() {
return new \ArrayIterator([$this->value]);
}
}
final class None extends Option {
/**
* @var null|self
*/
private static $instance = null;
private function __construct() {
//Prevent others from instantiating this class.
}
public static function getInstance() {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
public function isDefined() {
return false;
}
public function get() {
throw new \RuntimeException('Option is empty.');
}
public function getOrElse($default) {
return $default;
}
public function getOrCall($callable) {
return $callable();
}
public function orElse(Option $alternative) {
return $alternative;
}
public function map($callable) {
return $this;
}
public function flatMap($callable) {
return $this;
}
public function each($callable) {
//Intentionally does nothing.
return $this;
}
public function contains($value) {
return false;
}
#[\ReturnTypeWillChange]
public function getIterator() {
return new \EmptyIterator();
}
}
/**
* Lazy version of the Option class.
*
* This class just has an internal, lazy-initialized option, and it forwards all
* method calls to that option.
*
* @template T
* @extends Option<T>
*/
class LazyOption extends Option {
/**
* @var callable
*/
private $callback;
/**
* @var array
*/
private $arguments;
/**
* @var Option<T>|null
*/
private $innerOption = null;
public function __construct($callback, $arguments = array()) {
if ( !is_callable($callback) ) {
throw new \InvalidArgumentException('$callback must be a valid callable.');
}
$this->callback = $callback;
$this->arguments = $arguments;
}
private function resolve() {
if ( $this->innerOption === null ) {
$value = call_user_func_array($this->callback, $this->arguments);
if ( $value instanceof Option ) {
$this->innerOption = $value;
} else {
$this->innerOption = Option::fromValue($value);
}
}
return $this->innerOption;
}
public function isDefined() {
return $this->resolve()->isDefined();
}
public function get() {
return $this->resolve()->get();
}
public function getOrElse($default) {
return $this->resolve()->getOrElse($default);
}
public function getOrCall($callable) {
return $this->resolve()->getOrCall($callable);
}
public function orElse(Option $alternative) {
return $this->resolve()->orElse($alternative);
}
public function map($callable) {
return $this->resolve()->map($callable);
}
public function flatMap($callable) {
return $this->resolve()->flatMap($callable);
}
public function each($callable) {
$this->resolve()->each($callable);
return $this;
}
public function contains($value) {
return $this->resolve()->contains($value);
}
#[\ReturnTypeWillChange]
public function getIterator() {
return $this->resolve()->getIterator();
}
}