json-api-client

:construction_worker_woman: A PHP Library to handle the request or response body from a JSON API server.

Github星跟蹤圖

JsonApiClient

Latest Version
Software License
Build Status
Coverage Status
Total Downloads

JsonApiClient :construction_worker_woman: is a PHP Library to validate and handle the response body from a JSON API Server.

Format: JSON API 1.0

:checkered_flag: Goals

  • :heavy_check_mark: Be 100% JSON API spec conform
  • :heavy_check_mark: Be open for new spec versions
  • :heavy_check_mark: Handle/validate a server response body
  • :heavy_check_mark: Handle/validate a client request body
  • :heavy_check_mark: Offer an easy way to retrieve the data
  • :heavy_check_mark: Allow extendability and injection of classes/models

:package: Install

Via Composer

$ composer require art4/json-api-client

:building_construction: Development notice

JsonApiClient can be considered as stable and version 0.10 will be the last development version.

After version 0.8 there where no breaking changes. Every change was backward compatible and every functionality that will be removed in future now only triggers a deprecated warning.

After version 0.10 is released all deprecated code will be removed and version 1.0 will be released.

This simply means for you:

  1. Update to version 0.10
  2. Make your code deprecation free
  3. You are now ready for upgrading to version 1.0 without breaking your code

(Compare the Symfony upgrade documentation)

:warning: Attention for older versions

Version 0.6.1 and below interprets the pagination links wrong. Make sure you are using the latest version of JsonApiClient. See #19, #23 and #26 for more information.

:rocket: Usage

See the quickstart guide or the documentation.

Using as parser

use Art4\JsonApiClient\Exception\InputException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\Helper\Parser;

// The Response body from a JSON API server
$jsonapiString = '{"meta":{"info":"Testing the JsonApiClient library."}}';

try {
    // Use this if you have a response after calling a JSON API server
    $document = Parser::parseResponseString($jsonapiString);

    // Or use this if you have a request to your JSON API server
    $document = Parser::parseRequestString($jsonapiString);
} catch (InputException $e) {
    // $jsonapiString is not valid JSON
} catch (ValidationException $e) {
    // $jsonapiString is not valid JSON API
}

Note: Using Art4\JsonApiClient\Helper\Parser is just a shortcut for directly using the Manager.

$document implements the Art4\JsonApiClient\Accessable interface to access the parsed data. It has the methods has($key), get($key) and getKeys().

// Note that has() and get() have support for dot-notated keys
if ($document->has('meta.info'))
{
    echo $document->get('meta.info'); // "Testing the JsonApiClient library."
}

// you can get all keys as an array
var_dump($document->getKeys());

// array(
//   0 => "meta"
// )

Using as validator

JsonApiClient can be used as a validator for JSON API contents:

use Art4\JsonApiClient\Helper\Parser;

$wrong_jsonapi = '{"data":{},"meta":{"info":"This is wrong JSON API. `data` has to be `null` or containing at least `type` and `id`."}}';

if ( Parser::isValidResponseString($wrong_jsonapi) ) {
// or Parser::isValidRequestString($wrong_jsonapi)
	echo 'string is valid.';
} else {
	echo 'string is invalid json api!';
}

// echoes 'string is invalid json api!'

Extend the client

Need more functionality? Want to directly inject your model? Easily extend JsonApiClient with the Factory.

:loud_sound: Changelog

Please see CHANGELOG for more information what has changed recently.

:white_check_mark: Testing

$ phpunit

:wrench: Contributing

Please feel free to fork and sending Pull Requests. This project follows Semantic Versioning 2 and PSR-2.

:heart: Credits

:page_facing_up: License

GPL3. Please see License File for more information.

主要指標

概覽
名稱與所有者Art4/json-api-client
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證GNU General Public License v3.0
所有者活动
創建於2015-07-28 17:06:20
推送於2024-10-11 12:07:51
最后一次提交
發布數21
最新版本名稱1.3.0 (發布於 )
第一版名稱0.1 (發布於 2015-08-11 09:35:51)
用户参与
星數138
關注者數12
派生數20
提交數503
已啟用問題?
問題數42
打開的問題數5
拉請求數51
打開的拉請求數1
關閉的拉請求數5
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?