Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
engines:
phpmd:
enabled: true
checks:
Controversial/CamelCasePropertyName:
enabled: false
Naming/ShortVariable:
enabled: false
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.phar
/vendor/
composer.lock
/composer.lock
/.php_cs.cache
60 changes: 60 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
$header = <<<EOF
This file is part of JSON:API implementation for PHP.

(c) Alexey Karapetov <karapetov@gmail.com>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
$finder = PhpCsFixer\Finder::create()
->files()
->name('*.php')
->in(__DIR__ . '/src')
->in(__DIR__ . '/test');
return PhpCsFixer\Config::create()
->setUsingCache(true)
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => true,
'cast_spaces' => true,
'concat_space' => true,
'declare_strict_types' => true,
'header_comment' => array('header' => $header),
'include' => true,
'is_null' => true,
'lowercase_cast' => true,
'mb_str_functions' => true,
'method_separation' => true,
'native_function_casing' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'psr0' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'standardize_not_equals' => true,
'strict_comparison' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'whitespace_after_comma_in_array' => true,
])
->setFinder($finder);
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ language: php
php:
- '7.0'
- '7.1'
- nightly

before_script:
- composer install

script:
- vendor/bin/phpcs
- vendor/bin/php-cs-fixer fix -v --dry-run
- phpunit --coverage-clover build/logs/clover.xml
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "2.8.*"
"friendsofphp/php-cs-fixer": "^2.2"
},
"autoload": {
"psr-4": {
"JsonApiPhp\\JsonApi\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JsonApiPhp\\JsonApi\\Test\\": "test/"
}
},
"scripts": {
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always"
}
}
16 changes: 0 additions & 16 deletions phpcs.xml.dist

This file was deleted.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-->

<phpunit
bootstrap="test/bootstrap.php"
bootstrap="vendor/autoload.php"
stopOnFailure="false"
verbose="true"
colors="true"
Expand Down
49 changes: 24 additions & 25 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
<?php
/**
* This file is part of JSON:API implementation for PHP.
declare(strict_types=1);

/*
* This file is part of JSON:API implementation for PHP.
*
* (c) Alexey Karapetov <karapetov@gmail.com>
* (c) Alexey Karapetov <karapetov@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

use JsonApiPhp\JsonApi\Document\Resource\IdentifiableResource;
use JsonApiPhp\JsonApi\HasLinksAndMeta;
use JsonApiPhp\JsonApi\Document\Resource\ResourceInterface;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;

final class Document implements \JsonSerializable
{
const MEDIA_TYPE = 'application/vnd.api+json';
const DEFAULT_API_VERSION = '1.0';

use HasLinksAndMeta;
use LinksTrait;
use MetaTrait;

private $data;
private $errors;
private $meta;
private $json_api;
private $links;
private $api;
private $included;
private $is_sparse = false;

Expand All @@ -48,14 +47,14 @@ public static function fromErrors(Error ...$errors): self
return $doc;
}

public static function fromResource(IdentifiableResource $data): self
public static function fromResource(ResourceInterface $data): self
{
$doc = new self;
$doc->data = $data;
return $doc;
}

public static function fromResources(IdentifiableResource ...$data): self
public static function fromResources(ResourceInterface ...$data): self
{
$doc = new self;
$doc->data = $data;
Expand All @@ -64,15 +63,15 @@ public static function fromResources(IdentifiableResource ...$data): self

public function setApiVersion(string $version = self::DEFAULT_API_VERSION)
{
$this->json_api['version'] = $version;
$this->api['version'] = $version;
}

public function setApiMeta(array $meta)
{
$this->json_api['meta'] = $meta;
$this->api['meta'] = $meta;
}

public function setIncluded(IdentifiableResource ...$included)
public function setIncluded(ResourceObject ...$included)
{
$this->included = $included;
}
Expand All @@ -90,7 +89,7 @@ public function jsonSerialize()
'data' => $this->data,
'errors' => $this->errors,
'meta' => $this->meta,
'jsonapi' => $this->json_api,
'jsonapi' => $this->api,
'links' => $this->links,
'included' => $this->included,
],
Expand All @@ -113,9 +112,9 @@ private function enforceFullLinkage()
}
}

private function anotherIncludedResourceIdentifies(IdentifiableResource $resource): bool
private function anotherIncludedResourceIdentifies(ResourceObject $resource): bool
{
/** @var IdentifiableResource $included_resource */
/** @var ResourceObject $included_resource */
foreach ($this->included as $included_resource) {
if ($included_resource !== $resource && $included_resource->identifies($resource)) {
return true;
Expand All @@ -124,9 +123,9 @@ private function anotherIncludedResourceIdentifies(IdentifiableResource $resourc
return false;
}

private function hasLinkTo(IdentifiableResource $resource): bool
private function hasLinkTo(ResourceObject $resource): bool
{
/** @var IdentifiableResource $my_resource */
/** @var ResourceInterface $my_resource */
foreach ($this->toResources() as $my_resource) {
if ($my_resource->identifies($resource)) {
return true;
Expand All @@ -135,9 +134,9 @@ private function hasLinkTo(IdentifiableResource $resource): bool
return false;
}

private function toResources(): \Generator
private function toResources(): \Iterator
{
if ($this->data instanceof IdentifiableResource) {
if ($this->data instanceof ResourceInterface) {
yield $this->data;
} elseif (is_array($this->data)) {
foreach ($this->data as $datum) {
Expand Down
21 changes: 9 additions & 12 deletions src/Document/Error.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<?php
/**
* This file is part of JSON:API implementation for PHP.
declare(strict_types=1);

/*
* This file is part of JSON:API implementation for PHP.
*
* (c) Alexey Karapetov <karapetov@gmail.com>
* (c) Alexey Karapetov <karapetov@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

use JsonApiPhp\JsonApi\HasMeta;

final class Error implements \JsonSerializable
{
use HasMeta;
use MetaTrait;

private $id;
private $links;
Expand All @@ -25,7 +23,6 @@ final class Error implements \JsonSerializable
private $title;
private $detail;
private $source;
private $meta;

public function setId(string $id)
{
Expand Down Expand Up @@ -83,6 +80,6 @@ public function jsonSerialize()
function ($v) {
return null !== $v;
}
) ?: (object)[];
) ?: (object) [];
}
}
23 changes: 23 additions & 0 deletions src/Document/LinksTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/*
* This file is part of JSON:API implementation for PHP.
*
* (c) Alexey Karapetov <karapetov@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JsonApiPhp\JsonApi\Document;

trait LinksTrait
{
protected $links;

public function setLink(string $name, string $value, array $meta = null)
{
$this->links[$name] = $meta ? ['href' => $value, 'meta' => $meta] : $value;
}
}
20 changes: 11 additions & 9 deletions src/HasMeta.php → src/Document/MetaTrait.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php
/**
* This file is part of JSON:API implementation for PHP.
declare(strict_types=1);

/*
* This file is part of JSON:API implementation for PHP.
*
* (c) Alexey Karapetov <karapetov@gmail.com>
* (c) Alexey Karapetov <karapetov@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);
namespace JsonApiPhp\JsonApi\Document;

namespace JsonApiPhp\JsonApi;

trait HasMeta
trait MetaTrait
{
protected $meta;

public function setMeta(string $key, $val)
{
$this->meta[$key] = $val;
Expand Down
34 changes: 0 additions & 34 deletions src/Document/Resource/IdentifiableResource.php

This file was deleted.

Loading