Skip to content

Commit 36b0d29

Browse files
authored
Link model fixes (#58)
1 parent 6b54910 commit 36b0d29

33 files changed

+143
-22
lines changed

.php_cs.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ return PhpCsFixer\Config::create()
2121
'method_separation' => true,
2222
'native_function_casing' => true,
2323
'no_blank_lines_after_class_opening' => true,
24-
'no_blank_lines_after_phpdoc' => true,
2524
'no_empty_statement' => true,
2625
'no_extra_consecutive_blank_lines' => true,
2726
'no_leading_import_slash' => true,

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ A simple example to illustrate the general idea. This JSON representation from
2727
}
2828
}
2929
```
30-
can be built with the following php code:
30+
can be built with the following php code (less imports):
3131
```php
3232
<?php
3333
$articles = new ResourceObject('articles', '1');
3434
$author = Relationship::fromLinkage(
35-
Linkage::fromSingleIdentifier(
35+
new SingleLinkage(
3636
new ResourceIdentifier('people', '9')
3737
)
3838
);
3939
$author->setLink('self', '/articles/1/relationships/author');
40-
$author->setLink('related', '/articles/1/author');
40+
$author->setLink('related','/articles/1/author');
4141
$articles->setRelationship('author', $author);
4242
$articles->setAttribute('title', 'Rails is Omakase');
4343
$doc = Document::fromResource($articles);

src/Document.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi;

src/Document/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document;

src/Document/Link/Link.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
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+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace JsonApiPhp\JsonApi\Document\Link;
14+
15+
final class Link implements LinkInterface
16+
{
17+
private $url;
18+
19+
public function __construct(string $url)
20+
{
21+
$this->url = $url;
22+
}
23+
24+
public function jsonSerialize()
25+
{
26+
return $this->url;
27+
}
28+
}

src/Document/Link/LinkInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
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+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace JsonApiPhp\JsonApi\Document\Link;
14+
15+
interface LinkInterface extends \JsonSerializable
16+
{
17+
public function jsonSerialize();
18+
}

src/Document/Link/LinkObject.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
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+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace JsonApiPhp\JsonApi\Document\Link;
14+
15+
use JsonApiPhp\JsonApi\Document\Meta;
16+
17+
final class LinkObject implements LinkInterface
18+
{
19+
private $link;
20+
21+
public function __construct(string $href, Meta $meta = null)
22+
{
23+
$this->link['href'] = $href;
24+
if ($meta) {
25+
$this->link['meta'] = $meta;
26+
}
27+
}
28+
29+
public function jsonSerialize()
30+
{
31+
return $this->link;
32+
}
33+
}

src/Document/LinksTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document;
1314

15+
use JsonApiPhp\JsonApi\Document\Link\Link;
16+
use JsonApiPhp\JsonApi\Document\Link\LinkInterface;
17+
1418
trait LinksTrait
1519
{
20+
/**
21+
* @var LinkInterface[]
22+
*/
1623
protected $links;
1724

18-
public function setLink(string $name, string $value, array $meta = null)
25+
public function setLink(string $name, string $url)
26+
{
27+
$this->links[$name] = new Link($url);
28+
}
29+
30+
public function setLinkObject(string $name, LinkInterface $link)
1931
{
20-
$this->links[$name] = $meta ? ['href' => $value, 'meta' => $meta] : $value;
32+
$this->links[$name] = $link;
2133
}
2234
}

src/Document/Meta.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document;

src/Document/MetaTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document;

src/Document/PrimaryData/MultiIdentifierData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/PrimaryData/MultiResourceData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/PrimaryData/NullData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/PrimaryData/PrimaryDataInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/PrimaryData/SingleIdentifierData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/PrimaryData/SingleResourceData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\PrimaryData;

src/Document/Resource/Linkage/LinkageInterface.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
<?php declare(strict_types=1);
1+
<?php
22
/**
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-
*/
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+
*/
10+
11+
declare(strict_types=1);
1012

1113
namespace JsonApiPhp\JsonApi\Document\Resource\Linkage;
1214

src/Document/Resource/Linkage/MultiLinkage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource\Linkage;

src/Document/Resource/Linkage/NullLinkage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource\Linkage;

src/Document/Resource/Linkage/SingleLinkage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource\Linkage;

src/Document/Resource/Relationship/Relationship.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource\Relationship;
1314

15+
use JsonApiPhp\JsonApi\Document\Link\LinkInterface;
1416
use JsonApiPhp\JsonApi\Document\LinksTrait;
1517
use JsonApiPhp\JsonApi\Document\Meta;
1618
use JsonApiPhp\JsonApi\Document\MetaTrait;
@@ -38,17 +40,17 @@ public static function fromMeta(Meta $meta): self
3840
return $r;
3941
}
4042

41-
public static function fromSelfLink(string $link, array $meta = null): self
43+
public static function fromSelfLink(LinkInterface $link): self
4244
{
4345
$r = new self;
44-
$r->setLink('self', $link, $meta);
46+
$r->setLinkObject('self', $link);
4547
return $r;
4648
}
4749

48-
public static function fromRelatedLink(string $link, array $meta = null): self
50+
public static function fromRelatedLink(LinkInterface $link): self
4951
{
5052
$r = new self;
51-
$r->setLink('related', $link, $meta);
53+
$r->setLinkObject('related', $link);
5254
return $r;
5355
}
5456

src/Document/Resource/ResourceIdentifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource;

src/Document/Resource/ResourceObject.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document\Resource;

test/BaseTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Test;

test/Document/CompoundDocumentTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Test\Document;
1314

1415
use JsonApiPhp\JsonApi\Document;
16+
use JsonApiPhp\JsonApi\Document\Link\Link;
1517
use JsonApiPhp\JsonApi\Document\Meta;
1618
use JsonApiPhp\JsonApi\Document\Resource\Linkage\MultiLinkage;
1719
use JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage;

test/Document/DocumentTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Test\Document;
1314

1415
use JsonApiPhp\JsonApi\Document;
1516
use JsonApiPhp\JsonApi\Document\Error;
17+
use JsonApiPhp\JsonApi\Document\Link\LinkObject;
1618
use JsonApiPhp\JsonApi\Document\Meta;
1719
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
1820
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
@@ -163,7 +165,7 @@ public function testDocumentCanHaveExtraProperties()
163165
$doc->setApiMeta(Meta::fromArray(['a' => 'b']));
164166
$doc->setMeta(Meta::fromArray(['test' => 'test']));
165167
$doc->setLink('self', 'http://example.com/self');
166-
$doc->setLink('related', 'http://example.com/rel', ['foo' => 'bar']);
168+
$doc->setLinkObject('related', new LinkObject('http://example.com/rel', Meta::fromArray(['foo' => 'bar'])));
167169
$this->assertEncodesTo(
168170
'
169171
{

test/Document/ErrorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Document;

test/Document/MetaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Test\Document;

test/Document/Resource/Relationship/LinkageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
declare(strict_types=1);
1112

1213
namespace JsonApiPhp\JsonApi\Test\Document\Resource\Relationship;
1314

1415
use JsonApiPhp\JsonApi\Document\Resource\Linkage\MultiLinkage;
1516
use JsonApiPhp\JsonApi\Document\Resource\Linkage\NullLinkage;
1617
use JsonApiPhp\JsonApi\Document\Resource\Linkage\SingleLinkage;
17-
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
1818
use JsonApiPhp\JsonApi\Document\Resource\ResourceIdentifier;
1919
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
2020
use JsonApiPhp\JsonApi\Test\BaseTestCase;

0 commit comments

Comments
 (0)