Skip to content

Commit

Permalink
Make id optional
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed May 24, 2021
1 parent 3bd587b commit 5781035
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Objects/IResourceIdentifierObject.php
Expand Up @@ -29,9 +29,9 @@ interface IResourceIdentifierObject
{

/**
* @return string
* @return string|null
*/
public function getId(): string;
public function getId(): ?string;

/**
* @return string
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/IResourceObject.php
Expand Up @@ -29,9 +29,9 @@ interface IResourceObject
{

/**
* @return string
* @return string|null
*/
public function getId(): string;
public function getId(): ?string;

/**
* @return string
Expand Down
4 changes: 3 additions & 1 deletion src/Objects/ResourceIdentifierCollection.php
Expand Up @@ -178,7 +178,9 @@ public function getIds(): array
$ids = [];

foreach ($this->stack as $identifier) {
$ids[] = $identifier->getId();
if ($identifier->getId() !== null) {
$ids[] = $identifier->getId();
}
}

return $ids;
Expand Down
8 changes: 4 additions & 4 deletions src/Objects/ResourceIdentifierObject.php
Expand Up @@ -32,15 +32,15 @@ class ResourceIdentifierObject implements IResourceIdentifierObject
/** @var string */
private string $type;

/** @var string */
private string $id;
/** @var string|null */
private ?string $id;

public function __construct(IStandardObject $data)
{
$type = $data->get(JsonAPIDocument\IDocument::KEYWORD_TYPE);
$id = $data->get(JsonAPIDocument\IDocument::KEYWORD_ID);

if (!is_string($type) || !is_string($id)) {
if (!is_string($type) || (!is_string($id) && $id !== null)) {
throw new Exceptions\InvalidArgumentException('Data member has invalid format');
}

Expand All @@ -51,7 +51,7 @@ public function __construct(IStandardObject $data)
/**
* {@inheritDoc}
*/
public function getId(): string
public function getId(): ?string
{
return $this->id;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Objects/ResourceObject.php
Expand Up @@ -39,8 +39,10 @@ class ResourceObject implements IResourceObject
public function __construct(Objects\IStandardObject $data)
{
if (
!$data->has(JsonAPIDocument\IDocument::KEYWORD_ID)
|| !is_string($data->get(JsonAPIDocument\IDocument::KEYWORD_ID))
(
$data->has(JsonAPIDocument\IDocument::KEYWORD_ID)
&& !is_string($data->get(JsonAPIDocument\IDocument::KEYWORD_ID))
)
|| !$data->has(JsonAPIDocument\IDocument::KEYWORD_TYPE)
|| !is_string($data->get(JsonAPIDocument\IDocument::KEYWORD_TYPE))
) {
Expand All @@ -54,7 +56,7 @@ public function __construct(Objects\IStandardObject $data)
/**
* {@inheritDoc}
*/
public function getId(): string
public function getId(): ?string
{
return $this->identifier->getId();
}
Expand Down

0 comments on commit 5781035

Please sign in to comment.