Skip to content
Uxman Sarwar edited this page Mar 11, 2025 · 2 revisions

uxmansarwar/response Package Documentation

Introduction

Welcome to the uxmansarwar/response PHP package by Uxman Sarwar a Sr. PHP Developer.

This package provides a structured approach to handling API responses in PHP applications. It simplifies response management by offering dynamic result storage, error handling, and user input retrieval in JSON and array formats.

Developer Info


Features

  • Efficient response management with a singleton-based approach.
  • Dynamic storage for results and errors.
  • Automatic capturing of $_GET, $_POST, and JSON payloads.
  • Supports JSON, array, and custom key-value output formats.
  • Compatible with PHP 7.2 and above.

Installation

Install via Composer:

composer require uxmansarwar/response

Basic Usage

1. Initialize the Package

use UxmanSarwar\Response;

Response::init();

2. Add a Result

Response::result('Data processed successfully');

3. Add an Error

Response::error('Something went wrong');

4. Return a JSON Response

echo Response::json();

Output:

{
  "result": [
    "Data processed successfully"
  ],
  "error": [
    "Something went wrong"
  ]
}

5. Return Response as Array

$responseArray = Response::collection();

6. Include User Input in Response

Response::input(true);
echo Response::json();

Output:

{
  "result": [],
  "error": [],
  "input": {
    "key1": "value1",
    "key2": "value2"
  }
}

7. Using Custom Keys for Results and Errors

Response::key('user')->result('User created successfully');
Response::key('user')->error('Email already exists');

Output:

{
  "result": {
    "user": [
      "User created successfully"
    ]
  },
  "error": {
    "user": [
      "Email already exists"
    ]
  }
}

Advanced Usage

Initializing with User Input

The package automatically merges $_GET, $_POST, and raw JSON input from php://input. You can retrieve all user inputs by enabling input return:

Response::input(true);

Singleton Pattern

The Response class implements the Singleton pattern, ensuring a single instance is used throughout your application:

$instance = Response::init();

Using Collection Method

Retrieve response data as an associative array:

$response = Response::collection();

Testing

This package supports automated testing with PHPStan, PHP-CS-Fixer, and PestPHP.

Install Development Dependencies

composer install --dev

Run PHPStan Static Analysis

vendor/bin/phpstan analyse

Run PestPHP Tests

vendor/bin/pest

Run PHP-CS-Fixer to Ensure Code Formatting

vendor/bin/php-cs-fixer fix

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/YourFeature)
  3. Commit your changes (git commit -m 'feat: Add new feature')
  4. Push to the branch (git push origin feature/YourFeature)
  5. Create a new Pull Request

License

This project is open-source and available under the [MIT License](https://github.com/uxmansarwar/response/blob/master/LICENSE).


Contact

Thank you for using the Response package! 🚀