2017 © Pedro Peláez
 

library jsend

A simple PHP implementation of the JSend specification.

image

nannehuiges/jsend

A simple PHP implementation of the JSend specification.

  • Wednesday, July 11, 2018
  • by nanne
  • Repository
  • 2 Watchers
  • 26 Stars
  • 44,307 Installations
  • PHP
  • 3 Dependents
  • 1 Suggesters
  • 4 Forks
  • 0 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

Build Status Build Status Build Status Build Status, (*1)

Code Climate
Issue Count, (*2)

Total Downloads, (*3)

JSend

A simple PHP implementation of the JSend specification., (*4)

Usage

use JSend\JSendResponse;

New response

$success = new JSendResponse('success', $data);
$fail = new JSendResponse('fail', $data);
$error = new JSendResponse('error', $data, 'Not cool.', 9001);
$success = JSendResponse::success($data);
$fail = JSendResponse::fail($data);
$error = JSendResponse::error('Not cool.', 9001, $data);

Note: an InvalidJSendException is thrown if the status is invalid or if you're creating an error without a message., (*5)

Convert JSendResponse to JSON

__toString() is overridden to encode JSON automatically., (*6)

$json = $success->encode();
$json = (string) $success;

As JSendResponse is JsonSerializable, you can use the object directly in json_encode, (*7)

json_encode($success);

Setting flags

You can set flags if needed:, (*8)

$success->setEncodingOptions(\JSON_PRETTY_PRINT | \JSON_BIGINT_AS_STRING);
$json = $success->encode();

Convert JSON to JSendResponse

try {
    $response = JSendResponse::decode($json);
} catch (InvalidJSendException $e) {
    echo "You done gone passed me invalid JSend.";
}

Send JSON as HTTP Response

This sets the Content-Type header to application/json and spits out the JSON., (*9)

$jsend = new JSendResponse('success', $data);
$jsend->respond();

Get info

$isSuccess = $response->isSuccess();
$isError = $response->isError();
$isFail = $response->isFail();
$status = $response->getStatus();
$data = $response->getData();
$array = $response->asArray();

Additionally, you can call the following methods on an error. A BadMethodCallException is thrown if the status is not error, so check first., (*10)

if ($response->isError()) {
    $code = $response->getErrorCode;
    $message = $response->getErrorMessage;
}

Development

For your convenience, there is a dockerfile with the right dependencies (php, composer) available. Please use those to run various things (composer, phpunit, etc). You will need docker installed, but you don't need PHP or composer or any of the other dependencies., (*11)

Setting up and using a local environment

To start using the local environment for testing and debugging all you have to is open a shell in the root folder of where this project is checked out. Then run the following command., (*12)

make build install

This command should be run occasionally to keep the local environment up to date. For instance when the composer dependencies are changed., (*13)

Using the shell

To open a shell in the docker container run the following command., (*14)

make shell

Available commands are in /bin, (*15)

Running the code quality tools locally

We use a variety of tools to keep the code quality of the library high. To run one the tools you only need to run, (*16)

make <tool_name>

Available tools: - phpstan PHPStan is static analyser tool that can detect various code issues. - phpunit PHPUnit is the unit testing framework we use for this library. - codeclimate CodeClimate, (*17)

Notes

  • Note that the composer.lock file is ignored.

Credits

The library was written by Jamie Schembri. It has been transfered to the current account Nanne Huiges in december 2015., (*18)

The Versions

11/07 2018

dev-master

9999999-dev https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

by Nanne Huiges

php json jsend

11/07 2018

v3.0.0

3.0.0.0 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

by Nanne Huiges

php json jsend

03/07 2018

dev-to_7.0

dev-to_7.0 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

by Nanne Huiges

json jsend

03/07 2018

dev-to_7.1

dev-to_7.1 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

by Nanne Huiges

json jsend

24/12 2015

v2.1.0

2.1.0.0 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

by Nanne Huiges

json jsend

15/12 2015

v2.0.0

2.0.0.0 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

by Nanne Huiges

json jsend

14/12 2015

v1.2.1

1.2.1.0 https://github.com/NanneHuiges/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Nanne Huiges

json jsend

05/01 2014

v1.2.0

1.2.0.0 https://github.com/shkm/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

json jsend

17/08 2013

v1.1.0

1.1.0.0 https://github.com/shkm/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

json jsend

11/08 2013

v1.0.1

1.0.1.0 https://github.com/shkm/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

json jsend

11/04 2013

v1.0.0

1.0.0.0 https://github.com/shkm/jsend

A simple PHP implementation of the JSend specification.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

json jsend