trehinos / thor-safe-types
Some safe types for PHP 8.4 and above.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/trehinos/thor-safe-types
Requires
- php: ^8.4
Requires (Dev)
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2025-10-09 18:47:33 UTC
README
Provides some safe types.
Interfaces
Unwrap&Matchable, extended by :OptionEitherValidate
Types hierarchy
Validator: a derivableClosureclass which encapsulate afn(mixed): bool.- Unwrap :
final Result(withenum ResultType),- with Matchable :
- Option (with
enum Maybe) :abstract SomeOrNone:final Somefinal None,
- Either (with
enum EitherCase) :final Neitherfinal Both,abstract LeftOrRight:final Leftfinal 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$ifNotand 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,EitherandValidate.- Implemented partially by the trait
UnwrapOrThrow:
- needs
unwrapOrElse().- Implemented by the final class
Result.
Matchable
match(callable $ifA, callable $ifB): Call the function$ifAif some conditions are met or else$ifB. It returns the value returned by the called function.
- Extended by the interfaces
Option,EitherandValidate.
Option
Extends
UnwrapandMatchable.
The Maybe enumeration has the cases SOME and NONE.
is(): Maybe: returns the nature of the OptionMaybe::SOMEorMaybe::NONE.isNone(): bool: this function returntrueif the Option contains no value.isSome(): bool: this function returnfalseif the Option contains a value.isA(Maybe $maybe): bool: returns true if theOptioncorresponds 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 anOptionaccording to the specified value.
- Partially implemented by the abstract class
SomeOrNone:
- needs
match()andis(),- Implemented by the final types
SomeandNone.
Either
Extends
UnwrapandMatchable.
The EitherCase enumeration has the cases LEFT and RIGHT.
isA(EitherCAse): boolisLeft(): booltoLeft(): Leftleft(): OptionleftOr(mixed): mixedisRight(): booltoRight(): Rightright(): OptionrightOr(mixed): mixed
- Partially implemented by the abstract class
LeftOrRight:
- needs
is(),- implemented by the final types
LeftandRight,- Implemented by the final types
BothandNeither.
Validate
Extends
UnwrapandMatchable.
toOption(): OptionisValid(): boolisInvalid(): bool
Implemented by
Validated.
License
Copyright 2025 Sébastien GELDREICH
License MIT