Result Flow
Result Flow gives PHP 8.2+ a small, explicit Result type for success, failure, and metadata.
The model is simple:
text
Ok(value, meta) | Fail(error, meta)That simplicity is the point. It keeps ordinary failures in the type system, so you can compose work, preserve context, and finish at a boundary on purpose.
php
use Maxiviper117\ResultFlow\Result;
$result = Result::ok(['email' => 'dev@example.com'], ['request_id' => 'r-1'])
->ensure(fn (array $input) => isset($input['email']), 'Email is required')
->then(fn (array $input) => Result::ok([
'email' => strtolower($input['email']),
], ['operation' => 'normalize-email']))
->otherwise(fn ($error, array $meta) => Result::fail([
'message' => (string) $error,
'request_id' => $meta['request_id'] ?? null,
], $meta));
// Finish explicitly at the edge.
$payload = $result->match(
onSuccess: fn (array $value, array $meta) => ['ok' => true, 'data' => $value, 'meta' => $meta],
onFailure: fn ($error, array $meta) => ['ok' => false, 'error' => $error, 'meta' => $meta],
);Start here
What to read next
- Learn the mental model in Result model
- Get the full method tour in Kitchen sink
- See the common flow shape in Chaining
- Learn where to stop in Finalization boundaries
- Look up exact signatures in Reference
- Read practical patterns in Guides
- Jump to concrete problems in Recipes
- If you use Boost, read Laravel Boost