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