mediadevs / response-handler
A simple response handler for my projects, can be easily modified.
1.0.0
2019-11-04 10:56 UTC
Requires
- php: ^7.1
- ext-json: *
Requires (Dev)
This package is auto-updated.
Last update: 2025-04-13 04:53:29 UTC
README
Be aware!
Not everything has been tested yet, use on own risk.
Install
Via Composer
$ composer require mediadevs/response-handler
Via GIT
HTTPS: git clone https://github.com/mediadevs/response-handler.git SSH: git clone git@github.com:mediadevs/response-handler.git
Usage
Basic message
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages and returning a json response $response->add('Action executed successfully')->toJSON(); ?>
Response
[ {"level":"primary","message":"Action executed successfully"} ]
Single message
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Action executed successfully', [ /** There must be an array, but can be left empty*/ ], Response::SUCCESS); // Returning the message in json response $response->toJSON(); ?>
Response
[ {"level":"success","message":"Action executed successfully"} ]
Multiple parameters
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('{%action%} executed {%status%}', ['action' => 'My Custom Action', 'status' => 'successfully'], Response::SUCCESS); // Returning the message in json response $response->toJSON(); ?>
Response
[ {"level":"success","message":"My Custom Action executed successfully"} ]
Multiple messages with parameters
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('{%action%} executed successfully', ['action' => 'your_action'], Response::SUCCESS); $response->add('DUPLICATE {%action%}!', ['action' => 'your_action_2'], Response::WARNING); $response->add('DUPLICATE {%action%}!', ['action' => 'your_action_3'], Response::WARNING); $response->add('{%action%} does not exist', ['action' => 'your_action_4'], Response::ERROR); // Returning the messages in json response $response->toJSON(); ?>
Response
[ {"level":"success","message":"your_action executed successfully"}, {"level":"warning","message":"DUPLICATE your_action_2!"}, {"level":"warning","message":"DUPLICATE your_action_3!"}, {"level":"danger","message":"your_action_4 does not exist"} ]
Highlight parameters
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('"'); // Returning the message in json response $response->toJSON(); ?>
Response
[ {"level":"success","message":"Look how cool this is! \"Highlight me!\""} ]
In this case we used only one encapsulation string which were applied to both sides. If in your case you want different characters on either side you can add a second item to the array.
For example:
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('<strong>', '</strong>'); // Returning the message in json response $response->toJSON(); ?>
Response
[ {"level":"success","message":"Look how cool this is! <strong>Highlight me!</strong>"} ]
Return responses as an array
<?php use mikevandiepen\utility\Response; // Instantiating the Response class $response = new Response(); // Adding messages $response->add('Look how cool this is! {%parameter%}', ['parameter' => 'Highlight me!'], Response::SUCCESS)->delimiters('"'); // Returning the message in json response $response->toArray(); ?>
Response
array( "level" => "success", "message" => "Look how cool this is! \"Highlight me!\"" );
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Testing
``` bash
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email contact@mediadevs.nl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.