silasyudi/optional
最新稳定版本:v2.0.0
Composer 安装命令:
composer require silasyudi/optional
包简介
Portability of Java's java.util.Optional<T> class to PHP.
关键字:
README 文档
README
Portability of Java's java.util.Optional<T> class to PHP, updated with Java 11 features.
Summary
Language / Idioma
Leia a versão em português 🇧🇷 aqui.
Instalation
composer require silasyudi/optional
Requirements
- PHP 7.4+
- Composer
Features
The Optional class encapsulates a value and can perform various operations on it.
Example without Optional:
/** @var Entity|null $entity */ $entity = $this->repository->find($id); if (!$entity) { throw new SomeException(); } ...
Example with Optional:
/** @var SilasYudi\Optional $optional */ $optional = $this->repository->find($id); $entity = $optional->orElseThrow(new SomeException()); ...
Differences
Some differences could not be avoided due to the particularities of each language. The most important are listed below:
Optional.stream()of the Java was not imported into this package, as it doesn't have something similar in PHP and already has similar methods inmap,flatMapandfilter.Optional.hashCode()was not imported into this package.NullPointerExceptioneNoSuchElementExceptionof the Java was replaced byOptionalInvalidStateExceptionwhen the Optional object cannot be empty andTypeErrorwhen attempting to pass null incallableparameters.Optional.orElseThrowin Java 11 is overloaded, and expects no parameter or a Supplier parameter. In this package, this method expects a Throwable object ornullas parameter.Consumer,Function,PredicateandSupplierwas imported ascallable.
统计信息
- 总下载量: 8.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-14