Skip to content

Commit 52c9f69

Browse files
committed
Close #206
1 parent d8431d9 commit 52c9f69

File tree

5 files changed

+118
-16
lines changed

5 files changed

+118
-16
lines changed

src/Contracts/Document/DocumentInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ public function addError(ErrorInterface $error): void;
242242
*
243243
* If you add errors information no other elements will be in output document.
244244
*
245-
* @param ErrorInterface[]|ErrorCollection $errors
245+
* @param ErrorInterface[]|ErrorCollection|iterable $errors
246246
*
247247
* @return void
248248
*/
249-
public function addErrors($errors): void;
249+
public function addErrors(iterable $errors): void;
250250

251251
/**
252252
* Add JSON API version information.

src/Contracts/Encoder/EncoderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function encodeError(ErrorInterface $error): string;
132132
/**
133133
* Encode errors as JSON API string.
134134
*
135-
* @param ErrorInterface[]|ErrorCollection $errors
135+
* @param ErrorInterface[]|ErrorCollection|iterable $errors
136136
*
137137
* @return string
138138
*/

src/Document/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function addError(ErrorInterface $error): void
394394
/**
395395
* @inheritdoc
396396
*/
397-
public function addErrors($errors): void
397+
public function addErrors(iterable $errors): void
398398
{
399399
empty($this->errors) === false ?: $this->errors = [];
400400

src/Document/Error.php

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ public function __construct(
8787
array $source = null,
8888
$meta = null
8989
) {
90-
assert($idx === null || is_int($idx) === true || is_string($idx) === true);
91-
92-
$this->idx = $idx;
93-
$this->links = ($aboutLink === null ? null : [DocumentInterface::KEYWORD_ERRORS_ABOUT => $aboutLink]);
94-
$this->status = ($status !== null ? (string)$status : null);
95-
$this->code = ($code !== null ? (string)$code : null);
96-
$this->title = $title;
97-
$this->source = $source;
98-
$this->detail = $detail;
99-
$this->meta = $meta;
90+
$this
91+
->setId($idx)
92+
->setLink(DocumentInterface::KEYWORD_ERRORS_ABOUT, $aboutLink)
93+
->setStatus($status)
94+
->setCode($code)
95+
->setTitle($title)
96+
->setDetail($detail)
97+
->setSource($source)
98+
->setMeta($meta);
10099
}
101100

102101
/**
@@ -107,6 +106,20 @@ public function getId()
107106
return $this->idx;
108107
}
109108

109+
/**
110+
* @param string|int|null $idx
111+
*
112+
* @return self
113+
*/
114+
public function setId($idx): self
115+
{
116+
assert($idx === null || is_int($idx) === true || is_string($idx) === true);
117+
118+
$this->idx = $idx;
119+
120+
return $this;
121+
}
122+
110123
/**
111124
* @inheritdoc
112125
*/
@@ -115,6 +128,23 @@ public function getLinks(): ?array
115128
return $this->links;
116129
}
117130

131+
/**
132+
* @param string $name
133+
* @param LinkInterface|null $link
134+
*
135+
* @return self
136+
*/
137+
public function setLink(string $name, ?LinkInterface $link): self
138+
{
139+
if ($link !== null) {
140+
$this->links[$name] = $link;
141+
} else {
142+
unset($this->links[$name]);
143+
}
144+
145+
return $this;
146+
}
147+
118148
/**
119149
* @inheritdoc
120150
*/
@@ -123,6 +153,18 @@ public function getStatus(): ?string
123153
return $this->status;
124154
}
125155

156+
/**
157+
* @param string|null $status
158+
*
159+
* @return self
160+
*/
161+
public function setStatus(?string $status): self
162+
{
163+
$this->status = $status;
164+
165+
return $this;
166+
}
167+
126168
/**
127169
* @inheritdoc
128170
*/
@@ -131,6 +173,18 @@ public function getCode(): ?string
131173
return $this->code;
132174
}
133175

176+
/**
177+
* @param string|null $code
178+
*
179+
* @return self
180+
*/
181+
public function setCode(?string $code): self
182+
{
183+
$this->code = $code;
184+
185+
return $this;
186+
}
187+
134188
/**
135189
* @inheritdoc
136190
*/
@@ -139,6 +193,18 @@ public function getTitle(): ?string
139193
return $this->title;
140194
}
141195

196+
/**
197+
* @param null|string $title
198+
*
199+
* @return self
200+
*/
201+
public function setTitle(?string $title): self
202+
{
203+
$this->title = $title;
204+
205+
return $this;
206+
}
207+
142208
/**
143209
* @inheritdoc
144210
*/
@@ -147,6 +213,18 @@ public function getDetail(): ?string
147213
return $this->detail;
148214
}
149215

216+
/**
217+
* @param null|string $detail
218+
*
219+
* @return self
220+
*/
221+
public function setDetail(?string $detail): self
222+
{
223+
$this->detail = $detail;
224+
225+
return $this;
226+
}
227+
150228
/**
151229
* @inheritdoc
152230
*/
@@ -155,11 +233,35 @@ public function getSource(): ?array
155233
return $this->source;
156234
}
157235

236+
/**
237+
* @param array|null $source
238+
*
239+
* @return self
240+
*/
241+
public function setSource(?array $source): self
242+
{
243+
$this->source = $source;
244+
245+
return $this;
246+
}
247+
158248
/**
159249
* @inheritdoc
160250
*/
161251
public function getMeta()
162252
{
163253
return $this->meta;
164254
}
255+
256+
/**
257+
* @param mixed|null $meta
258+
*
259+
* @return self
260+
*/
261+
public function setMeta($meta): self
262+
{
263+
$this->meta = $meta;
264+
265+
return $this;
266+
}
165267
}

src/Encoder/Encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ protected function encodeErrorToArray(ErrorInterface $error): array
327327
}
328328

329329
/**
330-
* @param $errors
330+
* @param iterable $errors
331331
*
332332
* @return array
333333
*/
334-
protected function encodeErrorsToArray($errors): array
334+
protected function encodeErrorsToArray(iterable $errors): array
335335
{
336336
$docWriter = $this->getFactory()->createDocument();
337337
$docWriter->addErrors($errors);

0 commit comments

Comments
 (0)