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.

Maintainers

Package info

github.com/manhhuyvo/action-response

pkg:composer/manhhuyvo/action-response

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.1 2025-12-06 04:21 UTC

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 Response class with a fluent API
  • A ResponseStatus enum (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.