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: #[\Override]
22: public function isSome(): bool
23: {
24: return false;
25: }
26:
27: #[\Override]
28: public function isSomeAnd(\Closure $predicate): bool
29: {
30: return false;
31: }
32:
33: #[\Override]
34: public function isNone(): bool
35: {
36: return true;
37: }
38:
39: #[\Override]
40: public function unwrap(): never
41: {
42: throw new NoneException();
43: }
44:
45: /**
46: * @template S
47: *
48: * @param S $default
49: *
50: * @return S
51: */
52: #[\Override]
53: public function unwrapOr(mixed $default): mixed
54: {
55: return $default;
56: }
57:
58: #[\Override]
59: public function unwrapOrElse(\Closure $default): mixed
60: {
61: return $default();
62: }
63:
64: #[\Override]
65: public function map(\Closure $predicate): self
66: {
67: return $this;
68: }
69:
70: /**
71: * @template U
72: * @template V
73: *
74: * @param V $default
75: * @param (\Closure(never): U) $predicate
76: *
77: * @return V
78: */
79: #[\Override]
80: public function mapOr(mixed $default, \Closure $predicate): mixed
81: {
82: return $default;
83: }
84:
85: /**
86: * @template U
87: * @template V
88: *
89: * @param (\Closure(): V) $default
90: * @param (\Closure(never): U) $predicate
91: *
92: * @return V
93: */
94: #[\Override]
95: public function mapOrElse(\Closure $default, \Closure $predicate): mixed
96: {
97: return $default();
98: }
99:
100: #[\Override]
101: public function and(Option $other): self
102: {
103: return $this;
104: }
105:
106: #[\Override]
107: public function andThen(\Closure $predicate): self
108: {
109: return $this;
110: }
111:
112: #[\Override]
113: public function filter(\Closure $predicate): self
114: {
115: return $this;
116: }
117:
118: #[\Override]
119: public function or(Option $other): Option
120: {
121: return $other;
122: }
123:
124: #[\Override]
125: public function orElse(\Closure $other): Option
126: {
127: return $other();
128: }
129:
130: /**
131: * @template S
132: *
133: * @param Option<S> $other
134: *
135: * @return ($other is Some<S> ? Some<S> : self<T>)
136: */
137: #[\Override]
138: public function xor(Option $other): Option
139: {
140: return $other->isSome() ? $other : $this;
141: }
142:
143: /**
144: * @return \EmptyIterator
145: */
146: #[\Override]
147: public function getIterator(): \Traversable
148: {
149: return new \EmptyIterator();
150: }
151: }
152: