trehinos/thor-safe-types
Composer 安装命令:
composer require trehinos/thor-safe-types
包简介
Some safe types for PHP 8.4 and above.
README 文档
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
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-01