Download the PHP package ifixit/php7-optional without Composer
On this page you can find all versions of the php package ifixit/php7-optional. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download ifixit/php7-optional
More information about ifixit/php7-optional
Files in ifixit/php7-optional
Download ifixit/php7-optional
More information about ifixit/php7-optional
Files in ifixit/php7-optional
Vendor ifixit
Package php7-optional
Short Description PHP 7 optional wrapper that provides an alternative to null
License MIT
Package php7-optional
Short Description PHP 7 optional wrapper that provides an alternative to null
License MIT
Please rate this library. Is it a good library?
Informations about the package php7-optional
php7-optional
Installation
Usage
There are 3 main classes:
- Optional\Option
- Conceptually: Some or None. This is a box that optionally holds a value. Use this to replace null.
- Optional\Either
- Conceptually: Left or Right. This is a box that is bi-state. Can be Left, or Right. Not Both, not none. You can apply the same transformations to the left side as you can to the right.
- Optional\Result
- Conceptually: Okay or Error. This is a box that is bi-state, but leans towards okay state. The error state is limited as we want to make this object easy to deal with mapping or dump error.
- All callables have
Throwable
auto wrapped and thrown at the end of the chain.
- Optional\UnsafeResult
- Conceptually: Okay or Error. This is a box that is bi-state, but leans towards okay state. The error state is limited as we want to make this object easy to deal with mapping or dump error.
- All callables do not have
Throwable
auto wrapped. Thus, an Exception will be thrown immediately.
Read the full docs
Using Option
To use Optional, simply import the following namespace:
Creating optional values
There are examples under examples
.
Here is one of them which will show how fluently you can describe the chnage you want to apply.
Using Either
To use Either, simply import the following namespace:
Using Result
To use Result, simply import the following namespace:
Result Methods
Creation (Boxing)
- static okay($data): Result
- static error(Throwable $errorData): Result
- static okayWhen($data, Throwable $errorValue, callable $filterFunc): Result
- static errorWhen($data, Throwable $errorValue, callable $filterFunc): Result
- static okayNotNull($data, Throwable $errorValue): Result
- static fromArray(array $array, $key, Throwable $rightValue = null): Result
Flipping
- toError($errorValue): Result
- toOkay($dataValue): Result
Unboxing
- dataOrThrow()
State
- isOkay(): bool
- isError(): bool
- contains($value): bool
- errorContains($value): bool
- exists(callable $existsFunc): bool
Transformation
- orSetDataTo($data): Result
- orCreateResultWithData(callable $alternativeFactory): Result
- okayOr(self $alternativeResult): Result
- createIfError(callable $alternativeResultFactory): Result
- map(callable $mapFunc): Result
- mapError(callable $mapFunc): Result
- andThen(callable $mapFunc): Result
- flatMap(callable $mapFunc): Result
- toErrorIf(callable $filterFunc, Throwable $errorValue): Result
- toOkayIf(callable $filterFunc, $data): Result
- notNull(Throwable $errorValue): Result
- notFalsy(Throwable $errorValue): Result
Side Effect
- run(callable $dataFunc, callable $errorFunc)
- runOnOkay(callable $dataFunc): void
- runOnError(callable $errorFunc): void
Licence
MIT
Special Thanks
Heavily inspired by https://github.com/nlkl/Optional. In fact this is essentially a port of this library.
All versions of php7-optional with dependencies
PHP Build Version
Package Version
Requires
php Version
^7.2 || ^8.0
The package ifixit/php7-optional contains the following files
Loading the files please wait ....