result-type/composer.json000064400000001572150247600010011563 0ustar00{ "name": "graham-campbell/result-type", "description": "An Implementation Of The Result Type", "keywords": ["result", "result-type", "Result", "Result Type", "Result-Type", "Graham Campbell", "GrahamCampbell"], "license": "MIT", "authors": [ { "name": "Graham Campbell", "email": "hello@gjcampbell.co.uk", "homepage": "https://github.com/GrahamCampbell" } ], "require": { "php": "^7.2.5 || ^8.0", "phpoption/phpoption": "^1.9" }, "require-dev": { "phpunit/phpunit": "^8.5.28 || ^9.5.21" }, "autoload": { "psr-4": { "GrahamCampbell\\ResultType\\": "src/" } }, "autoload-dev": { "psr-4": { "GrahamCampbell\\Tests\\ResultType\\": "tests/" } }, "config": { "preferred-install": "dist" } } result-type/src/Error.php000064400000004324150247600010011430 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace GrahamCampbell\ResultType; use PhpOption\None; use PhpOption\Some; /** * @template T * @template E * @extends \GrahamCampbell\ResultType\Result */ final class Error extends Result { /** * @var E */ private $value; /** * Internal constructor for an error value. * * @param E $value * * @return void */ private function __construct($value) { $this->value = $value; } /** * Create a new error value. * * @template F * * @param F $value * * @return \GrahamCampbell\ResultType\Result */ public static function create($value) { return new self($value); } /** * Get the success option value. * * @return \PhpOption\Option */ public function success() { return None::create(); } /** * Map over the success value. * * @template S * * @param callable(T):S $f * * @return \GrahamCampbell\ResultType\Result */ public function map(callable $f) { return self::create($this->value); } /** * Flat map over the success value. * * @template S * @template F * * @param callable(T):\GrahamCampbell\ResultType\Result $f * * @return \GrahamCampbell\ResultType\Result */ public function flatMap(callable $f) { /** @var \GrahamCampbell\ResultType\Result */ return self::create($this->value); } /** * Get the error option value. * * @return \PhpOption\Option */ public function error() { return Some::create($this->value); } /** * Map over the error value. * * @template F * * @param callable(E):F $f * * @return \GrahamCampbell\ResultType\Result */ public function mapError(callable $f) { return self::create($f($this->value)); } } result-type/src/Result.php000064400000002532150247600010011614 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace GrahamCampbell\ResultType; /** * @template T * @template E */ abstract class Result { /** * Get the success option value. * * @return \PhpOption\Option */ abstract public function success(); /** * Map over the success value. * * @template S * * @param callable(T):S $f * * @return \GrahamCampbell\ResultType\Result */ abstract public function map(callable $f); /** * Flat map over the success value. * * @template S * @template F * * @param callable(T):\GrahamCampbell\ResultType\Result $f * * @return \GrahamCampbell\ResultType\Result */ abstract public function flatMap(callable $f); /** * Get the error option value. * * @return \PhpOption\Option */ abstract public function error(); /** * Map over the error value. * * @template F * * @param callable(E):F $f * * @return \GrahamCampbell\ResultType\Result */ abstract public function mapError(callable $f); } result-type/src/Success.php000064400000004222150247600010011744 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace GrahamCampbell\ResultType; use PhpOption\None; use PhpOption\Some; /** * @template T * @template E * @extends \GrahamCampbell\ResultType\Result */ final class Success extends Result { /** * @var T */ private $value; /** * Internal constructor for a success value. * * @param T $value * * @return void */ private function __construct($value) { $this->value = $value; } /** * Create a new error value. * * @template S * * @param S $value * * @return \GrahamCampbell\ResultType\Result */ public static function create($value) { return new self($value); } /** * Get the success option value. * * @return \PhpOption\Option */ public function success() { return Some::create($this->value); } /** * Map over the success value. * * @template S * * @param callable(T):S $f * * @return \GrahamCampbell\ResultType\Result */ public function map(callable $f) { return self::create($f($this->value)); } /** * Flat map over the success value. * * @template S * @template F * * @param callable(T):\GrahamCampbell\ResultType\Result $f * * @return \GrahamCampbell\ResultType\Result */ public function flatMap(callable $f) { return $f($this->value); } /** * Get the error option value. * * @return \PhpOption\Option */ public function error() { return None::create(); } /** * Map over the error value. * * @template F * * @param callable(E):F $f * * @return \GrahamCampbell\ResultType\Result */ public function mapError(callable $f) { return self::create($this->value); } } result-type/LICENSE000064400000002130150247600010010035 0ustar00The MIT License (MIT) Copyright (c) 2020-2022 Graham Campbell 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.