Some
extends Option
in package
Tags
Table of Contents
Properties
- $value : T
Methods
- __construct() : mixed
- create() : Some<string|int, U>
- ensure() : Option<string|int, S>|LazyOption<string|int, S>
- Option factory, which creates new option based on passed value.
- filter() : Option<string|int, T>
- If the option is empty, it is returned immediately without applying the callable.
- filterNot() : Option<string|int, T>
- If the option is empty, it is returned immediately without applying the callable.
- flatMap() : Option<string|int, S>
- Applies the callable to the value of the option if it is non-empty, and returns the return value of the callable directly.
- foldLeft() : S
- Binary operator for the initial value and the option's value.
- foldRight() : S
- foldLeft() but with reversed arguments for the callable.
- forAll() : Option<string|int, T>
- This is similar to map() except that the return value of the callable has no meaning.
- fromArraysValue() : Option<string|int, S>
- Creates an option from an array's value.
- fromReturn() : LazyOption<string|int, S>
- Creates a lazy-option with the given callback.
- fromValue() : Option<string|int, S>
- Creates an option given a return value.
- get() : T
- Returns the value if available, or throws an exception otherwise.
- getIterator() : ArrayIterator<int, T>
- getOrCall() : T|S
- Returns the value if available, or the results of the callable.
- getOrElse() : T|S
- Returns the value if available, or the default value if not.
- getOrThrow() : T
- Returns the value if available, or throws the passed exception.
- ifDefined() : void
- This is similar to map() below except that the return value has no meaning; the passed callable is simply executed if the option is non-empty, and ignored if the option is empty.
- isDefined() : bool
- Returns true if a value is available, false otherwise.
- isEmpty() : bool
- Returns true if no value is available, false otherwise.
- lift() : callable
- Lift a function so that it accepts Option as parameters.
- map() : Option<string|int, S>
- Applies the callable to the value of the option if it is non-empty, and returns the return value of the callable wrapped in Some().
- orElse() : Option<string|int, T>
- Returns this option if non-empty, or the passed option otherwise.
- reject() : Option<string|int, T>
- If the option is empty, it is returned immediately.
- select() : Option<string|int, T>
- If the option is empty, it is returned immediately.
Properties
$value
private
T
$value
Methods
__construct()
public
__construct(T $value) : mixed
Parameters
- $value : T
create()
public
static create(U $value) : Some<string|int, U>
Parameters
- $value : U
Tags
Return values
Some<string|int, U>ensure()
Option factory, which creates new option based on passed value.
public
static ensure(Option<string|int, S>|callable|S $value[, S $noneValue = null ]) : Option<string|int, S>|LazyOption<string|int, S>
If value is already an option, it simply returns. If value is callable, LazyOption with passed callback created and returned. If Option returned from callback, it returns directly. On other case value passed to Option::fromValue() method.
Parameters
- $value : Option<string|int, S>|callable|S
- $noneValue : S = null
-
Used when $value is mixed or callable, for None-check.
Tags
Return values
Option<string|int, S>|LazyOption<string|int, S>filter()
If the option is empty, it is returned immediately without applying the callable.
public
filter(mixed $callable) : Option<string|int, T>
If the option is non-empty, the callable is applied, and if it returns true, the option itself is returned; otherwise, None is returned.
Parameters
- $callable : mixed
Return values
Option<string|int, T>filterNot()
If the option is empty, it is returned immediately without applying the callable.
public
filterNot(mixed $callable) : Option<string|int, T>
If the option is non-empty, the callable is applied, and if it returns false, the option itself is returned; otherwise, None is returned.
Parameters
- $callable : mixed
Return values
Option<string|int, T>flatMap()
Applies the callable to the value of the option if it is non-empty, and returns the return value of the callable directly.
public
flatMap(mixed $callable) : Option<string|int, S>
In contrast to map
, the return value of the callable is expected to
be an Option itself; it is not automatically wrapped in Some().
Parameters
- $callable : mixed
-
must return an Option
Return values
Option<string|int, S>foldLeft()
Binary operator for the initial value and the option's value.
public
foldLeft(mixed $initialValue, mixed $callable) : S
If empty, the initial value is returned. If non-empty, the callable receives the initial value and the option's value as arguments.
$some = new Some(5);
$none = None::create();
$result = $some->foldLeft(1, function($a, $b) { return $a + $b; }); // int(6)
$result = $none->foldLeft(1, function($a, $b) { return $a + $b; }); // int(1)
// This can be used instead of something like the following:
$option = Option::fromValue($integerOrNull);
$result = 1;
if ( ! $option->isEmpty()) {
$result += $option->get();
}
Parameters
- $initialValue : mixed
- $callable : mixed
Return values
SfoldRight()
foldLeft() but with reversed arguments for the callable.
public
foldRight(mixed $initialValue, mixed $callable) : S
Parameters
- $initialValue : mixed
- $callable : mixed
Return values
SforAll()
This is similar to map() except that the return value of the callable has no meaning.
public
forAll(mixed $callable) : Option<string|int, T>
The passed callable is simply executed if the option is non-empty, and ignored if the option is empty. This method is preferred for callables with side-effects, while map() is intended for callables without side-effects.
Parameters
- $callable : mixed
Return values
Option<string|int, T>fromArraysValue()
Creates an option from an array's value.
public
static fromArraysValue(array<string|int, S>|ArrayAccess<string|int, S>|null $array, string|int|null $key) : Option<string|int, S>
If the key does not exist in the array, the array is not actually an array, or the array's value at the given key is null, None is returned. Otherwise, Some is returned wrapping the value at the given key.
Parameters
- $array : array<string|int, S>|ArrayAccess<string|int, S>|null
-
A potential array or \ArrayAccess value.
- $key : string|int|null
-
The key to check.
Tags
Return values
Option<string|int, S>fromReturn()
Creates a lazy-option with the given callback.
public
static fromReturn(callable $callback[, array<string|int, mixed> $arguments = [] ][, S $noneValue = null ]) : LazyOption<string|int, S>
This is also a helper constructor for lazy-consuming existing APIs where
the return value is not yet an option. By default, we treat null
as
None case, and everything else as Some.
Parameters
- $callback : callable
-
The callback to evaluate.
- $arguments : array<string|int, mixed> = []
-
The arguments for the callback.
- $noneValue : S = null
-
The value which should be considered "None"; null by default.
Tags
Return values
LazyOption<string|int, S>fromValue()
Creates an option given a return value.
public
static fromValue(S $value[, S $noneValue = null ]) : Option<string|int, S>
This is intended for consuming existing APIs and allows you to easily
convert them to an option. By default, we treat null
as the None
case, and everything else as Some.
Parameters
- $value : S
-
The actual return value.
- $noneValue : S = null
-
The value which should be considered "None"; null by default.
Tags
Return values
Option<string|int, S>get()
Returns the value if available, or throws an exception otherwise.
public
get() : T
Return values
TgetIterator()
public
getIterator() : ArrayIterator<int, T>
Return values
ArrayIterator<int, T>getOrCall()
Returns the value if available, or the results of the callable.
public
getOrCall(mixed $callable) : T|S
This is preferable over getOrElse
if the computation of the default
value is expensive.
Parameters
- $callable : mixed
Return values
T|SgetOrElse()
Returns the value if available, or the default value if not.
public
getOrElse(mixed $default) : T|S
Parameters
- $default : mixed
Return values
T|SgetOrThrow()
Returns the value if available, or throws the passed exception.
public
getOrThrow(Exception $ex) : T
Parameters
- $ex : Exception
Return values
TifDefined()
This is similar to map() below except that the return value has no meaning; the passed callable is simply executed if the option is non-empty, and ignored if the option is empty.
public
ifDefined(mixed $callable) : void
In all cases, the return value of the callable is discarded.
$comment->getMaybeFile()->ifDefined(function($file) {
// Do something with $file here.
});
If you're looking for something like ifEmpty
, you can use getOrCall
and getOrElse
in these cases.
Parameters
- $callable : mixed
isDefined()
Returns true if a value is available, false otherwise.
public
isDefined() : bool
Return values
boolisEmpty()
Returns true if no value is available, false otherwise.
public
isEmpty() : bool
Return values
boollift()
Lift a function so that it accepts Option as parameters.
public
static lift(callable $callback[, mixed $noneValue = null ]) : callable
We return a new closure that wraps the original callback. If any of the parameters passed to the lifted function is empty, the function will return a value of None. Otherwise, we will pass all parameters to the original callback and return the value inside a new Option, unless an Option is returned from the function, in which case, we use that.
Parameters
- $callback : callable
- $noneValue : mixed = null
Tags
Return values
callablemap()
Applies the callable to the value of the option if it is non-empty, and returns the return value of the callable wrapped in Some().
public
map(mixed $callable) : Option<string|int, S>
If the option is empty, then the callable is not applied.
(new Some("foo"))->map('strtoupper')->get(); // "FOO"
Parameters
- $callable : mixed
Return values
Option<string|int, S>orElse()
Returns this option if non-empty, or the passed option otherwise.
public
orElse(Option $else) : Option<string|int, T>
This can be used to try multiple alternatives, and is especially useful with lazy evaluating options:
$repo->findSomething()
->orElse(new LazyOption(array($repo, 'findSomethingElse')))
->orElse(new LazyOption(array($repo, 'createSomething')));
Parameters
- $else : Option
Return values
Option<string|int, T>reject()
If the option is empty, it is returned immediately.
public
reject(mixed $value) : Option<string|int, T>
If the option is non-empty, and its value does equal the passed value (via a shallow comparison ===), then None is returned; otherwise, the Option is returned.
In other words, this will let all values through except the passed value.
Parameters
- $value : mixed
Return values
Option<string|int, T>select()
If the option is empty, it is returned immediately.
public
select(mixed $value) : Option<string|int, T>
If the option is non-empty, and its value does not equal the passed value (via a shallow comparison ===), then None is returned. Otherwise, the Option is returned.
In other words, this will filter all but the passed value.
Parameters
- $value : mixed