1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | /** |
6: | * This file is part of the Nexus framework. |
7: | * |
8: | * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com> |
9: | * |
10: | * For the full copyright and license information, please view |
11: | * the LICENSE file that was distributed with this source code. |
12: | */ |
13: | |
14: | namespace Nexus\Option; |
15: | |
16: | /** |
17: | * @implements Option<never> |
18: | */ |
19: | final readonly class None implements Option |
20: | { |
21: | public function isSome(): bool |
22: | { |
23: | return false; |
24: | } |
25: | |
26: | public function isSomeAnd(\Closure $predicate): bool |
27: | { |
28: | return false; |
29: | } |
30: | |
31: | public function isNone(): bool |
32: | { |
33: | return true; |
34: | } |
35: | |
36: | public function unwrap(): never |
37: | { |
38: | throw new NoneException(); |
39: | } |
40: | |
41: | /** |
42: | * @template S |
43: | * |
44: | * @param S $default |
45: | * |
46: | * @return S |
47: | */ |
48: | public function unwrapOr(mixed $default): mixed |
49: | { |
50: | return $default; |
51: | } |
52: | |
53: | public function unwrapOrElse(\Closure $default): mixed |
54: | { |
55: | return $default(); |
56: | } |
57: | |
58: | public function map(\Closure $predicate): self |
59: | { |
60: | return $this; |
61: | } |
62: | |
63: | /** |
64: | * @template U |
65: | * @template V |
66: | * |
67: | * @param V $default |
68: | * @param (\Closure(never): U) $predicate |
69: | * |
70: | * @return V |
71: | */ |
72: | public function mapOr(mixed $default, \Closure $predicate): mixed |
73: | { |
74: | return $default; |
75: | } |
76: | |
77: | /** |
78: | * @template U |
79: | * @template V |
80: | * |
81: | * @param (\Closure(): V) $default |
82: | * @param (\Closure(never): U) $predicate |
83: | * |
84: | * @return V |
85: | */ |
86: | public function mapOrElse(\Closure $default, \Closure $predicate): mixed |
87: | { |
88: | return $default(); |
89: | } |
90: | |
91: | public function and(Option $other): self |
92: | { |
93: | return $this; |
94: | } |
95: | |
96: | public function andThen(\Closure $predicate): self |
97: | { |
98: | return $this; |
99: | } |
100: | |
101: | public function filter(\Closure $predicate): self |
102: | { |
103: | return $this; |
104: | } |
105: | |
106: | public function or(Option $other): Option |
107: | { |
108: | return $other; |
109: | } |
110: | |
111: | public function orElse(\Closure $other): Option |
112: | { |
113: | return $other(); |
114: | } |
115: | |
116: | /** |
117: | * @template S |
118: | * |
119: | * @param Option<S> $other |
120: | * |
121: | * @return ($other is Some<S> ? Some<S> : self<T>) |
122: | */ |
123: | public function xor(Option $other): Option |
124: | { |
125: | return $other->isSome() ? $other : $this; |
126: | } |
127: | |
128: | /** |
129: | * @return \EmptyIterator |
130: | */ |
131: | public function getIterator(): \Traversable |
132: | { |
133: | return new \EmptyIterator(); |
134: | } |
135: | } |
136: |