Skip to content

Commit 1517bf3

Browse files
committed
Update README.md
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
1 parent 440a8cd commit 1517bf3

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

README.md

+210
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,216 @@ divide(1, 0); // None
4949
divide(1, 1); // Some(1)
5050
```
5151

52+
## API
53+
54+
### `SomeInterface`
55+
``` php
56+
/**
57+
* @immutable
58+
*
59+
* @implements OptionInterface<TValue>
60+
*
61+
* @template TValue
62+
*/
63+
interface SomeInterface extends OptionInterface
64+
{
65+
/**
66+
* @template TSomeValue
67+
*
68+
* @param TSomeValue $value
69+
*
70+
* @return self<TSomeValue>
71+
*/
72+
public static function create(mixed $value): self;
73+
}
74+
```
75+
76+
### `NoneInterface`
77+
``` php
78+
/**
79+
* @immutable
80+
*
81+
* @implements OptionInterface<TValue>
82+
*
83+
* @template TValue
84+
*/
85+
interface NoneInterface extends OptionInterface
86+
{
87+
/** @return self<TValue> */
88+
public static function create(): self;
89+
}
90+
```
91+
92+
### `OptionInterface`
93+
``` php
94+
/**
95+
* @implements IteratorAggregate<TValue>
96+
*
97+
* @template TValue
98+
*/
99+
interface OptionInterface extends IteratorAggregate
100+
{
101+
/**
102+
* Returns None if the Option is None, otherwise returns $option.
103+
*/
104+
public function and(self $option): self;
105+
106+
/**
107+
* Returns None if the option is None, otherwise calls $function with the wrapped value and returns the result.
108+
*
109+
* @template TAndThen
110+
*
111+
* @param callable(TValue):TAndThen $function
112+
*
113+
* @return self<TAndThen|TValue>
114+
*/
115+
public function andThen(callable $function): self;
116+
117+
/**
118+
* Returns true if the option is a Some value containing the given $value.
119+
*
120+
* @param TValue $value
121+
*/
122+
public function contains(mixed $value): bool;
123+
124+
/**
125+
* Returns the contained Some value, consuming the self value.
126+
*
127+
* @throws Throwable if the value is a None with a custom $throwable provided
128+
*
129+
* @return TValue
130+
*/
131+
public function expect(Throwable $throwable): mixed;
132+
133+
/**
134+
* Returns None if the option is None, otherwise calls $function with the wrapped value and returns: Some(TValue) if
135+
* $function returns true (where TValue is the wrapped value), and None if $function returns false.
136+
*
137+
* @param callable(TValue):bool $function
138+
*
139+
* @return self<TValue>
140+
*/
141+
public function filter(callable $function): self;
142+
143+
/**
144+
* Converts from Option<Option<TValue>> to Option<TValue>. Flattening only removes one level of nesting at a time.
145+
*
146+
* @return self<TValue>
147+
*/
148+
public function flatten(): self;
149+
150+
public function getIterator(): Traversable;
151+
152+
/**
153+
* Returns true if the Option is an instance of None.
154+
*/
155+
public function isNone(): bool;
156+
157+
/**
158+
* Returns true if the Option is an instance of Some.
159+
*/
160+
public function isSome(): bool;
161+
162+
/**
163+
* Maps a Some<TValue> to Some<TValue> by applying the callable $function to the contained value.
164+
*
165+
* @template TMap
166+
*
167+
* @param callable(TValue):TMap $function
168+
*
169+
* @return self<TMap>
170+
*/
171+
public function map(callable $function): self;
172+
173+
/**
174+
* Applies a function to the contained value (if any), or returns the provided default (if not).
175+
*
176+
* @template TFunction
177+
* @template TFallback
178+
*
179+
* @param callable(TValue): TFunction $function
180+
* @param TFallback $fallback
181+
*
182+
* @return TFallback|TFunction
183+
*/
184+
public function mapOr(callable $function, mixed $fallback): mixed;
185+
186+
/**
187+
* Applies a function to the contained value (if any), or computes a default (if not).
188+
*
189+
* @template TFunction
190+
* @template TFallback
191+
*
192+
* @param callable(TValue):TFunction $function
193+
* @param callable():TFallback $fallback
194+
*
195+
* @return TFallback|TFunction
196+
*/
197+
public function mapOrElse(callable $function, callable $fallback): mixed;
198+
199+
/**
200+
* Creates an option with the given value.
201+
*
202+
* By default, we treat null as the None case, and everything else as Some.
203+
*
204+
* @template TNullableValue
205+
*
206+
* @param TNullableValue $value the actual value
207+
*
208+
* @return self<TNullableValue|TValue>
209+
*/
210+
public static function of(mixed $value): self;
211+
212+
/**
213+
* Returns the option if it contains a value, otherwise returns $option.
214+
*
215+
* Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended
216+
* to use orElse, which is lazily evaluated.
217+
*/
218+
public function or(self $option): self;
219+
220+
/**
221+
* Returns the option if it contains a value, otherwise calls $function and returns the result.
222+
*
223+
* @template TCallableResultValue
224+
*
225+
* @param callable(): OptionInterface<TCallableResultValue> $function
226+
*/
227+
public function orElse(callable $function): self;
228+
229+
/**
230+
* Returns the value out of the option<TValue> if it is Some(TValue).
231+
*
232+
* @throws NullPointerException if the Option<TValue> is None
233+
*
234+
* @return TValue
235+
*/
236+
public function unwrap(): mixed;
237+
238+
/**
239+
* Returns the contained value or a default $fallback value.
240+
*
241+
* @template TFallbackValue
242+
*
243+
* @param TFallbackValue $fallback
244+
*
245+
* @return TFallbackValue|TValue
246+
*/
247+
public function unwrapOr(mixed $fallback): mixed;
248+
249+
/**
250+
* Returns the contained value or computes it from the given $function.
251+
*
252+
* @template TCallableResultValue
253+
*
254+
* @param callable():TCallableResultValue $function
255+
*
256+
* @return TCallableResultValue|TValue
257+
*/
258+
public function unwrapOrElse(callable $function): mixed;
259+
}
260+
```
261+
52262
## Testing
53263

54264
``` bash

0 commit comments

Comments
 (0)