Skip to content

Commit

Permalink
Merge branch '2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lode committed Mar 3, 2019
2 parents e75477b + 399f5da commit c80ec0b
Show file tree
Hide file tree
Showing 118 changed files with 9,729 additions and 368 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# @see http://editorconfig.org

root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
tests/report/
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

install:
- rm composer.lock
- composer install --prefer-dist --no-interaction --no-progress

script:
- php script/test.php
173 changes: 123 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,163 @@
# jsonapi
# jsonapi [![Build Status](https://travis-ci.org/lode/jsonapi.svg?branch=master)](https://travis-ci.org/lode/jsonapi)

Simple and friendly library for api servers (PHP serving out JSON).
A simple and human-friendly library for api servers (php serving json).

It allows you to generate json output according to the [JSON:API](https://jsonapi.org/) standard,
while being easy to understand for people without knowledge of the jsonapi standard.

The JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them.
Read more about it at [jsonapi.org](https://jsonapi.org/).


## Installation

[Use Composer](http://getcomposer.org/) require to get the latest stable version:

```
composer require alsvanzelf/jsonapi
```

#### Upgrading from v1

If you used v1 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) on how to upgrade.

It generates json output according to the [jsonapi.org](http://jsonapi.org/) standard,
but aims to be easy to understand for people without knowledge of the jsonapi standard.


## Getting started

A small example:
#### A small resource example

```php
use alsvanzelf\jsonapi;

$user = new stdClass();
$user->id = 42;
$user->name = 'Zaphod Beeblebrox';
$user->heads = 2;
use alsvanzelf\jsonapi\ResourceDocument;

$jsonapi = new jsonapi\resource($type='user', $user->id);
$jsonapi->fill_data($user);
$jsonapi->send_response();
$document = new ResourceDocument($type='user', $id=42);
$document->add('name', 'Zaphod Beeblebrox');
$document->add('heads', 2);
$document->sendResponse();
```

Which will result in:

```json
{
"links": {
"self": "/examples/resource.php"
},
"data": {
"type": "user",
"id": 42,
"attributes": {
"name": "Zaphod Beeblebrox",
"heads": 2
},
"links": {
"self": "/examples/resource.php"
}
}
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "user",
"id": "42",
"attributes": {
"name": "Zaphod Beeblebrox",
"heads": 2
}
}
}
```

For a collection response, data is an array of resources.
Errors can also be send as response, even automatically by exceptions.
#### A collection of resources

Examples for all kind of responses are in the [/examples](/examples) directory.
```php
use alsvanzelf\jsonapi\CollectionDocument;

$document = new CollectionDocument();
$document->add('user', 42, ['name' => 'Zaphod Beeblebrox']);
$document->add('user', 1, ['name' => 'Ford Prefect']);
$document->add('user', 2, ['name' => 'Arthur Dent']);
$document->sendResponse();
```

## Installation
Which will result in:

```json
{
"jsonapi": {
"version": "1.0"
},
"data": [
{
"type": "user",
"id": "42",
"attributes": {
"name": "Zaphod Beeblebrox"
}
},
{
"type": "user",
"id": "1",
"attributes": {
"name": "Ford Prefect"
}
},
{
"type": "user",
"id": "2",
"attributes": {
"name": "Arthur Dent"
}
}
]
}
```

#### Turning an exception into jsonapi

```php
use alsvanzelf\jsonapi\ErrorsDocument;

[Use Composer](http://getcomposer.org/). And use require to get the latest stable version:
$exception = new Exception('That is not valid', 422);

$document = ErrorsDocument::fromException($exception);
$document->sendResponse();
```
composer require alsvanzelf/jsonapi

Which will result in:

```json
{
"jsonapi": {
"version": "1.0"
},
"errors": [
{
"status": "422",
"code": "Exception",
"meta": {
"class": "Exception",
"message": "That is not valid",
"code": 422,
"file": "README.md",
"line": 107,
"trace": []
}
}
]
}
```

Examples for all kind of responses are in the [/examples](/examples) directory.

## To Do

Right now, this library handles all the basics:
## Features

- generating single resources
- generating resource collections
- adding to-one and to-many relationships
- handling error responses
- sending out the json response with correct http headers
This library supports [v1.0 of the JSON:API specification](https://jsonapi.org/format/1.0/).

Plus some handy tools:
It has support for generating & sending documents with:

- easy turning thrown exceptions into jsonapi responses
- constants for easy setting http status codes
- sending out redirect locations
- single resources
- resource collections
- to-one and to-many relationships
- errors (easily turning exceptions into jsonapi output)

Plans for the [near](https://github.com/lode/jsonapi/labels/current%20focus)
and [later](https://github.com/lode/jsonapi/issues?utf8=%E2%9C%93&q=is%3Aopen+-label%3A%22current+focus%22+) future include:
Plans for the future include:

- import a database array as a collection response ([#2](https://github.com/lode/jsonapi/issues/2))
- handle creating, updating and deleting resources ([#5](https://github.com/lode/jsonapi/issues/5))
- support v1.1 of the specification ([#40](https://github.com/lode/jsonapi/pull/40))
- parse request options: sparse fields, sorting, pagination, filtering ([#44](https://github.com/lode/jsonapi/issues/44))
- parse requests for creating, updating and deleting resources and relationships ([#5](https://github.com/lode/jsonapi/issues/5))


## Contributing

Pull Requests or issues are welcome!
[Pull Requests](https://github.com/lode/jsonapi/pulls) or [issues](https://github.com/lode/jsonapi/issues) are welcome!


## Licence
Expand Down
Loading

0 comments on commit c80ec0b

Please sign in to comment.