Skip to content

Commit 9aac8e5

Browse files
authored
Cleaned up the object model. Added some extra checks. (#35) fixes: #30 #13
1 parent 2144daf commit 9aac8e5

31 files changed

+421
-350
lines changed

.codeclimate.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
engines:
2+
phpmd:
3+
enabled: true
4+
checks:
5+
Controversial/CamelCasePropertyName:
6+
enabled: false
7+
Naming/ShortVariable:
8+
enabled: false

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.phar
22
/vendor/
3-
composer.lock
3+
/composer.lock
4+
/.php_cs.cache

.php_cs.dist

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of JSON:API implementation for PHP.
4+
5+
(c) Alexey Karapetov <[email protected]>
6+
7+
For the full copyright and license information, please view the LICENSE
8+
file that was distributed with this source code.
9+
EOF;
10+
$finder = PhpCsFixer\Finder::create()
11+
->files()
12+
->name('*.php')
13+
->in(__DIR__ . '/src')
14+
->in(__DIR__ . '/test');
15+
return PhpCsFixer\Config::create()
16+
->setUsingCache(true)
17+
->setRiskyAllowed(true)
18+
->setRules([
19+
'@PSR2' => true,
20+
'array_syntax' => ['syntax' => 'short'],
21+
'binary_operator_spaces' => true,
22+
'cast_spaces' => true,
23+
'concat_space' => true,
24+
'declare_strict_types' => true,
25+
'header_comment' => array('header' => $header),
26+
'include' => true,
27+
'is_null' => true,
28+
'lowercase_cast' => true,
29+
'mb_str_functions' => true,
30+
'method_separation' => true,
31+
'native_function_casing' => true,
32+
'no_blank_lines_after_class_opening' => true,
33+
'no_blank_lines_after_phpdoc' => true,
34+
'no_empty_statement' => true,
35+
'no_extra_consecutive_blank_lines' => true,
36+
'no_leading_import_slash' => true,
37+
'no_leading_namespace_whitespace' => true,
38+
'no_trailing_comma_in_singleline_array' => true,
39+
'no_unused_imports' => true,
40+
'no_whitespace_in_blank_line' => true,
41+
'object_operator_without_whitespace' => true,
42+
'ordered_imports' => true,
43+
'phpdoc_align' => true,
44+
'phpdoc_indent' => true,
45+
'phpdoc_no_access' => true,
46+
'phpdoc_no_package' => true,
47+
'phpdoc_order' => true,
48+
'phpdoc_scalar' => true,
49+
'phpdoc_trim' => true,
50+
'phpdoc_types' => true,
51+
'psr0' => true,
52+
'short_scalar_cast' => true,
53+
'single_blank_line_before_namespace' => true,
54+
'standardize_not_equals' => true,
55+
'strict_comparison' => true,
56+
'ternary_operator_spaces' => true,
57+
'trailing_comma_in_multiline_array' => true,
58+
'whitespace_after_comma_in_array' => true,
59+
])
60+
->setFinder($finder);

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ language: php
22
php:
33
- '7.0'
44
- '7.1'
5-
- nightly
65

76
before_script:
87
- composer install
98

109
script:
11-
- vendor/bin/phpcs
10+
- vendor/bin/php-cs-fixer fix -v --dry-run
1211
- phpunit --coverage-clover build/logs/clover.xml

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
1515
},
1616
"require-dev": {
1717
"phpunit/phpunit": "^6.0",
18-
"squizlabs/php_codesniffer": "2.8.*"
18+
"friendsofphp/php-cs-fixer": "^2.2"
1919
},
2020
"autoload": {
2121
"psr-4": {
2222
"JsonApiPhp\\JsonApi\\": "src/"
2323
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"JsonApiPhp\\JsonApi\\Test\\": "test/"
28+
}
29+
},
30+
"scripts": {
31+
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always"
2432
}
2533
}

phpcs.xml.dist

Lines changed: 0 additions & 16 deletions
This file was deleted.

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
-->
1010

1111
<phpunit
12-
bootstrap="test/bootstrap.php"
12+
bootstrap="vendor/autoload.php"
1313
stopOnFailure="false"
1414
verbose="true"
1515
colors="true"

src/Document/Document.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
<?php
2-
/**
3-
* This file is part of JSON:API implementation for PHP.
2+
declare(strict_types=1);
3+
4+
/*
5+
* This file is part of JSON:API implementation for PHP.
46
*
5-
* (c) Alexey Karapetov <[email protected]>
7+
* (c) Alexey Karapetov <[email protected]>
68
*
7-
* For the full copyright and license information, please view the LICENSE
8-
* file that was distributed with this source code.
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
911
*/
1012

11-
declare(strict_types=1);
12-
1313
namespace JsonApiPhp\JsonApi\Document;
1414

15-
use JsonApiPhp\JsonApi\Document\Resource\IdentifiableResource;
16-
use JsonApiPhp\JsonApi\HasLinksAndMeta;
15+
use JsonApiPhp\JsonApi\Document\Resource\ResourceInterface;
16+
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
1717

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

