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;

0 commit comments

Comments
 (0)