comparison core/modules/jsonapi/src/JsonApiResource/ErrorCollection.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\jsonapi\JsonApiResource;
4
5 use Drupal\Component\Assertion\Inspector;
6 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
7
8 /**
9 * To be used when the primary data is `errors`.
10 *
11 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
12 * may change at any time and could break any dependencies on it.
13 *
14 * @see https://www.drupal.org/project/jsonapi/issues/3032787
15 * @see jsonapi.api.php
16 *
17 * (The spec says the top-level `data` and `errors` members MUST NOT coexist.)
18 * @see http://jsonapi.org/format/#document-top-level
19 *
20 * @see http://jsonapi.org/format/#error-objects
21 */
22 class ErrorCollection implements \IteratorAggregate {
23
24 /**
25 * The HTTP exceptions.
26 *
27 * @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface[]
28 */
29 protected $errors;
30
31 /**
32 * Instantiates an ErrorCollection object.
33 *
34 * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface[] $errors
35 * The errors.
36 */
37 public function __construct(array $errors) {
38 assert(Inspector::assertAll(function ($error) {
39 return $error instanceof HttpExceptionInterface;
40 }, $errors));
41 $this->errors = $errors;
42 }
43
44 /**
45 * Returns an iterator for errors.
46 *
47 * @return \ArrayIterator
48 * An \ArrayIterator instance
49 */
50 public function getIterator() {
51 return new \ArrayIterator($this->errors);
52 }
53
54 }