Skip to content

Commit 3a56655

Browse files
committed
Close #175
1 parent 84355a8 commit 3a56655

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

src/Contracts/Schema/ResourceObjectInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getType();
3333
/**
3434
* Get resource ID.
3535
*
36-
* @return string
36+
* @return string|null
3737
*/
3838
public function getId();
3939

src/Document/Presenters/ElementPresenter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ private function convertResourceToArray(ResourceObjectInterface $resource, $reso
288288
{
289289
$representation = [
290290
Document::KEYWORD_TYPE => $resource->getType(),
291-
Document::KEYWORD_ID => $resource->getId(),
292291
];
292+
if (($resourceId = $resource->getId()) !== null) {
293+
$representation[Document::KEYWORD_ID] = $resourceId;
294+
}
293295

294296
$attributes = $resource->getAttributes();
295297

src/Schema/ResourceObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
class ResourceObject implements ResourceObjectInterface
2828
{
2929
/**
30-
* @var string
30+
* @var string|false
3131
*/
32-
private $idx;
32+
private $idx = false;
3333

3434
/**
3535
* @var array
@@ -149,7 +149,12 @@ public function getType()
149149
*/
150150
public function getId()
151151
{
152-
return $this->idx === null ? $this->idx = (string)$this->schema->getId($this->resource) : $this->idx;
152+
if ($this->idx === false) {
153+
$index = $this->schema->getId($this->resource);
154+
$this->idx = $index === null ? $index : (string)$index;
155+
}
156+
157+
return $this->idx;
153158
}
154159

155160
/**

tests/Data/AuthorSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getAttributes($author)
5252
*/
5353
public function getRelationships($author, $isPrimary, array $includeRelationships)
5454
{
55-
assert('$author instanceof '.Author::class);
55+
assert('$author instanceof '. Author::class);
5656

5757
if (($isPrimary && $this->isIsLinksInPrimary()) || (!$isPrimary && $this->isIsLinksInIncluded())) {
5858
$selfLink = $this->getRelationshipSelfLink($author, Author::LINK_COMMENTS);

tests/Encoder/EncodeSimpleObjectsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,43 @@ public function testEncodeObjectWithAttributesOnly()
175175
$this->assertEquals($expected, $actual);
176176
}
177177

178+
/**
179+
* Test encode simple object without ID and attributes only.
180+
*/
181+
public function testEncodeObjectWithAttributesOnlyAndNoId()
182+
{
183+
$author = Author::instance(9, 'Dan', 'Gebhardt');
184+
$author->{Author::ATTRIBUTE_ID} = null;
185+
$encoder = Encoder::instance([
186+
Author::class => function ($factory) {
187+
$schema = new AuthorSchema($factory);
188+
$schema->linkRemove(Author::LINK_COMMENTS);
189+
$schema->setResourceLinksClosure(function () {
190+
return []; // no `self` link and others
191+
});
192+
return $schema;
193+
}
194+
], $this->encoderOptions);
195+
196+
$actual = $encoder->encodeData($author);
197+
198+
$expected = <<<EOL
199+
{
200+
"data" : {
201+
"type" : "people",
202+
"attributes" : {
203+
"first_name" : "Dan",
204+
"last_name" : "Gebhardt"
205+
}
206+
}
207+
}
208+
EOL;
209+
// remove formatting from 'expected'
210+
$expected = json_encode(json_decode($expected));
211+
212+
$this->assertEquals($expected, $actual);
213+
}
214+
178215
/**
179216
* Test encode simple object with attributes and custom links.
180217
*

0 commit comments

Comments
 (0)