From 136332177756a9ce0ab8aac673e863183768ebb8 Mon Sep 17 00:00:00 2001 From: someniatko Date: Tue, 20 Apr 2021 16:36:26 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ LICENSE | 7 +++++ README.md | 46 ++++++++++++++++++++++++++++++ composer.json | 22 +++++++++++++++ phpcs.xml | 12 ++++++++ psalm.xml | 15 ++++++++++ src/Error.php | 44 +++++++++++++++++++++++++++++ src/ResultInterface.php | 62 +++++++++++++++++++++++++++++++++++++++++ src/Success.php | 43 ++++++++++++++++++++++++++++ 9 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpcs.xml create mode 100644 psalm.xml create mode 100644 src/Error.php create mode 100644 src/ResultInterface.php create mode 100644 src/Success.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de4a392 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor +/composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5f5af55 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2021 someniatko + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..50bfeff --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# someniatko/result-type + +Library representing a generic Result type with Success and Error states, +made for aiding this functional programming pattern in PHP. + +It is generically typed using Psalm annotations, so you are expected to use Psalm if +you want typechecking of your code. + +**Why using this library if `graham-campbell/result-type` exists?** +Unfortunately, there are several downsides of that library: +- while it's also typed with Psalm annotations, actually using and checking against them might be painful. +- it's coupled to the `phpoption/phpoption` library which unfortunately suffers from the same problem. +- there is no convenience method in Result interface which would allow getting either value from it. + + +## Installation +This library requires PHP 7.4 or 8.0. +You can install it via Composer: + +```shell +composer install someniatko/result-type +``` + + +## Usage + +See [`ResultInterface`](src/ResultInterface.php) for details. + +Example: + +```php +map(fn (string $s) => substr_count($s, ' ')) // Success<2> + ->chain(fn (int $wordsCount) => $wordsCount > 3 + ? new Success('Long text') + : new Error('short text') + ) // Error<'short text'> + ->map(fn (string $s) => str_replace(' ', '', $s)) // will not be called because we're in Error result + ->mapError(fn (string $s) => strtoupper($s)) // Error<'SHORT TEXT'> + ->get(); // 'SHORT TEXT' +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..986f0f5 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "someniatko/result-type", + "license": "MIT", + "description": "Functional-ish generic Result type: either Success or Error", + "require": { + "php": "~7.4 || ~8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "psalm/phar": "^4.7" + }, + "autoload": { + "psr-4": { + "Someniatko\\ResultType\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Test\\": "test/" + } + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..9241b03 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,12 @@ + + + src + test + + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..8802209 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + diff --git a/src/Error.php b/src/Error.php new file mode 100644 index 0000000..bcf4e8d --- /dev/null +++ b/src/Error.php @@ -0,0 +1,44 @@ + + * @psalm-immutable + */ +final class Error implements ResultInterface +{ + /** @var TError */ + private $value; + + /** @param TError $value */ + public function __construct($value) + { + $this->value = $value; + } + + public function map(callable $map): ResultInterface + { + // no success value, so nothing to map. + return $this; + } + + public function mapError(callable $map): ResultInterface + { + return new self($map($this->value)); + } + + public function chain(callable $map): ResultInterface + { + // no success value, so nothing to map. + return $this; + } + + public function get() + { + return $this->value; + } +} diff --git a/src/ResultInterface.php b/src/ResultInterface.php new file mode 100644 index 0000000..d32ca79 --- /dev/null +++ b/src/ResultInterface.php @@ -0,0 +1,62 @@ + + */ + public function map(callable $map): self; + + /** + * Takes a callable which maps **error** value to a new value, returns new ResultInterface. + * The callable will be called only if this result is Error. + * + * If this result is Success, returns it as is. + * If this result is Error, returns new Error with changed value. + + * @template TNewError + * + * @param callable(TError):TNewError $map + * @return self + */ + public function mapError(callable $map): self; + + /** + * Chains Success path processing. May either just change Success value, or change the result type to Error. + * Takes a callable which takes **success** value and returns new ResultInterface. + * The callable will be called only if this result is Success. + * + * @template TNewSuccess + * @template TNewError + * + * @param callable(TSuccess):self $map + * @return self + */ + public function chain(callable $map): self; + + /** + * Returns the final value of this result. + * The value will be returned for both Success and Error cases. + * + * @return TSuccess|TError + */ + public function get(); +} diff --git a/src/Success.php b/src/Success.php new file mode 100644 index 0000000..2f00041 --- /dev/null +++ b/src/Success.php @@ -0,0 +1,43 @@ + + * @psalm-immutable + */ +final class Success implements ResultInterface +{ + /** @var TSuccess */ + private $value; + + /** @param TSuccess $value */ + public function __construct($value) + { + $this->value = $value; + } + + public function map(callable $map): ResultInterface + { + return new self($map($this->value)); + } + + public function mapError(callable $map): ResultInterface + { + // no error, so nothing to map. + return $this; + } + + public function chain(callable $map): ResultInterface + { + return $map($this->value); + } + + public function get() + { + return $this->value; + } +}