Skip to content

Commit

Permalink
Performance improvements (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Mar 1, 2018
1 parent 7fd1382 commit 911633f
Show file tree
Hide file tree
Showing 90 changed files with 1,270 additions and 787 deletions.
14 changes: 6 additions & 8 deletions README.md
Expand Up @@ -35,23 +35,21 @@ use JsonApiPhp\JsonApi\Attribute;
use JsonApiPhp\JsonApi\DataDocument;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\Link\Url;
use JsonApiPhp\JsonApi\Relationship;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceObject;
use JsonApiPhp\JsonApi\SingleLinkage;
use JsonApiPhp\JsonApi\ToOne;

echo json_encode(
new DataDocument(
new ResourceObject(
'articles',
'1',
new Attribute('title', 'Rails is Omakase'),
new Relationship(
new ToOne(
'author',
new SingleLinkage(new ResourceIdentifier('author', '9')),
new SelfLink(new Url('/articles/1/relationships/author')),
new RelatedLink(new Url('/articles/1/author'))
new ResourceIdentifier('author', '9'),
new SelfLink('/articles/1/relationships/author'),
new RelatedLink('/articles/1/author')
)
)
),
Expand All @@ -77,7 +75,7 @@ The library API and use-cases are expressed in comprehensive suite of tests.
- [Compound Documents](./test/CompoundDocumentTest.php)
- [Error Documents](./test/ErrorDocumentTest.php)
- [Meta Documents (containing neither data nor errors)](./test/MetaDocumentTest.php)
- [Pagination links](./test/PaginationLinksTest.php)
- [Pagination](./test/PaginationTest.php)
- [Link Objects](./test/LinkObjectTest.php)
- [JSON API Object](./test/JsonApiTest.php)
- [Meta Objects](./test/MetaTest.php)
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -14,7 +14,7 @@
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpunit/phpunit": "^7.0",
"friendsofphp/php-cs-fixer": "^2.2"
},
"autoload": {
Expand Down
53 changes: 28 additions & 25 deletions examples/compound_doc.php
@@ -1,18 +1,19 @@
<?php declare(strict_types=1);

use JsonApiPhp\JsonApi\Attribute;
use JsonApiPhp\JsonApi\CompoundDocument;
use JsonApiPhp\JsonApi\Included;
use JsonApiPhp\JsonApi\Link\LastLink;
use JsonApiPhp\JsonApi\Link\NextLink;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\Link\Url;
use JsonApiPhp\JsonApi\MultiLinkage;
use JsonApiPhp\JsonApi\Relationship;
use JsonApiPhp\JsonApi\PaginatedResourceCollection;
use JsonApiPhp\JsonApi\Pagination;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceIdentifierCollection;
use JsonApiPhp\JsonApi\ResourceObject;
use JsonApiPhp\JsonApi\ResourceObjectSet;
use JsonApiPhp\JsonApi\SingleLinkage;
use JsonApiPhp\JsonApi\ToMany;
use JsonApiPhp\JsonApi\ToOne;

require_once __DIR__.'/../vendor/autoload.php';

Expand All @@ -22,53 +23,55 @@
new Attribute('first-name', 'Dan'),
new Attribute('last-name', 'Gebhardt'),
new Attribute('twitter', 'dgeb'),
new SelfLink(new Url('http://example.com/people/9'))
new SelfLink('http://example.com/people/9')
);

$comment05 = new ResourceObject(
'comments',
'5',
new Attribute('body', 'First!'),
new SelfLink(new Url('http://example.com/comments/5')),
new Relationship('author', new SingleLinkage(new ResourceIdentifier('people', '2')))
new SelfLink('http://example.com/comments/5'),
new ToOne('author', new ResourceIdentifier('people', '2'))

);
$comment12 = new ResourceObject(
'comments',
'12',
new Attribute('body', 'I like XML better'),
new SelfLink(new Url('http://example.com/comments/12')),
new Relationship('author', new SingleLinkage($dan->identifier()))
new SelfLink('http://example.com/comments/12'),
new ToOne('author', $dan->toIdentifier())
);

$document = new CompoundDocument(
new ResourceObjectSet(
new PaginatedResourceCollection(
new Pagination(
new NextLink('http://example.com/articles?page[offset]=2'),
new LastLink('http://example.com/articles?page[offset]=10')
),
new ResourceObject(
'articles',
'1',
new Attribute('title', 'JSON API paints my bikeshed!'),
new SelfLink(new Url('http://example.com/articles/1')),
new Relationship(
new SelfLink('http://example.com/articles/1'),
new ToOne(
'author',
new SingleLinkage($dan->identifier()),
new SelfLink(new Url('http://example.com/articles/1/relationships/author')),
new RelatedLink(new Url('http://example.com/articles/1/author'))
$dan->toIdentifier(),
new SelfLink('http://example.com/articles/1/relationships/author'),
new RelatedLink('http://example.com/articles/1/author')
),
new Relationship(
new ToMany(
'comments',
new MultiLinkage(
$comment05->identifier(),
$comment12->identifier()
new ResourceIdentifierCollection(
$comment05->toIdentifier(),
$comment12->toIdentifier()
),
new SelfLink(new Url('http://example.com/articles/1/relationships/comments')),
new RelatedLink(new Url('http://example.com/articles/1/comments'))
new SelfLink('http://example.com/articles/1/relationships/comments'),
new RelatedLink('http://example.com/articles/1/comments')
)
)
),
new Included($dan, $comment05, $comment12),
new SelfLink(new Url('http://example.com/articles')),
new NextLink(new Url('http://example.com/articles?page[offset]=2')),
new LastLink(new Url('http://example.com/articles?page[offset]=10'))
new SelfLink('http://example.com/articles')
);

echo json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
12 changes: 5 additions & 7 deletions examples/simple_doc.php
Expand Up @@ -5,23 +5,21 @@
use JsonApiPhp\JsonApi\DataDocument;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\Link\Url;
use JsonApiPhp\JsonApi\Relationship;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceObject;
use JsonApiPhp\JsonApi\SingleLinkage;
use JsonApiPhp\JsonApi\ToOne;

echo json_encode(
new DataDocument(
new ResourceObject(
'articles',
'1',
new Attribute('title', 'Rails is Omakase'),
new Relationship(
new ToOne(
'author',
new SingleLinkage(new ResourceIdentifier('author', '9')),
new SelfLink(new Url('/articles/1/relationships/author')),
new RelatedLink(new Url('/articles/1/author'))
new ResourceIdentifier('author', '9'),
new SelfLink('/articles/1/relationships/author'),
new RelatedLink('/articles/1/author')
)
)
),
Expand Down
22 changes: 0 additions & 22 deletions src/AttachableValue.php

This file was deleted.

17 changes: 14 additions & 3 deletions src/Attribute.php
Expand Up @@ -2,12 +2,23 @@

namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\PrimaryData\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;

final class Attribute extends ResourceField
final class Attribute implements ResourceField
{
use ResourceFieldTrait;
private $val;

public function __construct(string $name, $val)
{
$this->validateFieldName($name);
$this->name = $name;
$this->val = $val;
}

public function attachTo(object $o)
{
parent::attachTo(child($o, 'attributes'));
child($o, 'attributes')->{$this->name} = $this->val;
}
}
26 changes: 12 additions & 14 deletions src/CompoundDocument.php
Expand Up @@ -2,23 +2,21 @@

namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\PrimaryData\PrimaryData;
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
use JsonApiPhp\JsonApi\Internal\PrimaryData;

final class CompoundDocument extends JsonSerializableValue
final class CompoundDocument implements \JsonSerializable
{
private $doc;

public function __construct(PrimaryData $data, Included $included, DataDocumentMember ...$members)
{
foreach ($included as $resource) {
if ($data->identifies($resource)) {
continue;
}
foreach ($included as $anotherResource) {
if ($anotherResource->identifies($resource)) {
continue 2;
}
}
throw new \DomainException('Full linkage required for '.json_encode($resource->identifier()));
}
parent::__construct(combine($data, $included, ...$members));
$included->validateLinkage($data);
$this->doc = combine($data, $included, ...$members);
}

public function jsonSerialize()
{
return $this->doc;
}
}
14 changes: 11 additions & 3 deletions src/DataDocument.php
Expand Up @@ -2,12 +2,20 @@

namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\PrimaryData\PrimaryData;
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
use JsonApiPhp\JsonApi\Internal\PrimaryData;

final class DataDocument extends JsonSerializableValue
final class DataDocument implements \JsonSerializable
{
private $value;

public function __construct(PrimaryData $data, DataDocumentMember ...$members)
{
parent::__construct(combine($data, ...$members));
$this->value = combine($data, ...$members);
}

public function jsonSerialize()
{
return $this->value;
}
}
10 changes: 0 additions & 10 deletions src/DataDocumentMember.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DocumentMember.php

This file was deleted.

14 changes: 10 additions & 4 deletions src/Error.php
Expand Up @@ -2,17 +2,23 @@

namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\Error\ErrorMember;
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

final class Error extends JsonSerializableValue implements ErrorDocumentMember
final class Error implements ErrorDocumentMember
{
private $error;

public function __construct(ErrorMember ...$members)
{
parent::__construct(combine(...$members));
$this->error = (object) [];
foreach ($members as $member) {
$member->attachTo($this->error);
}
}

public function attachTo(object $o)
{
$o->errors[] = $this;
$o->errors[] = $this->error;
}
}
16 changes: 13 additions & 3 deletions src/Error/Code.php
Expand Up @@ -2,15 +2,25 @@

namespace JsonApiPhp\JsonApi\Error;

use JsonApiPhp\JsonApi\AttachableValue;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

final class Code extends AttachableValue implements ErrorMember
final class Code implements ErrorMember
{
/**
* @var string
*/
private $code;

/**
* @param string $code an application-specific error code, expressed as a string value
*/
public function __construct(string $code)
{
parent::__construct('code', $code);
$this->code = $code;
}

public function attachTo(object $o): void
{
$o->code = $this->code;
}
}
16 changes: 13 additions & 3 deletions src/Error/Detail.php
Expand Up @@ -2,15 +2,25 @@

namespace JsonApiPhp\JsonApi\Error;

use JsonApiPhp\JsonApi\AttachableValue;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

final class Detail extends AttachableValue implements ErrorMember
final class Detail implements ErrorMember
{
/**
* @var string
*/
private $detail;

/**
* @param string $detail a human-readable explanation specific to this occurrence of the problem.
*/
public function __construct(string $detail)
{
parent::__construct('detail', $detail);
$this->detail = $detail;
}

public function attachTo(object $o): void
{
$o->detail = $this->detail;
}
}

0 comments on commit 911633f

Please sign in to comment.