neography7/easy-response

Standardized response for REST APIs with easy usability.

1.0.0 2023-06-30 18:29 UTC

This package is auto-updated.

Last update: 2025-01-08 01:02:23 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Easy Response allows you to create REST Callbacks in an easy way. This package has 3 ways to create callbacks which you can use what you want. But if it's suitable for you, the helpers will be more easy and clean.

This package follows PSR-2 and PSR-4 standards.

Installation

You can install the package via composer:

composer require neography7/easy-response

Usage

There are three ways to use this package but using it with the helpers is recommended.

1) Helpers

easySuccess helper

easySuccess( message, title = null, data = null);
easySuccess(
    message: 'Test message',
    title: 'Test Title',
    data: [
        "key" => "value"
    ]
);

or like this

easySuccess('Test message', 'Test Title', [ "key" => "value" ]);

easyError helper

easyError( message, title = null, code = null, data = null);
easyError(
    message: 'Test message',
    title: 'Test Title',
    code: 400,
    data: [
        "key" => "value"
    ]
);

or like this

easyError('Test message', 'Test Title', 400, [ "key" => "value" ]);

2) Class

Firstly import the class, then create an instance.

use Neography7\EasyResponse\EasyResponse;

$callback = new EasyResponse;

You can add message, title, success, code, data or add custom key with chaining methods.

$callback->title("Title")
            ->message("Message")
            ->success("true")
            ->response();

The response method is going to make it all together into an array then it responds as a json callback. If success is given as true, the response code will be 200. Additionally, you can add data, code, and custom key-value with the chaining methods.

$callback->title("Error Title")
            ->message("Error message.")
            ->success("false")
            ->code(404)
            ->data["key" => value]
            ->addKey("key", value)
            ->response();

If you want to remove the key that you added, you can use this method.

$callback->removeKey("key");

3) Static Class

Firstly import the class that initializes EasyResponse, then call the success or error method that you want to use.

use Neography7\EasyResponse\EA;

$callbackSuccess = EA::success($message, $title = null);
$callbackError = EA::error($message, $title = null, $code = null);

You must to call response method after use.

$callback EA::success("Message", "Title")->response();

Testing

I recommend testbench with "nunomaduro/collision" for testing.

php vendor/bin/testbench package:test

Roadmap

  • The package were created
  • More helpers function will be added
  • Initial callbacks messages and their translations will be added

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email ilkerakyel97@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.