Skip to content

Commit

Permalink
Simplification, Formatting, Performance (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Mar 3, 2018
1 parent 0ffb320 commit 42d32f3
Show file tree
Hide file tree
Showing 40 changed files with 277 additions and 276 deletions.
45 changes: 24 additions & 21 deletions examples/compound_doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use JsonApiPhp\JsonApi\Link\NextLink;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\PaginatedResourceCollection;
use JsonApiPhp\JsonApi\PaginatedCollection;
use JsonApiPhp\JsonApi\Pagination;
use JsonApiPhp\JsonApi\ResourceCollection;
use JsonApiPhp\JsonApi\ResourceIdentifier;
use JsonApiPhp\JsonApi\ResourceIdentifierCollection;
use JsonApiPhp\JsonApi\ResourceObject;
Expand Down Expand Up @@ -39,34 +40,36 @@
'12',
new Attribute('body', 'I like XML better'),
new SelfLink('http://example.com/comments/12'),
new ToOne('author', $dan->toIdentifier())
new ToOne('author', $dan->identifier())
);

$document = new CompoundDocument(
new PaginatedResourceCollection(
new PaginatedCollection(
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('http://example.com/articles/1'),
new ToOne(
'author',
$dan->toIdentifier(),
new SelfLink('http://example.com/articles/1/relationships/author'),
new RelatedLink('http://example.com/articles/1/author')
),
new ToMany(
'comments',
new ResourceIdentifierCollection(
$comment05->toIdentifier(),
$comment12->toIdentifier()
new ResourceCollection(
new ResourceObject(
'articles',
'1',
new Attribute('title', 'JSON API paints my bikeshed!'),
new SelfLink('http://example.com/articles/1'),
new ToOne(
'author',
$dan->identifier(),
new SelfLink('http://example.com/articles/1/relationships/author'),
new RelatedLink('http://example.com/articles/1/author')
),
new SelfLink('http://example.com/articles/1/relationships/comments'),
new RelatedLink('http://example.com/articles/1/comments')
new ToMany(
'comments',
new ResourceIdentifierCollection(
$comment05->identifier(),
$comment12->identifier()
),
new SelfLink('http://example.com/articles/1/relationships/comments'),
new RelatedLink('http://example.com/articles/1/comments')
)
)
)
),
Expand Down
5 changes: 4 additions & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use JsonApiPhp\JsonApi\Internal\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;

/**
* @see http://jsonapi.org/format/#document-resource-object-attributes
*/
final class Attribute implements ResourceField
{
use ResourceFieldTrait;
Expand All @@ -17,7 +20,7 @@ public function __construct(string $name, $val)
$this->val = $val;
}

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'attributes')->{$this->name} = $this->val;
}
Expand Down
4 changes: 4 additions & 0 deletions src/CompoundDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
use JsonApiPhp\JsonApi\Internal\PrimaryData;

/**
* A Document with the "included" member
* @see http://jsonapi.org/format/#document-compound-documents
*/
final class CompoundDocument implements \JsonSerializable
{
private $doc;
Expand Down
4 changes: 4 additions & 0 deletions src/DataDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
use JsonApiPhp\JsonApi\Internal\PrimaryData;

/**
* A Document containing the "data" member
* @see http://jsonapi.org/format/#document-top-level
*/
final class DataDocument implements \JsonSerializable
{
private $value;
Expand Down
6 changes: 5 additions & 1 deletion src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

/**
* An Error Object
* @see
*/
final class Error implements ErrorDocumentMember
{
private $error;
Expand All @@ -17,7 +21,7 @@ public function __construct(ErrorMember ...$members)
}
}

public function attachTo(object $o)
public function attachTo(object $o): void
{
$o->errors[] = $this->error;
}
Expand Down
14 changes: 9 additions & 5 deletions src/ErrorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@

use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;

/**
* A Document containing an array of Error objects
* @see http://jsonapi.org/format/#document-top-level
*/
final class ErrorDocument implements \JsonSerializable
{
private $doc;
private $obj;

public function __construct(Error $error, ErrorDocumentMember ...$members)
{
$this->doc = (object) [];
$error->attachTo($this->doc);
$this->obj = (object) [];
$error->attachTo($this->obj);
foreach ($members as $member) {
$member->attachTo($this->doc);
$member->attachTo($this->obj);
}
}

public function jsonSerialize()
{
return $this->doc;
return $this->obj;
}
}
24 changes: 11 additions & 13 deletions src/Included.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\Internal\Attachable;
use JsonApiPhp\JsonApi\Internal\IdentifierRegistry;
use JsonApiPhp\JsonApi\Internal\PrimaryData;

final class Included implements Attachable
Expand All @@ -13,34 +12,33 @@ final class Included implements Attachable
*/
private $resources = [];

private $ids;
private $identifiers = [];

public function __construct(ResourceObject ...$resources)
{
$this->ids = new IdentifierRegistry();
foreach ($resources as $resource) {
$string_id = $resource->key();
if (isset($this->resources[$string_id])) {
throw new \LogicException("Resource $string_id is already included");
$key = $resource->key();
if (isset($this->resources[$key])) {
throw new \LogicException("Resource $resource is already included");
}
$this->resources[$string_id] = $resource;
$resource->registerIn($this->ids);
$this->resources[$key] = $resource;
$resource->registerIn($this->identifiers);
}
}

public function validateLinkage(PrimaryData $data): void
{
$dataRegistry = new IdentifierRegistry();
$data->registerIn($dataRegistry);
$registry = [];
$data->registerIn($registry);
foreach ($this->resources as $resource) {
if ($dataRegistry->has($resource->key()) || $this->ids->has($resource->key())) {
if (isset($registry[$resource->key()]) || isset($this->identifiers[$resource->key()])) {
continue;
}
throw new \LogicException('Full linkage required for '.$resource->key());
throw new \LogicException('Full linkage required for '.$resource);
}
}

public function attachTo(object $o)
public function attachTo(object $o): void
{
foreach ($this->resources as $resource) {
$resource->attachAsIncludedTo($o);
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Attachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
interface Attachable
{
public function attachTo(object $o);
public function attachTo(object $o): void;
}
7 changes: 7 additions & 0 deletions src/Internal/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Internal;

interface Collection extends PrimaryData
{
}
6 changes: 5 additions & 1 deletion src/Internal/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
*/
interface Identifier
{
public function registerIn(IdentifierRegistry $registry);
/**
* @internal
* @param array $registry
*/
public function registerIn(array &$registry): void;
}
27 changes: 0 additions & 27 deletions src/Internal/IdentifierRegistry.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Internal/IdentityTrait.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Internal/LinkTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Internal;

Expand Down
7 changes: 7 additions & 0 deletions src/Internal/ToManyMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Internal;

interface ToManyMember extends ToOneMember
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/**
* @internal
*/
interface RelationshipMember extends Attachable
interface ToOneMember extends Attachable
{
}
10 changes: 5 additions & 5 deletions src/JsonApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

final class JsonApi implements MetaDocumentMember, DataDocumentMember, ErrorDocumentMember
{
private $jsonapi;
private $obj;

public function __construct(string $version = '1.0', Meta $meta = null)
{
$this->jsonapi = (object) [
$this->obj = (object) [
'version' => $version,
];
if ($meta) {
$meta->attachTo($this->jsonapi);
$meta->attachTo($this->obj);
}
}

public function attachTo(object $o)
public function attachTo(object $o): void
{
$o->jsonapi = $this->jsonapi;
$o->jsonapi = $this->obj;
}
}
2 changes: 1 addition & 1 deletion src/Link/AboutLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class AboutLink implements ErrorMember
{
use LinkTrait;

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'links')->about = $this->link;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/FirstLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class FirstLink implements PaginationLink
{
use LinkTrait;

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'links')->first = $this->link;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/LastLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class LastLink implements PaginationLink
{
use LinkTrait;

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'links')->last = $this->link;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/NextLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class NextLink implements PaginationLink
{
use LinkTrait;

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'links')->next = $this->link;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Link/PrevLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class PrevLink implements PaginationLink
{
use LinkTrait;

public function attachTo(object $o)
public function attachTo(object $o): void
{
child($o, 'links')->prev = $this->link;
}
Expand Down
Loading

0 comments on commit 42d32f3

Please sign in to comment.