2017 © Pedro Peláez
 

library json-response

Structures JSON responses for the JSend format

image

gridonic/json-response

Structures JSON responses for the JSend format

  • Wednesday, October 26, 2016
  • by btemperli
  • Repository
  • 8 Watchers
  • 0 Stars
  • 1,515 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

JsonResponse

Provides simple classes for JSON responses that adhere to a standardized structure. Since JSON responses may have very different formats, this package supports the specific JSend format defined at http://labs.omniti.com/labs/jsend., (*1)

This package is an extension of the HttpFoundation\JsonResponse class from the Symfony package., (*2)

Build Status, (*3)

Install

The recommended way to install JsonResponse is through composer., (*4)

You can either add it as a depedency to your project using the command line:, (*5)

$ composer require gridonic/json-response

or by adding it directly to your composer.json file:, (*6)

{
    "require": {
        "gridonic/json-response": "1.*"
    }
}

Run these two commands to install it:, (*7)

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Now you can add the autoloader, and you will have access to the library:, (*8)

<?php
require 'vendor/autoload.php';

JsonResponse

We differentiate between two different types of Responses: * SuccessJsonResponse * ErrorJsonResponse, (*9)

SuccessJsonResponse

Parameter Type Needed? Default value Description
$data mixed optional null The response data
$message string optional null A success message
$title string optional null A success title
$status integer optional 200 The response status code
$headers array optional array() An array of response headers
/**
 * @throws \InvalidArgumentException
 */
 new SuccessJsonResponse($data, 'Success message', 'Success title', 200);
{
    "status" : "success",
    "data" : { ... },
    "message" : "Sucess message",
    "title" : "Success title"
}

ErrorJsonResponse

Parameter Type Needed? Default value Description
$data mixed optional null The response data
$message string required The error message
$title string optional null An error title
$status integer optional 400 The response status code
$errorCode string optional null An individual error code
$errors array optional array() An array of errors
$headers array optional array() An array of response headers
/**
 * @throws \InvalidArgumentException
 */
 new ErrorJsonResponse($data, 'Error message', 'Error title', 400, 'e311', $errors);
{
    "status" : "error",
    "data" : { ... },
    "message" : "Error message",
    "title" : "Error title",
    "error_code" : "e311",
    "errors" : { ... }
}

JSend

Our Responses are based on the Model of JSend. You can find the documentation of JSend on the JSend website, (*10)

Usage

SuccessJsonResponse

Use a Gridonic\JsonResponse\SuccessJsonResponse to build a structured JSON Response., (*11)

Empty SuccessJsonResponse

new SuccessJsonResponse();
{
    "status" : "success",
    "data" : null
}

SuccessJsonResponse with Content

$data = array(
    'post' => array(
        'id' => 1,
        'title' => 'A blog post',
    )
);
$message = 'The Blog post was successfully created.';
$title = 'Successfully created!';
$statusCode = 205;

new SuccessJsonResponse($data, $message, $title, $statusCode);
{
    "status" : "success",
    "data" : {
        "post": {
            "id" : 1,
            "title" : "A blog post"
        }
    },
    "message" : "The Blog post was successfully created.",
    "title" : "Successfully created!"
}

SuccessJsonResponse

Use a Gridonic\JsonResponse\ErrorJsonResponse to build a structured JSON Response., (*12)

ErrorJsonResponse with Message

$message = 'Oups, data is missing.';

new ErrorJsonResponse(null, $message); // you have to send a message!
{
    "status" : "error",
    "data" : null,
    "message" : "Oups, data is missing"
}

ErrorJsonResponse with Content

$data = array(
    'post' => array(
        'title' => 'A blog post',
    )
);
$message = 'Oups, data is not correct.';
$title = 'An error occured!';
$statusCode = 400;
$errorCode = e311;
$errors = array(
    'body' => 'The parameter is missing.',
    'title' => 'This parameter is too long.'
);

new ErrorJsonResponse($data, $message, $title, $statusCode, $errorCode, $errors);
{
    "status" : "error",
    "data" : {
        "post": {
            "title" : "A blog post"
        }
    },
    "message" : "Oups, data is missing",
    "title" : "An error occured",
    "error_code" : "e311",
    "errors" : {
        "body" : "The parameter is missing.",
        "title" : "This parameter is too long."
    }
}

Major & Minor Releases

1.1.0

New structure of the responses, (*13)

1.0.0

Initial Release, (*14)

Licence

The JsonResponse is licensed under the MIT license., (*15)

The Versions

26/10 2016

dev-develop

dev-develop http://gridonic.ch

Structures JSON responses for the JSend format

  Sources   Download

MIT

The Requires

 

The Development Requires

json http symfony

20/02 2015

dev-master

9999999-dev http://gridonic.ch

Structures JSON responses for the JSend format

  Sources   Download

MIT

The Requires

 

The Development Requires

json http symfony

20/02 2015

1.1.1

1.1.1.0 http://gridonic.ch

Structures JSON responses for the JSend format

  Sources   Download

MIT

The Requires

 

The Development Requires

json http symfony

06/02 2015

1.1.0

1.1.0.0 http://gridonic.ch

Structures JSON responses for the JSend format

  Sources   Download

MIT

The Requires

 

The Development Requires

json http symfony

21/12 2014

dev-strict-json-response-structure

dev-strict-json-response-structure http://gridonic.ch

Structures JSON responses for the JSend format

  Sources   Download

MIT

The Requires

 

The Development Requires

json http symfony

21/10 2014

1.0.5

1.0.5.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation

21/10 2014

1.0.4

1.0.4.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation

21/10 2014

1.0.3

1.0.3.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation

17/10 2014

v1.0.2

1.0.2.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation

17/10 2014

v1.0.0

1.0.0.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation

17/10 2014

v1.0.1

1.0.1.0 http://gridonic.ch

Nice structured JsonResponse for the HttpFoundation

  Sources   Download

MIT

The Requires

 

symfony httpfoundation