trehinos / thor-safe-types
Some safe types for PHP 8.4 and above.
dev-master
2025-02-01 08:24 UTC
Requires
- php: ^8.4
Requires (Dev)
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2025-04-29 09:17:54 UTC
README
Provides some safe types.
Interfaces
Unwrap
&Matchable
, extended by :Option
Either
Validate
Types hierarchy
Validator
: a derivableClosure
class which encapsulate afn(mixed): bool
.- Unwrap :
final Result
(withenum ResultType
),- with Matchable :
- Option (with
enum Maybe
) :abstract SomeOrNone
:final Some
final None
,
- Either (with
enum EitherCase
) :final Neither
final Both
,abstract LeftOrRight
:final Left
final Right
,
- Validate :
final Validated
- Option (with
Types details
Unwrap
An interface defining utility methods for extracting contained values from a structure.
unwrapOrElse(callable $ifNot): mixed
: returns the contained value if it is safe to return or else calls the function$ifNot
and returns its returned value.unwrapOr(mixed $default): mixed
: returns the contained value if it is safe to return or else returns$default
.unwrapOrThrow(Throwable): mixed
: returns the contained value if it is safe to return or else throws the specifiedThrowable
.unwrap(): mixed
: returns the contained value if it is safe to return or else throws a predefinedThrowable
.
- Extended by the interfaces
Option
,Either
andValidate
.- Implemented partially by the trait
UnwrapOrThrow
:
- needs
unwrapOrElse()
.- Implemented by the final class
Result
.
Matchable
match(callable $ifA, callable $ifB)
: Call the function$ifA
if some conditions are met or else$ifB
. It returns the value returned by the called function.
- Extended by the interfaces
Option
,Either
andValidate
.
Option
Extends
Unwrap
andMatchable
.
The Maybe
enumeration has the cases SOME
and NONE
.
is(): Maybe
: returns the nature of the OptionMaybe::SOME
orMaybe::NONE
.isNone(): bool
: this function returntrue
if the Option contains no value.isSome(): bool
: this function returnfalse
if the Option contains a value.isA(Maybe $maybe): bool
: returns true if theOption
corresponds the nature described by the specifiedMaybe
.map(callable(mixed): mixed $f): $this
: returns aSome($value)
if thisOption::isSome()
else returns aNone
.from(mixed $data): static
: creates anOption
according to the specified value.
- Partially implemented by the abstract class
SomeOrNone
:
- needs
match()
andis()
,- Implemented by the final types
Some
andNone
.
Either
Extends
Unwrap
andMatchable
.
The EitherCase
enumeration has the cases LEFT
and RIGHT
.
isA(EitherCAse): bool
isLeft(): bool
toLeft(): Left
left(): Option
leftOr(mixed): mixed
isRight(): bool
toRight(): Right
right(): Option
rightOr(mixed): mixed
- Partially implemented by the abstract class
LeftOrRight
:
- needs
is()
,- implemented by the final types
Left
andRight
,- Implemented by the final types
Both
andNeither
.
Validate
Extends
Unwrap
andMatchable
.
toOption(): Option
isValid(): bool
isInvalid(): bool
Implemented by
Validated
.
License
Copyright 2025 Sébastien GELDREICH
License MIT