-
Notifications
You must be signed in to change notification settings - Fork 8
/
Option.php
268 lines (243 loc) · 6.75 KB
/
Option.php
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
<?php
/**
* Option
*
* Documentation and API borrowed from Rust: https://doc.rust-lang.org/std/option/enum.Option.html
* @author Oskar Thornblad
*/
declare(strict_types=1);
namespace Prewk;
use Closure;
use Exception;
use Prewk\Option\{OptionException, Some, None};
use Traversable;
/**
* Describes an optional value
*
* @template T
* The optional value
*/
abstract class Option
{
/**
* Returns true if the option is a Some value.
*
* @return bool
*/
abstract public function isSome(): bool;
/**
* Returns true if the option is a None value.
*
* @return bool
*/
abstract public function isNone(): bool;
/**
* Unwraps a result, yielding the content of a Some.
*
* @param Exception $msg
* @return mixed
* @psalm-return T
* @throws Exception the message if the value is a None.
*/
abstract public function expect(Exception $msg);
/**
* Unwraps an option, yielding the content of a Some.
*
* @return mixed
* @psalm-return T
* @throws OptionException if the value is a None.
*/
abstract public function unwrap();
/**
* Unwraps a result, yielding the content of a Some. Else, it returns optb.
*
* @param mixed $optb
* @psalm-param T $optb
* @return mixed
* @psalm-return T
*/
abstract public function unwrapOr($optb);
/**
* Returns the contained value or computes it from a closure.
*
* @param Closure $op
* @psalm-param Closure(mixed...):T $op
* @return mixed
* @psalm-return T
*/
abstract public function unwrapOrElse(Closure $op);
/**
* Maps an Option by applying a function to a contained Some value, leaving a None value untouched.
*
* @template U
*
* @param Closure $mapper
* @psalm-param Closure(T=,mixed...):U $mapper
* @return Option
* @psalm-return Option<U>
*/
abstract public function map(Closure $mapper): Option;
/**
* Applies a function to the contained value (if any), or returns a default (if not).
*
* @template U
*
* @param mixed $default
* @psalm-param U $default
* @param Closure $mapper
* @psalm-param Closure(T=,mixed...):U $mapper
* @return mixed
* @psalm-return U
*/
abstract public function mapOr($default, Closure $mapper);
/**
* Applies a function to the contained value (if any), or computes a default (if not).
*
* @template U
*
* @param Closure $default
* @psalm-param Closure(mixed...):U $default
* @param Closure $mapper
* @psalm-param Closure(T=,mixed...):U $mapper
* @return mixed
* @psalm-return U
*/
abstract public function mapOrElse(Closure $default, Closure $mapper);
/**
* Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).
*
* @template E
*
* @param mixed $err
* @psalm-param E $err
* @return Result
* @psalm-return Result<T, E>
*/
abstract public function okOr($err): Result;
/**
* Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).
*
* @template E
*
* @param Closure $err
* @psalm-param Closure(mixed...):E $err
* @return Result
* @psalm-return Result<T, E>
*/
abstract public function okOrElse(Closure $err): Result;
/**
* Returns an iterator over the possibly contained value.
* The iterator yields one value if the result is Some, otherwise none.
*
* @return array
* @psalm-return array<int, T>
*/
abstract public function iter(): array;
/**
* Returns None if the option is None, otherwise returns optb.
*
* @template U
*
* @param Option $optb
* @psalm-param Option<U> $optb
* @return Option
* @psalm-return Option<U>
*/
abstract public function and(Option $optb): Option;
/**
* Returns None if the option is None, otherwise calls op with the wrapped value and returns the result.
* Some languages call this operation flatmap.
*
* @template U
*
* @param Closure $op
* @psalm-param Closure(T=,mixed...):Option<U> $op
* @return Option
* @psalm-return Option<U>
*/
abstract public function andThen(Closure $op): Option;
/**
* Returns the option if it contains a value, otherwise returns optb.
*
* @param Option $optb
* @psalm-param Option<T> $optb
* @return Option
* @psalm-return Option<T>
*/
abstract public function or(Option $optb): Option;
/**
* Returns the option if it contains a value, otherwise calls op and returns the result.
*
* @param Closure $op
* @psalm-param Closure(mixed...):Option<T> $op
* @return Option
* @psalm-return Option<T>
*/
abstract public function orElse(Closure $op): Option;
/**
* The attached pass-through args will be unpacked into extra args into chained closures
*
* @param mixed ...$args
* @return Option
* @psalm-return Option<T>
*/
abstract public function with(...$args): Option;
/**
* Create a Some<T> if T is something using isset(T), None otherwise
*
* @template V
*
* @param mixed $thing
* @psalm-param V|null $thing
* @return Option Option<V>
* @psalm-return Option<V>
*/
public static function fromNullable($thing): Option
{
return isset($thing) ? new Some($thing) : new None;
}
/**
* Create a Some<V> from C[K] if it exists using array_key_exists(C, K), None otherwise
*
* @param array $coll C
* @param mixed $key
* @psalm-param array-key $key
* @return Option Option<V>
*/
public static function fromKey(array $coll, $key): Option
{
return array_key_exists($key, $coll) ? new Some($coll[$key]) : new None;
}
/**
* Create a Some<T> if T is non-empty using empty(T), None otherwise
*
* @template V
*
* @param mixed $thing
* @psalm-param V|null $thing
* @return Option Option<V>
* @psalm-return Option<V>
*/
public static function fromEmptyable($thing): Option
{
return !empty($thing) ? new Some($thing) : new None;
}
/**
* Iterates over an iterable and creates a Some<V> from the first item, returning None if the iterable is empty
*
* @template V
*
* @param iterable $iterable
* @psalm-param iterable<V> $iterable
* @return Option
* @psalm-return Option<V>
* @throws OptionException
*/
public static function fromFirst(iterable $iterable): Option
{
foreach ($iterable as $item) {
return new Some($item);
}
return new None;
}
}