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