2017 © Pedro Peláez
 

library json-response-builder

this package semplifies sending API json response by using a standard and unique response structure

image

saad/json-response-builder

this package semplifies sending API json response by using a standard and unique response structure

  • Wednesday, March 28, 2018
  • by ahmad-sa3d
  • Repository
  • 1 Watchers
  • 4 Stars
  • 31 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 11 % Grown

The README.md

, (*1)

Json Response Builder Package

Build Status License , (*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)

addMeta($key, $value)

Appends to meta new member with the given key and value, (*25)

mergeMeta($array)

merge given array with meta, (*26)

addHeader($header, $value)

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)

The Versions

28/03 2018

dev-master

9999999-dev

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

28/03 2018

1.3.1

1.3.1.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

28/03 2018

1.3

1.3.0.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

24/02 2018

1.2.1

1.2.1.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

24/02 2018

1.2.0

1.2.0.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

22/02 2018

1.1.0

1.1.0.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad

16/02 2018

v1.0.0

1.0.0.0

this package semplifies sending API json response by using a standard and unique response structure

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ahmed Saad