2017 © Pedro Peláez
 

library jsend

An Immutable Value Object JSend compliant

image

carpediem/jsend

An Immutable Value Object JSend compliant

  • Tuesday, March 15, 2016
  • by ignace-dev
  • Repository
  • 3 Watchers
  • 4 Stars
  • 2,021 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 7 Versions
  • 17 % Grown

The README.md

JSend

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads, (*1)

JSend is a simple library to ease the use of JSend compliant objects., (*2)

<?php

use Carpediem\JSend\JSend;

$data = [
    'post' => [
        'id' => 1,
        'title' => 'foo',
        'author' => 'bar',
    ],
];
$response = JSend::success($data);
$response->send(['Access-Control-Allow-Origin' => 'example.com']);
die;

Highlights

  • Simple API
  • Immutable Value Object

System Requirements

You need PHP >= 7.0 to use JSend but the latest stable version of PHP/HHVM is recommended., (*3)

Install

Install JSend using Composer., (*4)

$ composer require carpediem/jsend

Documentation

Class summary

final class JSend implements JsonSerializable
{
    const STATUS_SUCCESS = 'success';
    const STATUS_ERROR = 'error';
    const STATUS_FAIL = 'fail';

    public static function fromJSON($json, int $depth = 512, int $options = 0): self;
    public static function fromArray(array $arr): self;
    public static function success($data = null): self;
    public static function fail($data = null): self;
    public static function error($errorMessage, int $errorCode = null, $data = null): self;
    public function getStatus(): string;
    public function getData(): array;
    public function getErrorMessage(): ?string;
    public function getErrorCode(): ?int;
    public function isSuccess(): bool;
    public function isFail(): bool;
    public function isError(): bool;
    public function toArray(): array;
    public function __toString(): string;
    public function jsonSerialize(): array;
    public function send(array $headers = []): int;
    public function withStatus(string $status): self;
    public function withData($data): self;
    public function withError($errorMessage, int $errorCode = null): self;
}

Class Import

<?php

use Carpediem\JSend\JSend;

Create new instances

Use named constructors to instantiate a JSend object creation, (*5)

$success = JSend::success($data);
$fail = JSend::fail($data);
$error = JSend::error('Not Found', 404, $data);
$response = JSend::fromJSON('{"status":"success","data":{"post":{"id":1,"title":"foo","author":"bar"}}}');
$altResponse = JSend::fromArray(['data' => ['post' => 1], 'code' => 404, 'message' => 'Post not Found']);

If the object can not be created a Carpediem\JSend\Exception will be thrown., (*6)

Access properties

$response = JSend::error('Not Found', 404, ['post' => 1234]);
$response->getStatus();       // returns 'success, 'error', 'fail'
$response->getErrorMessage(); // returns 'Not Found'
$response->getErrorCode();    // returns 404
$response->getData();         // returns $data
$response->isSuccess();       // boolean
$response->isFail();          // boolean
$response->isError();         // boolean
  • JSend::getErrorMessage returns null when JSend::getStatus is not equal to error;
  • JSend::getErrorCode is an integer or null;

Manipulations

$response = JSend::success(['post' => 1234]);
(string) $response;  // returns {"status": "success", "data": {"post": 1234}}
echo json_encode($response, JSON_PRETTY_PRINT);
// returns
// {
//    "status": "success",
//    "data": {
//        "post": 1234
//    }
//}
$response->toArray();
// returns
// [
//    'status' => 'success',
//    'data' => [
//        'post' => 1234,
//    ]
// ]

Updates

The JSend object is immutable so any changes to the object will return a new object, (*7)

$response = JSend::success();
$newResponse = $response->withData(['post' => 1234]);
$failResponse = $response->witStatus(JSend::STATUS_FAIL);
$errorResponse = $response->withError('This is an error', 404);

echo $response;      // returns {"status": "success"}
echo $newResponse;   // returns {"status": "success", "data": {"post": 1234}}
echo $failResponse;  // returns {"status": "fail"}
echo $errorResponse; // returns {"status": "error", "message": "This is an error", code: 404}

JSend::withData accepts the null value, an array or a JsonSerializable objects whose jsonSerialize method returns an array, (*8)

JSend::withError $errorCode parameter is optional, (*9)

Creating an HTTP Response

header('HTTP/1.1 404 Not Found'); // don't forget to add the HTTP header
$response = JSend::fail(['post' => 1234]);
$response->send(['Access-Control-Allow-Origin' => '*']);
die;

JSend::send accepts additional headers in form of key/value pairs., (*10)

Testing

the library has a:, (*11)

  • a PHPUnit test suite
  • a coding style compliance test suite using PHP CS Fixer.
  • a code analysis compliance test suite using PHPStan.

To run the tests, run the following command from the project folder., (*12)

bash $ composer test, (*13)

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details., (*14)

Security

If you discover any security related issues, please email dev@carpediem.fr instead of using the issue tracker., (*15)

Credits

License

The MIT License (MIT). Please see LICENSE for more information., (*16)

The Versions

15/03 2016

dev-master

9999999-dev http://carpediem.github.io/JSend

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

15/03 2016

1.2.3

1.2.3.0 http://carpediem.github.io/JSend

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

14/03 2016

1.2.2

1.2.2.0 http://carpediem.github.io/JSend

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

11/03 2016

1.2.1

1.2.1.0 http://carpediem.github.io/JSend

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

09/03 2016

1.2.0

1.2.0.0 https://www.carpediem.fr

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

04/03 2016

1.1.0

1.1.0.0 https://www.carpediem.fr

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend

03/03 2016

1.0.0

1.0.0.0 https://www.carpediem.fr

An Immutable Value Object JSend compliant

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

json rest export import jsend