Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/silasyudi/optional
Portability of Java's java.util.Optional<T> class to PHP, updated with Java 11 features
https://github.com/silasyudi/optional
library optional php
Last synced: about 1 month ago
JSON representation
Portability of Java's java.util.Optional<T> class to PHP, updated with Java 11 features
- Host: GitHub
- URL: https://github.com/silasyudi/optional
- Owner: silasyudi
- License: mit
- Created: 2022-03-23T17:20:42.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-14T20:12:12.000Z (over 2 years ago)
- Last Synced: 2024-11-22T01:20:54.465Z (about 2 months ago)
- Topics: library, optional, php
- Language: PHP
- Homepage: https://packagist.org/packages/silasyudi/optional
- Size: 38.1 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Optional
[![Tests](https://github.com/silasyudi/optional/actions/workflows/tests.yml/badge.svg)](https://github.com/silasyudi/optional/actions/workflows/tests.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/22aefd9a146abde68afd/maintainability)](https://codeclimate.com/github/silasyudi/optional/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/22aefd9a146abde68afd/test_coverage)](https://codeclimate.com/github/silasyudi/optional/test_coverage)Portability of Java's `java.util.Optional` class to PHP, updated with Java 11 features.
## Summary
- [Language / Idioma](#language--idioma)
- [Instalation](#instalation)
- [Requirements](#requirements)
- [Features](#features)
- [Differences](#differences)## Language / Idioma
Leia a versão em português :brazil: [aqui](README_PT_BR.md).
## Instalation
```sh
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:
```php
/** @var Entity|null $entity */
$entity = $this->repository->find($id);if (!$entity) {
throw new SomeException();
}...
```### Example with Optional:
```php
/** @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 in `map`, `flatMap` and `filter`.
* `Optional.hashCode()` was not imported into this package.
* `NullPointerException` e `NoSuchElementException` of the Java was replaced by `OptionalInvalidStateException`
when the Optional object cannot be empty and `TypeError` when attempting to pass null in `callable` parameters.
* `Optional.orElseThrow` in Java 11 is overloaded, and expects no parameter or a Supplier parameter.
In this package, this method expects a Throwable object or `null` as parameter.
* `Consumer`, `Function`, `Predicate` and `Supplier` was imported as `callable`.