, (*1)
Json Response Builder Package
, (*2)
Install
``` bash
composer require saad/json-response-builder, (*3)
### Change Log
> `V 1.3.1`
add feature to merge meta when adding data
`addData(str $key, mix $val, bool $merge_meta)`
``` php
$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();
$builder->addData('users', [
['name' => 'Ahmed Saad'],
'meta' => [
'name' => 'iam meta',
]
], true); // Note the third argument
return $builder->getResponse();
// Output
{
"success": true,
"meta" : {
'name' => 'iam meta',
},
"data": [
'users': [
{"name": "Ahmed Saad"}
],
],
"message": "Successfully Retrieved"
}
V 1.3
, (*4)
Add Strict Mode and enabled by default:
in strict mode if data or meta are empty it will set it's value to null
instead of []
, (*5)
to turn of strickt mode, on constructor pass false
to disable strickt mode so that it will return data, and meta as empty array if they are empty, (*6)
you can set mode on instance by the method strictMode(bool)
, (*7)
Usage
-
Basic Example:, (*8)
inside your controller:, (*9)
``` php, (*10)
$builder = new Saad\JsonResponseBuilder\JsonResponseBuilder();
$builder->mergeData([
['name' => 'Ahmed Saad'],
['name' => 'John Doe']
]);
$builder->addMeta([
'pagination' => [
'page' => 1,
'per_page' => 4,
]
]);
return $builder->getResponse();
```, (*11)
-
the above example will output:, (*12)
``` javascript, (*13)
{
"success": true,
"meta" : {
"pagination": {
"page": 1,
"per_page": 4
}
},
"data": [
{"name": "Ahmed Saad"},
{"name": "John Doe"}
],
"message": "Successfully Retrieved"
}
```, (*14)
Available Methods
addData($key, $value)
Appends to data new member with the given key and value, (*15)
``` php
$builder->addData('doctors', ['ahmed', 'mohamed', 'saad']);
$builder->addData('patients', ['patient1', 'patient3', 'patient3']);, (*16)
// Output data will be, (*17)
"data": {
"doctors": ["ahmed", "mohamed", "saad"],
"patients" ["patient1", "patient4", "patient3"]
},, (*18)
```, (*19)
mergeData($array)
merge given array with data with given array keys as keys, this is usefull when we want to send data as json array insteadof json object with key and value, (*20)
this method also if the given array has key called 'meta' it will remove that key and add it to response meta, (*21)
``` php
$builder->mergeData(['ahmed', 'mohamed', 'meta' => ['key' => 'Iam Meta']]);, (*22)
// Output will be, (*23)
{
"success": true,
"meta": {
"key": "Iam Meta"
},
"data": [
"ahmed",
"mohamed"
],
"message": "Successfully Retrieved"
}
```, (*24)
Appends to meta new member with the given key and value, (*25)
merge given array with meta, (*26)
add header to response headers, (*27)
success($response_message = null)
set response success status to true
, and set response message if supplied., (*28)
setMessage($response_message = null)
set response message if supplied., (*29)
error($message = null, $error_code = null)
set response success status to false
and set nessage and error code, (*30)
``` php
$builder->error('Fails!', 2345);, (*31)
// Output will be, (*32)
{
"success": false,
"meta": null,
"data": null,
'error': {
"message": "Fails!",
"code": 2345
}
"message": "Fails!"
}, (*33)
addError($key, mixed $value)
Add key to error array, (*34)
``` php
$builder->error('Fails!', 2345)
->addError('validation', 'validation value');, (*35)
// Output will be, (*36)
{
"success": false,
"meta": null,
"data": null,
'error': {
"message": "Fails!",
"code": 2345,
"validation": "validation value"
}
"message": "Fails!"
}, (*37)
setStatusCode(301)
set response status code., (*38)
strictMode(bool)
default value true
, (*39)
since V1.3
, (*40)
enable or disable strict mode., (*41)
``` php
$builder->getResponse();, (*42)
// Strict Mode Enabled, Output will be, (*43)
{
"success": false,
"meta": null,
"data": null,
"message": ""
}, (*44)
$builder->strictMode(false)->getResponse();, (*45)
// Strict Mode Disabled, Output will be, (*46)
{
"success": false,
"meta":[],
"data":[],
"message": ""
}, (*47)
getResponse($status_code = null)
set response status code if supplied, and return Response Object, (*48)
License
The Laravel framework is open-sourced software licensed under the MIT license.README.md, (*49)