manhhuyvo / action-response
There is no license information available for the latest version (v1.0.1) of this package.
A fluent, structured PHP action response class which supports messages, errors, and data.
v1.0.1
2025-12-06 04:21 UTC
Requires
- php: >=8.1
- illuminate/support: ^12.32
Requires (Dev)
- phpunit/phpunit: ^12.4
- symfony/var-dumper: ^7.3
This package is auto-updated.
Last update: 2026-03-06 04:50:54 UTC
README
A lightweight and expressive response helper designed to provide a consistent structure when returning action results in PHP applications.
This package includes:
- A
Responseclass with a fluent API - A
ResponseStatusenum (success,error,snooze) - Convenience constructors (
success(),error(),snooze()) - Helpers for attaching messages, errors, data, and retrieving nested data via dot notation
๐ฆ Installation
Install via Composer:
composer require manhhuyvo/action-response
๐ Basic Usage
Create a success response
use ManhHuyVo\ActionResponse\Response; $response = Response::success() ->message('Action completed successfully.') ->data(['foo' => 'bar']); return $response;
Create an error response
$response = Response::error() ->message('Unable to process request.') ->errors(['Invalid input', 'Another error']); return $response;
Create a snooze response
Useful when the action should be skipped or is not applicable.
$response = Response::snooze() ->message('Skipping this action for now.'); return $response;
๐งฑ Response Structure
A Response object contains:
| Property | Type | Description |
|---|---|---|
status |
string | One of success, error, snooze |
message |
string | Optional explanation |
errors |
array | Unique list of errors (duplicates removed automatically) |
data |
array | Optional payload |
Example structure:
[
'status' => 'success',
'message' => 'Action completed successfully.',
'errors' => [],
'data' => [
'foo' => 'bar'
],
]
๐ Status Checking
$response->isSuccessful(); // true / false $response->isError(); // true / false $response->isSnooze(); // true / false
๐ฏ Setting Values Manually
$response = (new Response()) ->status('success') ->message('Done') ->data(['x' => 1]) ->errors([]); return $response;
๐ฅ Accessing Nested Data (Dot Notation)
If your data is nested:
$response = Response::success()->data([ 'user' => [ 'profile' => [ 'email' => 'john@example.com' ], ], ]);
You can fetch nested values:
$response->getData('user.profile.email'); // "john@example.com"
๐งฉ ResponseStatus Enum
enum ResponseStatus: string { case Success = 'success'; case Error = 'error'; case Snooze = 'snooze'; }
๐งช Running Tests
If you are developing locally, run PHPUnit:
vendor/bin/phpunit --testdox
๐ Example Usage in a Real Action
use ManhHuyVo\ActionResponse\Response; function processOrder(array $order): Response { if (empty($order)) { return Response::error() ->message('Order cannot be empty.') ->errors(['order_empty']); } if ($order['status'] === 'skipped') { return Response::snooze() ->message('Order skipped.'); } return Response::success() ->message('Order processed.') ->data(['id' => 123]); }
๐ License
MIT License.