23-
use HasLinksAndMeta;
23+
use LinksTrait;
24+
use MetaTrait;
2425

2526
private $data;
2627
private $errors;
27-
private $meta;
28-
private $json_api;
29-
private $links;
28+
private $api;
3029
private $included;
3130
private $is_sparse = false;
3231

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

51-
public static function fromResource(IdentifiableResource $data): self
50+
public static function fromResource(ResourceInterface $data): self
5251
{
5352
$doc = new self;
5453
$doc->data = $data;
5554
return $doc;
5655
}
5756

58-
public static function fromResources(IdentifiableResource ...$data): self
57+
public static function fromResources(ResourceInterface ...$data): self
5958
{
6059
$doc = new self;
6160
$doc->data = $data;
@@ -64,15 +63,15 @@ public static function fromResources(IdentifiableResource ...$data): self
6463

6564
public function setApiVersion(string $version = self::DEFAULT_API_VERSION)
6665
{
67-
$this->json_api['version'] = $version;
66+
$this->api['version'] = $version;
6867
}
6968

7069
public function setApiMeta(array $meta)
7170
{
72-
$this->json_api['meta'] = $meta;
71+
$this->api['meta'] = $meta;
7372
}
7473

75-
public function setIncluded(IdentifiableResource ...$included)
74+
public function setIncluded(ResourceObject ...$included)
7675
{
7776
$this->included = $included;
7877
}
@@ -90,7 +89,7 @@ public function jsonSerialize()
9089
'data' => $this->data,
9190
'errors' => $this->errors,
9291
'meta' => $this->meta,
93-
'jsonapi' => $this->json_api,
92+
'jsonapi' => $this->api,
9493
'links' => $this->links,
9594
'included' => $this->included,
9695
],
@@ -113,9 +112,9 @@ private function enforceFullLinkage()
113112
}
114113
}
115114

116-
private function anotherIncludedResourceIdentifies(IdentifiableResource $resource): bool
115+
private function anotherIncludedResourceIdentifies(ResourceObject $resource): bool
117116
{
118-
/** @var IdentifiableResource $included_resource */
117+
/** @var ResourceObject $included_resource */
119118
foreach ($this->included as $included_resource) {
120119
if ($included_resource !== $resource && $included_resource->identifies($resource)) {
121120
return true;
@@ -124,9 +123,9 @@ private function anotherIncludedResourceIdentifies(IdentifiableResource $resourc
124123
return false;
125124
}
126125

127-
private function hasLinkTo(IdentifiableResource $resource): bool
126+
private function hasLinkTo(ResourceObject $resource): bool
128127
{
129-
/** @var IdentifiableResource $my_resource */
128+
/** @var ResourceInterface $my_resource */
130129
foreach ($this->toResources() as $my_resource) {
131130
if ($my_resource->identifies($resource)) {
132131
return true;
@@ -135,9 +134,9 @@ private function hasLinkTo(IdentifiableResource $resource): bool
135134
return false;
136135
}
137136

138-
private function toResources(): \Generator
137+
private function toResources(): \Iterator
139138
{
140-
if ($this->data instanceof IdentifiableResource) {
139+
if ($this->data instanceof ResourceInterface) {
141140
yield $this->data;
142141
} elseif (is_array($this->data)) {
143142
foreach ($this->data as $datum) {

src/Document/Error.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
2-
/**
3-
* This file is part of JSON:API implementation for PHP.
2+
declare(strict_types=1);
3+
4+
/*
5+
* This file is part of JSON:API implementation for PHP.
46
*
5-
* (c) Alexey Karapetov <[email protected]>
7+
* (c) Alexey Karapetov <[email protected]>
68
*
7-
* For the full copyright and license information, please view the LICENSE
8-
* file that was distributed with this source code.
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
911
*/
1012

11-
declare(strict_types=1);
12-
1313
namespace JsonApiPhp\JsonApi\Document;
1414

15-
use JsonApiPhp\JsonApi\HasMeta;
16-
1715
final class Error implements \JsonSerializable
1816
{
19-
use HasMeta;
17+
use MetaTrait;
2018

2119
private $id;
2220
private $links;
@@ -25,7 +23,6 @@ final class Error implements \JsonSerializable
2523
private $title;
2624
private $detail;
2725
private $source;
28-
private $meta;
2926

3027
public function setId(string $id)
3128
{
@@ -83,6 +80,6 @@ public function jsonSerialize()
8380
function ($v) {
8481
return null !== $v;
8582
}
86-
) ?: (object)[];
83+
) ?: (object) [];
8784
}
8885
}

src/Document/LinksTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/*
5+
* This file is part of JSON:API implementation for PHP.
6+
*
7+
* (c) Alexey Karapetov <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace JsonApiPhp\JsonApi\Document;
14+
15+
trait LinksTrait
16+
{
17+
protected $links;
18+
19+
public function setLink(string $name, string $value, array $meta = null)
20+
{
21+
$this->links[$name] = $meta ? ['href' => $value, 'meta' => $meta] : $value;
22+
}
23+
}

0 commit comments

Comments
 (0)