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\Result; |
15: | |
16: | /** |
17: | * @template T |
18: | * |
19: | * @implements Result<T, never> |
20: | */ |
21: | final readonly class Ok implements Result |
22: | { |
23: | /** |
24: | * @param T $value |
25: | */ |
26: | public function __construct( |
27: | private mixed $value, |
28: | ) {} |
29: | |
30: | #[\Override] |
31: | public function isOk(): bool |
32: | { |
33: | return true; |
34: | } |
35: | |
36: | #[\Override] |
37: | public function isOkAnd(\Closure $predicate): bool |
38: | { |
39: | return $predicate($this->value); |
40: | } |
41: | |
42: | #[\Override] |
43: | public function isErr(): bool |
44: | { |
45: | return false; |
46: | } |
47: | |
48: | #[\Override] |
49: | public function isErrAnd(\Closure $predicate): bool |
50: | { |
51: | return false; |
52: | } |
53: | |
54: | /** |
55: | * @template U |
56: | * |
57: | * @param (\Closure(T): U) $predicate |
58: | * |
59: | * @return self<U> |
60: | */ |
61: | #[\Override] |
62: | public function map(\Closure $predicate): self |
63: | { |
64: | return new self($predicate($this->value)); |
65: | } |
66: | |
67: | #[\Override] |
68: | public function mapOr(mixed $default, \Closure $predicate): mixed |
69: | { |
70: | return $predicate($this->value); |
71: | } |
72: | |
73: | #[\Override] |
74: | public function mapOrElse(\Closure $default, \Closure $predicate): mixed |
75: | { |
76: | return $predicate($this->value); |
77: | } |
78: | |
79: | /** |
80: | * @return self<T> |
81: | */ |
82: | #[\Override] |
83: | public function mapErr(\Closure $predicate): self |
84: | { |
85: | return $this; |
86: | } |
87: | |
88: | /** |
89: | * @throws void |
90: | */ |
91: | #[\Override] |
92: | public function unwrap(): mixed |
93: | { |
94: | return $this->value; |
95: | } |
96: | |
97: | /** |
98: | * @return T |
99: | */ |
100: | #[\Override] |
101: | public function unwrapOr(mixed $default): mixed |
102: | { |
103: | return $this->value; |
104: | } |
105: | |
106: | /** |
107: | * @return T |
108: | */ |
109: | #[\Override] |
110: | public function unwrapOrElse(\Closure $op): mixed |
111: | { |
112: | return $this->value; |
113: | } |
114: | |
115: | #[\Override] |
116: | public function unwrapErr(): never |
117: | { |
118: | $message = static fn(string $arg): string => \sprintf('Unwrapped an Ok result: %s', $arg); |
119: | |
120: | if ($this->value instanceof \Throwable) { |
121: | throw new UnwrappedResultException($message($this->value->getMessage()), 0, $this->value); |
122: | } |
123: | |
124: | if (\is_scalar($this->value)) { |
125: | throw new UnwrappedResultException($message(var_export($this->value, true))); |
126: | } |
127: | |
128: | throw new UnwrappedResultException('Unwrapped an Ok result.'); |
129: | } |
130: | |
131: | /** |
132: | * @template U |
133: | * @template E |
134: | * @template R of Result<U, E> |
135: | * |
136: | * @param R $res |
137: | * |
138: | * @return R |
139: | */ |
140: | #[\Override] |
141: | public function and(Result $res): Result |
142: | { |
143: | return $res; |
144: | } |
145: | |
146: | /** |
147: | * @template U |
148: | * @template E |
149: | * @template R of Result<U, E> |
150: | * |
151: | * @param (\Closure(T): R) $op |
152: | * |
153: | * @return R |
154: | */ |
155: | #[\Override] |
156: | public function andThen(\Closure $op): Result |
157: | { |
158: | return $op($this->value); |
159: | } |
160: | |
161: | /** |
162: | * @return self<T> |
163: | */ |
164: | #[\Override] |
165: | public function or(Result $res): self |
166: | { |
167: | return $this; |
168: | } |
169: | |
170: | /** |
171: | * @return self<T> |
172: | */ |
173: | #[\Override] |
174: | public function orElse(\Closure $op): self |
175: | { |
176: | return $this; |
177: | } |
178: | } |
179: |