Skip to content

Commit e68d2b9

Browse files
committed
Initial commit
0 parents  commit e68d2b9

27 files changed

+1146
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.phar
2+
/vendor/
3+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Alexey
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Implementation of [JSON API](jsonapi.org) in PHP
2+
3+
## WARNING! Work in progress! The internal library API is not stable yet!
4+
This library is an attempt to express business rules of JSON API specification in a set of PHP 7 classes.
5+
6+
A simple example to illustrate the general idea. This (slightly modified) JSON representation from
7+
[the documentation](http://jsonapi.org/format/#document-resource-objects)
8+
9+
```json
10+
{
11+
"data": {
12+
"type": "articles",
13+
"id": "1",
14+
"attributes": {
15+
"title": "Rails is Omakase"
16+
},
17+
"relationships": {
18+
"author": {
19+
"data": {
20+
"type": "people",
21+
"id": "9"
22+
},
23+
"links": {
24+
"self": "\/articles\/1\/relationships\/author",
25+
"related": "\/articles\/1\/author"
26+
}
27+
}
28+
}
29+
}
30+
}
31+
```
32+
can be built with the following php code:
33+
```php
34+
$author = Relationship::fromLinkage(
35+
Linkage::fromSingleResourceId(
36+
new ResourceId('people', '9')
37+
)
38+
);
39+
$author->setLink('self', '/articles/1/relationships/author');
40+
$author->setLink('related', '/articles/1/author');
41+
42+
$articles = new Resource('articles', '1');
43+
$articles->setRelationship('author', $author);
44+
$articles->setAttribute('title', 'Rails is Omakase');
45+
46+
echo json_encode(Document::fromData($articles), JSON_PRETTY_PRINT);
47+
```
48+
49+
Please refer to [the tests](./test) for the full API documentation:
50+
* [Document](./test/Document/DocumentTest.php)
51+
* [Errors](./test/Document/ErrorTest.php)
52+
* [Resources](./test/Document/Resource/ResourceTest.php)
53+
* [Relationships](./test/Document/Resource/Relationship/RelationshipTest.php)
54+
* [Linkage](./test/Document/Resource/Relationship/LinkageTest.php)

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "json-api-php/json-api",
3+
"description": "jsonapi.org",
4+
"type": "library",
5+
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Alexey Karapetov",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^6.0",
18+
"squizlabs/php_codesniffer": "2.8.*"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"JsonApiPhp\\JsonApi\\": "src/"
23+
}
24+
}
25+
}

phpcs.xml.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<file>src</file>
4+
<file>test</file>
5+
<exclude-pattern>vendor/*</exclude-pattern>
6+
<rule ref="PSR2"/>
7+
</ruleset>

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
bootstrap="test/bootstrap.php"
4+
stopOnFailure="false"
5+
verbose="true"
6+
syntaxCheck="true">
7+
<testsuites>
8+
<testsuite name="Main">
9+
<directory>./test</directory>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist>
14+
<directory suffix=".php">./src/</directory>
15+
</whitelist>
16+
</filter>
17+
</phpunit>

src/Document/Document.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JsonApiPhp\JsonApi\Document;
5+
6+
use JsonApiPhp\JsonApi\HasLinksAndMeta;
7+
8+
final class Document implements \JsonSerializable
9+
{
10+
const MEDIA_TYPE = 'application/vnd.api+json';
11+
const DEFAULT_API_VERSION = '1.0';
12+
13+
use HasLinksAndMeta;
14+
15+
protected $data;
16+
protected $errors;
17+
protected $meta;
18+
protected $jsonapi;
19+
protected $links;
20+
21+
/**
22+
* Use named constructors instead
23+
*/
24+
private function __construct()
25+
{
26+
}
27+
28+
public static function fromMeta(array $meta): self
29+
{
30+
$doc = new self;
31+
$doc->replaceMeta($meta);
32+
return $doc;
33+
}
34+
35+
public static function fromErrors(Error ...$errors): self
36+
{
37+
$doc = new self;
38+
$doc->errors = $errors;
39+
return $doc;
40+
}
41+
42+
public static function fromData(PrimaryData $data): self
43+
{
44+
$doc = new self;
45+
$doc->data = $data;
46+
return $doc;
47+
}
48+
49+
public static function fromDataItems(PrimaryDataItem ...$data): self
50+
{
51+
$doc = new self;
52+
$doc->data = $data;
53+
return $doc;
54+
}
55+
56+
public function setApiVersion(string $version = self::DEFAULT_API_VERSION): void
57+
{
58+
$this->jsonapi['version'] = $version;
59+
}
60+
61+
public function setApiMeta(array $meta): void
62+
{
63+
$this->jsonapi['meta'] = $meta;
64+
}
65+
66+
public function jsonSerialize()
67+
{
68+
return array_filter(
69+
[
70+
'data' => $this->data,
71+
'errors' => $this->errors,
72+
'meta' => $this->meta,
73+
'jsonapi' => $this->jsonapi,
74+
'links' => $this->links,
75+
],
76+
function ($v) {
77+
return null !== $v;
78+
}
79+
);
80+
}
81+
}

src/Document/Error.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JsonApiPhp\JsonApi\Document;
5+
6+
use JsonApiPhp\JsonApi\HasMeta;
7+
8+
final class Error implements \JsonSerializable
9+
{
10+
use HasMeta;
11+
12+
private $id;
13+
private $links;
14+
private $status;
15+
private $code;
16+
private $title;
17+
private $detail;
18+
private $source;
19+
private $meta;
20+
21+
public function setId(string $id): void
22+
{
23+
$this->id = $id;
24+
}
25+
26+
public function setAboutLink(string $link): void
27+
{
28+
$this->links['about'] = $link;
29+
}
30+
31+
public function setStatus(string $status): void
32+
{
33+
$this->status = $status;
34+
}
35+
36+
public function setCode(string $code): void
37+
{
38+
$this->code = $code;
39+
}
40+
41+
42+
public function setTitle(string $title): void
43+
{
44+
$this->title = $title;
45+
}
46+
47+
public function setDetail(string $detail): void
48+
{
49+
$this->detail = $detail;
50+
}
51+
52+
public function setSourcePointer(string $pointer): void
53+
{
54+
$this->source['pointer'] = $pointer;
55+
}
56+
57+
public function setSourceParameter(string $parameter): void
58+
{
59+
$this->source['parameter'] = $parameter;
60+
}
61+
62+
public function jsonSerialize()
63+
{
64+
return array_filter(
65+
[
66+
'id' => $this->id,
67+
'links' => $this->links,
68+
'status' => $this->status,
69+
'code' => $this->code,
70+
'title' => $this->title,
71+
'detail' => $this->detail,
72+
'source' => $this->source,
73+
'meta' => $this->meta,
74+
],
75+
function ($v) {
76+
return null !== $v;
77+
}
78+
) ?: (object)[];
79+
}
80+
}

src/Document/PrimaryData.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JsonApiPhp\JsonApi\Document;
5+
6+
/**
7+
* Primary data of a Single Resource Document
8+
* @package F3\JsonApi\Resource
9+
*/
10+
interface PrimaryData extends \JsonSerializable
11+
{
12+
}

src/Document/PrimaryDataItem.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JsonApiPhp\JsonApi\Document;
5+
6+
/**
7+
* Primary Data Element of a Multi Resource Document
8+
* @package F3\JsonApi\Resource
9+
*/
10+
interface PrimaryDataItem extends \JsonSerializable
11+
{
12+
}

0 commit comments

Comments
 (0)