Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/monsieurluge/result
A result-oriented ecosystem, written in PHP
https://github.com/monsieurluge/result
Last synced: 6 days ago
JSON representation
A result-oriented ecosystem, written in PHP
- Host: GitHub
- URL: https://github.com/monsieurluge/result
- Owner: monsieurluge
- License: mit
- Archived: true
- Created: 2018-11-29T15:32:45.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-09T21:18:55.000Z (almost 5 years ago)
- Last Synced: 2024-07-27T22:40:42.366Z (4 months ago)
- Language: PHP
- Size: 142 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Result
![logo](logo.png)
The goal of the Result library is to say goodbye to the `if` and `try catch` control structures when requesting a storage or any object who can return either "nothing" or the desired result.
The code also becomes more declarative and object oriented.
## Examples
### Using a repository
Context: We want to send a message to an user, only known by his ID.
#### As usually seen
```php
userById(1234);if (true === is_null($user)) {
// error handling
} else {
$user->sendMessage('Hi!');
}
```#### With Result
```php
;
}// ---------------------------------------- implementation
$userRepository = new MySqlUserRepository();
$userRepository
->userById(1234)
->then(function (User $user) {
$user->sendMessage('Hi!');
})
->else(function (Error $error) {
// error handling
});
```### Within a HTTP controller
Context: We want to fetch an user name and return it using an HTTP#200 response if the user is known, or via an HTTP#404 if the user is unknown.
#### As usually seen
```php
userRepository = $userRepository;
}public function handle(int $uuid): Response {
$user = $this->userRepository->userById($uuid);if (true === is_null($user)) {
$response = new Response(
sprintf('the user %s does not exist', $uuid),
404
);
} else {
$response = new Response($user->name(), 200);
}return $response;
}
}
```#### With Result
```php
;
}// ---------------------------------------- implementation
class UserNameController
{
private $userRepository;public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}public function handle(int $uuid): Response {
return $this->userRepository
->userById($uuid)
->map(function (User $user) {
return new Response($user->name(), 200);
})
->getOr(function (Error $error) use ($uuid) {
return new Response(
sprintf('the user %s does not exist', $uuid),
404
);
});
}
}
```### How to manage multiple uncertainties ?
Context: We want to add an user to a group. But only the corresponding IDs are provided. Both may not exist.
#### As usually seen
```php
groupRepository->groupById($groupUuid);
$user = $this->userRepository->userById($userUuid);if (true === is_null($user) || true === is_null($user)) {
// error handling
} else {
$group->add($user);
}
```#### With Result
```php
;
}interface GroupRepository
{
public function groupById(int $uuid): Result;
}// ---------------------------------------- implementation
(new Combined([
$this->groupRepository->groupById($groupUuid),
$this->userRepository->userById($userUuid),
]))
->then(function (Group $group, User $user) {
$group->add($user);
})
->else(function (Error $error) {
/** error handling */
});
```or
```php
groupRepository
->groupById($groupUuid)
->join($this->userRepository->userById($userUuid))
->then(function (Group $group, User $user) {
$group->add($user);
})
->else(function (Error $error) {
/** error handling */
});
```