### Install Result Type with Composer Source: https://github.com/grahamcampbell/result-type/blob/1.1/README.md Use this command to add the latest version of the Result Type package to your project. Ensure you are using PHP 7.2.5 or higher. ```bash $ composer require "graham-campbell/result-type:^1.1" ``` -------------------------------- ### Access Results with success() and error() using Option wrappers Source: https://context7.com/grahamcampbell/result-type/llms.txt The `success()` and `error()` methods return `PhpOption\Option` instances, providing safe value access without null checks. Use `getOrElse()` for safe retrieval with a fallback value, or `get()` for direct retrieval which throws an exception if the Option is empty. ```php success()->isDefined()); // bool(true) var_dump($ok->error()->isEmpty()); // bool(true) var_dump($err->error()->isDefined()); // bool(true) var_dump($err->success()->isEmpty()); // bool(true) // getOrElse() — safe retrieval with fallback $value = $ok->success()->getOrElse(0); // 99 $value = $err->success()->getOrElse(0); // 0 // get() — direct retrieval (throws RuntimeException if None) $value = $ok->success()->get(); // 99 // $err->success()->get() would throw: "None has no value." // orElse() — return an alternative Option $fallback = $err->success()->orElse(\PhpOption\Some::create(-1)); echo $fallback->get(); // -1 ``` -------------------------------- ### Create Error Result Source: https://context7.com/grahamcampbell/result-type/llms.txt Wrap an error value using `Error::create()`. The `error()` accessor returns `Some($value)` and `success()` returns `None`. Attempting `get()` on the `None` side will throw a `RuntimeException`. ```php error()->isDefined()); // bool(true) var_dump($result->success()->isEmpty()); // bool(true) $message = $result->error()->get(); // 'User not found' // Safely read error with fallback — never throws $message = $result->error()->getOrElse('Unknown error'); // Attempting ->get() on the None side throws RuntimeException try { $result->success()->get(); } catch (\RuntimeException $e) { echo $e->getMessage(); // "None has no value." } ``` -------------------------------- ### Create Success Result Source: https://context7.com/grahamcampbell/result-type/llms.txt Wrap a successful value using `Success::create()`. The `success()` accessor returns `Some($value)` and `error()` returns `None`. Safe retrieval with `getOrElse()` is recommended. ```php 42, 'name' => 'Alice']); // Check and retrieve success value $successOption = $result->success(); // PhpOption\Some $errorOption = $result->error(); // PhpOption\None var_dump($successOption->isDefined()); // bool(true) var_dump($errorOption->isEmpty()); // bool(true) $user = $successOption->get(); // ['id' => 42, 'name' => 'Alice'] // Safe retrieval with a fallback (never throws) $user = $result->success()->getOrElse([]); ``` -------------------------------- ### Result::success() and Result::error() Source: https://context7.com/grahamcampbell/result-type/llms.txt Access Option wrappers for success and error values. Both methods return a PhpOption\Option instance. `success()` returns `Some(value)` on a `Success` and `None` on an `Error`; `error()` is the inverse. The `Option` API provides safe value access without null checks. ```APIDOC ## `Result::success()` and `Result::error()` — Access Option wrappers Both methods return a `PhpOption\Option` instance. `success()` returns `Some(value)` on a `Success` and `None` on an `Error`; `error()` is the inverse. The `Option` API provides safe value access without null checks. ```php success()->isDefined()); // bool(true) var_dump($ok->error()->isEmpty()); // bool(true) var_dump($err->error()->isDefined()); // bool(true) var_dump($err->success()->isEmpty()); // bool(true) // getOrElse() — safe retrieval with fallback $value = $ok->success()->getOrElse(0); // 99 $value = $err->success()->getOrElse(0); // 0 // get() — direct retrieval (throws RuntimeException if None) $value = $ok->success()->get(); // 99 // $err->success()->get() would throw: "None has no value." // orElse() — return an alternative Option $fallback = $err->success()->orElse(\PhpOption\Some::create(-1)); echo $fallback->get(); // -1 ``` ``` -------------------------------- ### Success::create($value) Source: https://context7.com/grahamcampbell/result-type/llms.txt Creates a Result instance representing a successful outcome, wrapping any value. The success() accessor returns Some($value) and error() returns None. ```APIDOC ## Success::create($value) - Wrap a successful value ### Description Creates a `Result` instance representing a successful outcome, wrapping any value of type `T`. The `success()` accessor returns `Some($value)` and `error()` returns `None`. ### Method `Success::create($value)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php 42, 'name' => 'Alice']); // Check and retrieve success value $successOption = $result->success(); // PhpOption\Some $errorOption = $result->error(); // PhpOption\None var_dump($successOption->isDefined()); // bool(true) var_dump($errorOption->isEmpty()); // bool(true) $user = $successOption->get(); // ['id' => 42, 'name' => 'Alice'] // Safe retrieval with a fallback (never throws) $user = $result->success()->getOrElse([]); ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Chain Result-Returning Operations with flatMap Source: https://context7.com/grahamcampbell/result-type/llms.txt Use `flatMap` to chain operations that return a `Result`. The callable is skipped if the initial `Result` is an `Error`, and the error propagates. This method is useful for building pipelines of operations that may fail. ```php 0 ? Success::create((int)$input) : Error::create("'{$input}' is not a positive integer"); } function divide(int $numerator, int $denominator): Result { return $denominator !== 0 ? Success::create($numerator / $denominator) : Error::create('Division by zero'); } // Chain: parse input, then divide $result = parsePositiveInt('100') ->flatMap(fn(int $n) => divide(200, $n)); echo $result->success()->get(); // 2 // Short-circuits on first failure $result = parsePositiveInt('abc') ->flatMap(fn(int $n) => divide(200, $n)); echo $result->error()->get(); // "'abc' is not a positive integer" // flatMap returning Error mid-chain $result = Success::create('foo') ->flatMap(fn(string $s) => Error::create('OH NO')); echo $result->error()->get(); // 'OH NO' ``` -------------------------------- ### Error::create($value) Source: https://context7.com/grahamcampbell/result-type/llms.txt Creates a Result instance representing a failed outcome, wrapping any error value. The error() accessor returns Some($value) and success() returns None. ```APIDOC ## Error::create($value) - Wrap an error value ### Description Creates a `Result` instance representing a failed outcome, wrapping any error value of type `E`. The `error()` accessor returns `Some($value)` and `success()` returns `None`. ### Method `Error::create($value)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php error()->isDefined()); // bool(true) var_dump($result->success()->isEmpty()); // bool(true) $message = $result->error()->get(); // 'User not found' // Safely read error with fallback — never throws $message = $result->error()->getOrElse('Unknown error'); // Attempting ->get() on the None side throws RuntimeException try { $result->success()->get(); } catch (\RuntimeException $e) { echo $e->getMessage(); // "None has no value." } ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Transform Success Value with map() Source: https://context7.com/grahamcampbell/result-type/llms.txt Use `map()` to apply a callable to the success value, returning a new `Success`. When called on an `Error`, `map()` is a no-op and the original error is returned. ```php map('strtoupper'); echo $result->success()->get(); // 'HELLO' // map() on an Error is a no-op — error passes through $result = Error::create('something went wrong')->map('strtoupper'); echo $result->error()->get(); // 'something went wrong' // Practical example: parse and transform an API response function fetchUsername(int $id): Result { // Simulate a lookup return $id > 0 ? Success::create('alice') : Error::create('Invalid ID'); } $display = fetchUsername(1) ->map(fn(string $name) => strtoupper($name)) // 'ALICE' ->map(fn(string $name) => "Welcome, {$name}!"); // 'Welcome, ALICE!' echo $display->success()->getOrElse('Error occurred'); // 'Welcome, ALICE!' ``` -------------------------------- ### Result::flatMap(callable $f) Source: https://context7.com/grahamcampbell/result-type/llms.txt Applies a callable to the success value where the callable itself returns a Result. This enables safe chaining of operations that may themselves fail, without nesting. When called on an Error, the callable is skipped and the original error propagates. ```APIDOC ## `Result::flatMap(callable $f)` — Chain result-returning operations Applies a callable to the success value where the callable itself returns a `Result`. This enables safe chaining of operations that may themselves fail, without nesting. When called on an `Error`, the callable is skipped and the original error propagates. ```php 0 ? Success::create((int)$input) : Error::create("'{$input}' is not a positive integer"); } function divide(int $numerator, int $denominator): Result { return $denominator !== 0 ? Success::create($numerator / $denominator) : Error::create('Division by zero'); } // Chain: parse input, then divide $result = parsePositiveInt('100') ->flatMap(fn(int $n) => divide(200, $n)); echo $result->success()->get(); // 2 // Short-circuits on first failure $result = parsePositiveInt('abc') ->flatMap(fn(int $n) => divide(200, $n)); echo $result->error()->get(); // "'abc' is not a positive integer" // flatMap returning Error mid-chain $result = Success::create('foo') ->flatMap(fn(string $s) => Error::create('OH NO')); echo $result->error()->get(); // 'OH NO' ``` ``` -------------------------------- ### Result::map(callable $f) Source: https://context7.com/grahamcampbell/result-type/llms.txt Applies a callable to the wrapped success value and returns a new Success. If called on an Error, the callable is ignored and the original error is returned. ```APIDOC ## Result::map(callable $f) - Transform a success value ### Description Applies a callable to the wrapped success value and returns a new `Success` wrapping the transformed result. When called on an `Error`, the callable is ignored and the original error is returned unchanged. ### Method `Result::map(callable $f)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php map('strtoupper'); echo $result->success()->get(); // 'HELLO' // map() on an Error is a no-op — error passes through $result = Error::create('something went wrong')->map('strtoupper'); echo $result->error()->get(); // 'something went wrong' // Practical example: parse and transform an API response function fetchUsername(int $id): Result { // Simulate a lookup return $id > 0 ? Success::create('alice') : Error::create('Invalid ID'); } $display = fetchUsername(1) ->map(fn(string $name) => strtoupper($name)) // 'ALICE' ->map(fn(string $name) => "Welcome, {$name}!"); // 'Welcome, ALICE!' echo $display->success()->getOrElse('Error occurred'); // 'Welcome, ALICE!' ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Result::mapError(callable $f) Source: https://context7.com/grahamcampbell/result-type/llms.txt Applies a callable to the wrapped error value and returns a new Error. If called on a Success, the callable is ignored and the original success is returned. ```APIDOC ## Result::mapError(callable $f) - Transform an error value ### Description Applies a callable to the wrapped error value and returns a new `Error` wrapping the transformed result. When called on a `Success`, the callable is ignored and the original success is returned unchanged. ### Method `Result::mapError(callable $f)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php mapError('ucfirst'); echo $result->error()->get(); // 'Not found' // mapError() on a Success is a no-op $result = Success::create('data')->mapError('strtoupper'); echo $result->success()->get(); // 'data' // Practical example: enrich error with context $result = Error::create('connection refused') ->mapError(fn(string $msg) => "[DB Error] {$msg}"); echo $result->error()->get(); // '[DB Error] connection refused' ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Transform Error Value with mapError() Source: https://context7.com/grahamcampbell/result-type/llms.txt Use `mapError()` to apply a callable to the error value, returning a new `Error`. When called on a `Success`, `mapError()` is a no-op and the original success is returned. ```php mapError('ucfirst'); echo $result->error()->get(); // 'Not found' // mapError() on a Success is a no-op $result = Success::create('data')->mapError('strtoupper'); echo $result->success()->get(); // 'data' // Practical example: enrich error with context $result = Error::create('connection refused') ->mapError(fn(string $msg) => "[DB Error] {$msg}"); echo $result->error()->get(); // '[DB Error] connection refused' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.