Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit b6a424e

Browse files
committed
Merge pull request #85 from josephmcdermott/feature/relationship-without-data
Allow relationship without requirement of 'data'
2 parents 46142e5 + d4129e1 commit b6a424e

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/Document.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ protected function getIncluded(ElementInterface $element, $includeParent = false
8080
foreach ($resource->getUnfilteredRelationships() as $relationship) {
8181
$includedElement = $relationship->getData();
8282

83+
if (! $includedElement instanceof ElementInterface) {
84+
continue;
85+
}
86+
8387
foreach ($this->getIncluded($includedElement, true) as $child) {
8488
// If this resource is the same as the top-level "data"
8589
// resource, then we don't want it to show up again in the

src/Relationship.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ class Relationship
1919
/**
2020
* The data object.
2121
*
22-
* @var ElementInterface
22+
* @var ElementInterface|null
2323
*/
2424
protected $data;
2525

2626
/**
2727
* Create a new relationship.
2828
*
29-
* @param ElementInterface $data
29+
* @param ElementInterface|null $data
3030
*/
31-
public function __construct(ElementInterface $data)
31+
public function __construct(ElementInterface $data = null)
3232
{
3333
$this->data = $data;
3434
}
3535

3636
/**
3737
* Get the data object.
3838
*
39-
* @return ElementInterface
39+
* @return ElementInterface|null
4040
*/
4141
public function getData()
4242
{
@@ -46,7 +46,7 @@ public function getData()
4646
/**
4747
* Set the data object.
4848
*
49-
* @param ElementInterface $data
49+
* @param ElementInterface|null $data
5050
* @return $this
5151
*/
5252
public function setData($data)

src/Resource.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ protected function buildRelationships()
271271
$relationship = $this->serializer->getRelationship($this->data, $name);
272272

273273
if ($relationship) {
274-
$relationship->getData()->with($nested)->fields($this->fields);
274+
$relationshipData = $relationship->getData();
275+
if ($relationshipData instanceof ElementInterface) {
276+
$relationshipData->with($nested)->fields($this->fields);
277+
}
275278

276279
$relationships[$name] = $relationship;
277280
}

0 commit comments

Comments
 (0)