Skip to content

Commit

Permalink
Close #206
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Mar 29, 2018
1 parent d8431d9 commit 52c9f69
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Contracts/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ public function addError(ErrorInterface $error): void;
*
* If you add errors information no other elements will be in output document.
*
* @param ErrorInterface[]|ErrorCollection $errors
* @param ErrorInterface[]|ErrorCollection|iterable $errors
*
* @return void
*/
public function addErrors($errors): void;
public function addErrors(iterable $errors): void;

/**
* Add JSON API version information.
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function encodeError(ErrorInterface $error): string;
/**
* Encode errors as JSON API string.
*
* @param ErrorInterface[]|ErrorCollection $errors
* @param ErrorInterface[]|ErrorCollection|iterable $errors
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function addError(ErrorInterface $error): void
/**
* @inheritdoc
*/
public function addErrors($errors): void
public function addErrors(iterable $errors): void
{
empty($this->errors) === false ?: $this->errors = [];

Expand Down
122 changes: 112 additions & 10 deletions src/Document/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ public function __construct(
array $source = null,
$meta = null
) {
assert($idx === null || is_int($idx) === true || is_string($idx) === true);

$this->idx = $idx;
$this->links = ($aboutLink === null ? null : [DocumentInterface::KEYWORD_ERRORS_ABOUT => $aboutLink]);
$this->status = ($status !== null ? (string)$status : null);
$this->code = ($code !== null ? (string)$code : null);
$this->title = $title;
$this->source = $source;
$this->detail = $detail;
$this->meta = $meta;
$this
->setId($idx)
->setLink(DocumentInterface::KEYWORD_ERRORS_ABOUT, $aboutLink)
->setStatus($status)
->setCode($code)
->setTitle($title)
->setDetail($detail)
->setSource($source)
->setMeta($meta);
}

/**
Expand All @@ -107,6 +106,20 @@ public function getId()
return $this->idx;
}

/**
* @param string|int|null $idx
*
* @return self
*/
public function setId($idx): self
{
assert($idx === null || is_int($idx) === true || is_string($idx) === true);

$this->idx = $idx;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -115,6 +128,23 @@ public function getLinks(): ?array
return $this->links;
}

/**
* @param string $name
* @param LinkInterface|null $link
*
* @return self
*/
public function setLink(string $name, ?LinkInterface $link): self
{
if ($link !== null) {
$this->links[$name] = $link;
} else {
unset($this->links[$name]);
}

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -123,6 +153,18 @@ public function getStatus(): ?string
return $this->status;
}

/**
* @param string|null $status
*
* @return self
*/
public function setStatus(?string $status): self
{
$this->status = $status;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -131,6 +173,18 @@ public function getCode(): ?string
return $this->code;
}

/**
* @param string|null $code
*
* @return self
*/
public function setCode(?string $code): self
{
$this->code = $code;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -139,6 +193,18 @@ public function getTitle(): ?string
return $this->title;
}

/**
* @param null|string $title
*
* @return self
*/
public function setTitle(?string $title): self
{
$this->title = $title;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -147,6 +213,18 @@ public function getDetail(): ?string
return $this->detail;
}

/**
* @param null|string $detail
*
* @return self
*/
public function setDetail(?string $detail): self
{
$this->detail = $detail;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -155,11 +233,35 @@ public function getSource(): ?array
return $this->source;
}

/**
* @param array|null $source
*
* @return self
*/
public function setSource(?array $source): self
{
$this->source = $source;

return $this;
}

/**
* @inheritdoc
*/
public function getMeta()
{
return $this->meta;
}

/**
* @param mixed|null $meta
*
* @return self
*/
public function setMeta($meta): self
{
$this->meta = $meta;

return $this;
}
}
4 changes: 2 additions & 2 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ protected function encodeErrorToArray(ErrorInterface $error): array
}

/**
* @param $errors
* @param iterable $errors
*
* @return array
*/
protected function encodeErrorsToArray($errors): array
protected function encodeErrorsToArray(iterable $errors): array
{
$docWriter = $this->getFactory()->createDocument();
$docWriter->addErrors($errors);
Expand Down

0 comments on commit 52c9f69

Please sign in to comment.