Skip to content

Commit 5f47c15

Browse files
authored
Relationship with no "data" member. Fixed #92 (#93)
1 parent 668ec0c commit 5f47c15

19 files changed

+132
-24
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: php
22
php:
33
- '7.1'
4+
- '7.2'
5+
- '7.3'
46

57
before_script:
68
- composer install

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [2.1.0] - 2019-02-25
10+
### Fixed
11+
- Relationship without data property (#92)
12+
913
## [2.0.1] - 2018-12-31
1014
### Changed
1115
- Downgraded min required php version to 7.1
@@ -14,5 +18,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1418
### Added
1519
- v2 initial release
1620

17-
[Unreleased]: https://github.com/json-api-php/json-api/compare/2.0.1...HEAD
21+
[Unreleased]: https://github.com/json-api-php/json-api/compare/2.1.0...HEAD
22+
[2.1.0]: https://github.com/json-api-php/json-api/compare/2.0.1...2.1.0
1823
[2.0.1]: https://github.com/json-api-php/json-api/compare/2.0.0...2.0.1

phpunit.xml.dist

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
bootstrap="vendor/autoload.php"
44
stopOnFailure="false"
55
verbose="true"
6-
colors="true"
7-
syntaxCheck="true">
6+
colors="true">
87
<testsuites>
98
<testsuite name="Main">
109
<directory>./test</directory>

src/EmptyRelationship.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JsonApiPhp\JsonApi;
4+
5+
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
6+
use JsonApiPhp\JsonApi\Internal\ResourceField;
7+
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;
8+
9+
/**
10+
* A relationship with no data
11+
*/
12+
class EmptyRelationship implements ResourceField
13+
{
14+
use ResourceFieldTrait;
15+
16+
private $obj;
17+
18+
public function __construct(string $name, RelationshipMember $member, RelationshipMember ...$members)
19+
{
20+
$this->name = $name;
21+
$this->obj = combine($member, ...$members);
22+
}
23+
24+
/**
25+
* @param object $o
26+
*/
27+
public function attachTo($o): void
28+
{
29+
child($o, 'relationships')->{$this->name} = $this->obj;
30+
}
31+
}

src/Error/SourceParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace JsonApiPhp\JsonApi\Error;
44

5-
use JsonApiPhp\JsonApi\Internal\ErrorMember;
65
use function JsonApiPhp\JsonApi\child;
6+
use JsonApiPhp\JsonApi\Internal\ErrorMember;
77

88
final class SourceParameter implements ErrorMember
99
{

src/Error/SourcePointer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace JsonApiPhp\JsonApi\Error;
44

5-
use JsonApiPhp\JsonApi\Internal\ErrorMember;
65
use function JsonApiPhp\JsonApi\child;
6+
use JsonApiPhp\JsonApi\Internal\ErrorMember;
77

88
final class SourcePointer implements ErrorMember
99
{

src/Internal/RelationshipMember.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace JsonApiPhp\JsonApi\Internal;
4+
5+
/**
6+
* @internal
7+
*/
8+
interface RelationshipMember extends ToOneMember, ToManyMember
9+
{
10+
}

src/Internal/ToManyMember.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Internal;
44

5-
interface ToManyMember extends ToOneMember
5+
/**
6+
* @internal
7+
*/
8+
interface ToManyMember extends Attachable
69
{
710
}

src/Link/AboutLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\ErrorMember;
67
use JsonApiPhp\JsonApi\Internal\LinkTrait;
7-
use function JsonApiPhp\JsonApi\child;
88

99
final class AboutLink implements ErrorMember
1010
{

src/Link/FirstLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\LinkTrait;
67
use JsonApiPhp\JsonApi\Internal\PaginationLink;
7-
use function JsonApiPhp\JsonApi\child;
88

99
final class FirstLink implements PaginationLink
1010
{

src/Link/LastLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\LinkTrait;
67
use JsonApiPhp\JsonApi\Internal\PaginationLink;
7-
use function JsonApiPhp\JsonApi\child;
88

99
final class LastLink implements PaginationLink
1010
{

src/Link/NextLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\LinkTrait;
67
use JsonApiPhp\JsonApi\Internal\PaginationLink;
7-
use function JsonApiPhp\JsonApi\child;
88

99
final class NextLink implements PaginationLink
1010
{

src/Link/PrevLink.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\LinkTrait;
67
use JsonApiPhp\JsonApi\Internal\PaginationLink;
7-
use function JsonApiPhp\JsonApi\child;
88

99
final class PrevLink implements PaginationLink
1010
{

src/Link/RelatedLink.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5-
use JsonApiPhp\JsonApi\Internal\LinkTrait;
6-
use JsonApiPhp\JsonApi\Internal\ToOneMember;
75
use function JsonApiPhp\JsonApi\child;
6+
use JsonApiPhp\JsonApi\Internal\LinkTrait;
7+
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
88

9-
final class RelatedLink implements ToOneMember
9+
final class RelatedLink implements RelationshipMember
1010
{
1111
use LinkTrait;
1212

src/Link/SelfLink.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace JsonApiPhp\JsonApi\Link;
44

5+
use function JsonApiPhp\JsonApi\child;
56
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
67
use JsonApiPhp\JsonApi\Internal\LinkTrait;
8+
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
79
use JsonApiPhp\JsonApi\Internal\ResourceMember;
8-
use JsonApiPhp\JsonApi\Internal\ToOneMember;
9-
use function JsonApiPhp\JsonApi\child;
1010

11-
final class SelfLink implements DataDocumentMember, ResourceMember, ToOneMember
11+
final class SelfLink implements DataDocumentMember, ResourceMember, RelationshipMember
1212
{
1313
use LinkTrait;
1414

src/Meta.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
77
use JsonApiPhp\JsonApi\Internal\ErrorMember;
88
use JsonApiPhp\JsonApi\Internal\MetaDocumentMember;
9+
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
910
use JsonApiPhp\JsonApi\Internal\ResourceMember;
10-
use JsonApiPhp\JsonApi\Internal\ToOneMember;
1111

12-
final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, ToOneMember
12+
final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember
1313
{
1414
/**
1515
* @var string

src/ToMany.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
use JsonApiPhp\JsonApi\Internal\Identifier;
66
use JsonApiPhp\JsonApi\Internal\ResourceField;
77
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;
8-
use JsonApiPhp\JsonApi\Internal\ToOneMember;
8+
use JsonApiPhp\JsonApi\Internal\ToManyMember;
99

1010
final class ToMany implements Identifier, ResourceField
1111
{
1212
use ResourceFieldTrait;
1313
/**
14-
* @var ToOneMember[]
14+
* @var ToManyMember[]
1515
*/
1616
private $members;
1717
/**
1818
* @var ResourceIdentifierCollection
1919
*/
2020
private $collection;
2121

22-
public function __construct(string $name, ResourceIdentifierCollection $collection, ToOneMember ...$members)
22+
public function __construct(string $name, ResourceIdentifierCollection $collection, ToManyMember ...$members)
2323
{
2424
$this->validateFieldName($name);
2525
$this->name = $name;

test/CompoundDocumentTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource()
253253

254254
/**
255255
* A compound document MUST NOT include more than one resource object for each type and id pair.
256-
* @expectedException \LogicException
257-
* @expectedExceptionMessage Resource apples:1 is already included
258256
*/
259257
public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers()
260258
{
259+
$this->expectException('LogicException');
260+
$this->expectExceptionMessage('Resource apples:1 is already included');
261261
$apple = new ResourceObject('apples', '1');
262262
new CompoundDocument($apple->identifier(), new Included($apple, $apple));
263263
}

test/ResourceObjectTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use JsonApiPhp\JsonApi\Attribute;
66
use JsonApiPhp\JsonApi\DataDocument;
7+
use JsonApiPhp\JsonApi\EmptyRelationship;
78
use JsonApiPhp\JsonApi\Link\RelatedLink;
89
use JsonApiPhp\JsonApi\Link\SelfLink;
910
use JsonApiPhp\JsonApi\Meta;
@@ -152,6 +153,63 @@ public function testRelationshipWithEmptyMultiIdLinkage()
152153
);
153154
}
154155

156+
public function testRelationshipWithNoData()
157+
{
158+
$this->assertEncodesTo(
159+
'
160+
{
161+
"data": {
162+
"type": "basket",
163+
"id": "1",
164+
"relationships": {
165+
"empty": {
166+
"links": {
167+
"related": "/foo"
168+
}
169+
}
170+
}
171+
}
172+
}
173+
',
174+
new DataDocument(
175+
new ResourceObject(
176+
'basket',
177+
'1',
178+
new EmptyRelationship('empty', new RelatedLink('/foo'))
179+
)
180+
)
181+
);
182+
183+
$this->assertEncodesTo(
184+
'
185+
{
186+
"data": {
187+
"type": "basket",
188+
"id": "1",
189+
"relationships": {
190+
"empty": {
191+
"links": {
192+
"related": "/foo",
193+
"self": "/bar"
194+
},
195+
"meta": {
196+
"foo": "bar"
197+
}
198+
}
199+
}
200+
}
201+
}
202+
',
203+
new DataDocument(
204+
new ResourceObject(
205+
'basket',
206+
'1',
207+
new EmptyRelationship('empty', new RelatedLink('/foo'), new SelfLink('/bar'), new Meta('foo', 'bar'))
208+
)
209+
)
210+
);
211+
}
212+
155213
public function testCanNotCreateIdAttribute()
156214
{
157215
$this->expectException(\DomainException::class);

0 commit comments

Comments
 (0)