4
4
5
5
namespace Ghostwriter \Option ;
6
6
7
- use Closure ;
8
- use Generator ;
9
7
use Ghostwriter \Option \Exception \NullPointerException ;
10
- use Ghostwriter \Option \Exception \OptionException ;
11
8
use Ghostwriter \Option \Exception \ShouldNotHappenException ;
12
9
use Ghostwriter \Option \Interface \NoneInterface ;
13
10
use Ghostwriter \Option \Interface \OptionInterface ;
16
13
use Tests \Unit \SomeTest ;
17
14
use Throwable ;
18
15
19
- use function is_iterable ;
16
+ use function get_debug_type ;
20
17
use function sprintf ;
21
18
22
19
/**
28
25
*/
29
26
final readonly class Some implements SomeInterface
30
27
{
31
- /**
32
- * @param TSome $value
33
- */
34
- public function __construct (
28
+ /** @param TSome $value */
29
+ private function __construct (
35
30
private mixed $ value
36
31
) {}
37
32
@@ -42,19 +37,17 @@ public function __construct(
42
37
*
43
38
* @throws NullPointerException
44
39
*
45
- * @return SomeInterface<TNew>
40
+ * @return (TNew is SomeInterface ? TNew : SomeInterface <TNew>)
46
41
*/
47
42
#[Override]
48
43
public static function new (mixed $ value ): SomeInterface
49
44
{
50
45
return match (true ) {
51
- null === $ value => throw new NullPointerException (),
46
+ $ value instanceof NoneInterface, null === $ value => throw new NullPointerException (),
52
47
53
48
$ value instanceof SomeInterface => $ value ,
54
49
55
- default => /** @var SomeInterface<TNew> */ new self ($ value ),
56
-
57
- $ value instanceof NoneInterface => throw new ShouldNotHappenException (),
50
+ default => new self ($ value ),
58
51
};
59
52
}
60
53
@@ -64,8 +57,11 @@ public function and(OptionInterface $option): OptionInterface
64
57
return $ option ;
65
58
}
66
59
60
+ /**
61
+ * @throws ShouldNotHappenException
62
+ */
67
63
#[Override]
68
- public function andThen (Closure $ function ): OptionInterface
64
+ public function andThen (callable $ function ): OptionInterface
69
65
{
70
66
/** @var null|OptionInterface<TSome> $result */
71
67
$ result = $ function ($ this ->value );
@@ -74,63 +70,62 @@ public function andThen(Closure $function): OptionInterface
74
70
return $ result ;
75
71
}
76
72
77
- throw new OptionException (sprintf (
78
- 'Closure passed to andThen() must return an instance of %s. ' ,
73
+ throw new ShouldNotHappenException (sprintf (
74
+ 'Callable passed to andThen() must return an instance of %s. ' ,
79
75
OptionInterface::class
80
76
));
81
77
}
82
78
79
+ /** @throws Throwable */
83
80
#[Override]
84
- public function contains ( mixed $ value ): bool
81
+ public function expect ( Throwable $ throwable ): mixed
85
82
{
86
- if ($ this instanceof NoneInterface) {
87
- return false ;
88
- }
89
-
90
- return $ this ->value === $ value ;
83
+ return $ this ->value ;
91
84
}
92
85
93
86
/**
94
- * @throws Throwable
87
+ * @throws ShouldNotHappenException
95
88
*/
96
89
#[Override]
97
- public function expect ( Throwable $ throwable ): mixed
90
+ public function filter ( callable $ function ): OptionInterface
98
91
{
99
- if ($ this instanceof NoneInterface) {
100
- throw $ throwable ;
101
- }
92
+ $ result = $ function ($ this ->value );
102
93
103
- return $ this ->value ;
94
+ return match ($ result ) {
95
+ true => $ this ,
96
+ false => None::new (),
97
+ default => throw new ShouldNotHappenException (sprintf (
98
+ 'Callable passed to filter() must return a boolean, %s given. ' ,
99
+ get_debug_type ($ result )
100
+ )),
101
+ };
104
102
}
105
103
104
+ /**
105
+ * @throws NullPointerException
106
+ */
106
107
#[Override]
107
- public function filter ( Closure $ function ): OptionInterface
108
+ public function get ( ): mixed
108
109
{
109
- return match (true ) {
110
- $ function ($ this ->value ) => $ this ,
111
- default => None::new ()
112
- };
110
+ return $ this ->value ;
113
111
}
114
112
115
113
#[Override]
116
- public function flatten ( ): OptionInterface
114
+ public function getOr ( mixed $ fallback ): mixed
117
115
{
118
- return match (true ) {
119
- $ this ->value instanceof SomeInterface => $ this ->value ,
120
- default => $ this
121
- };
116
+ return $ this ->value ;
122
117
}
123
118
124
119
#[Override]
125
- public function getIterator ( ): Generator
120
+ public function getOrElse ( callable $ function ): mixed
126
121
{
127
- $ value = $ this ->value ;
122
+ return $ this ->value ;
123
+ }
128
124
129
- if (is_iterable ($ value )) {
130
- yield from $ value ;
131
- } else {
132
- yield $ value ;
133
- }
125
+ #[Override]
126
+ public function is (mixed $ value ): bool
127
+ {
128
+ return $ this ->value === $ value ;
134
129
}
135
130
136
131
#[Override]
@@ -146,27 +141,19 @@ public function isSome(): bool
146
141
}
147
142
148
143
#[Override]
149
- public function map (Closure $ function ): OptionInterface
144
+ public function map (callable $ function ): OptionInterface
150
145
{
151
- $ value = $ function ($ this ->value );
152
-
153
- return match (true ) {
154
- $ value instanceof OptionInterface => $ value ,
155
-
156
- null === $ value => None::new (),
157
-
158
- default => self ::new ($ value ),
159
- };
146
+ return Option::new ($ function ($ this ->value ));
160
147
}
161
148
162
149
#[Override]
163
- public function mapOr (Closure $ function , mixed $ fallback ): mixed
150
+ public function mapOr (callable $ function , mixed $ fallback ): mixed
164
151
{
165
152
return $ function ($ this ->value );
166
153
}
167
154
168
155
#[Override]
169
- public function mapOrElse (Closure $ function , Closure $ fallback ): mixed
156
+ public function mapOrElse (callable $ function , callable $ fallback ): mixed
170
157
{
171
158
return $ function ($ this ->value );
172
159
}
@@ -178,38 +165,8 @@ public function or(OptionInterface $option): OptionInterface
178
165
}
179
166
180
167
#[Override]
181
- public function orElse (Closure $ function ): OptionInterface
168
+ public function orElse (callable $ function ): OptionInterface
182
169
{
183
170
return $ this ;
184
171
}
185
-
186
- #[Override]
187
- public function get (): mixed
188
- {
189
- return $ this ->value ;
190
- }
191
-
192
- #[Override]
193
- public function getOr (mixed $ fallback ): mixed
194
- {
195
- return $ this ->value ;
196
- }
197
-
198
- #[Override]
199
- public function getOrElse (Closure $ function ): mixed
200
- {
201
- return $ this ->value ;
202
- }
203
-
204
- #[Override]
205
- public static function nullable (mixed $ value ): OptionInterface
206
- {
207
- return match (true ) {
208
- $ value instanceof OptionInterface => $ value ,
209
-
210
- null === $ value => None::new (),
211
-
212
- default => self ::new ($ value ),
213
- };
214
- }
215
172
}
0 commit comments