diff core/modules/jsonapi/src/JsonApiResource/ErrorCollection.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/modules/jsonapi/src/JsonApiResource/ErrorCollection.php	Thu May 09 15:33:08 2019 +0100
@@ -0,0 +1,54 @@
+<?php
+
+namespace Drupal\jsonapi\JsonApiResource;
+
+use Drupal\Component\Assertion\Inspector;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
+
+/**
+ * To be used when the primary data is `errors`.
+ *
+ * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
+ *   may change at any time and could break any dependencies on it.
+ *
+ * @see https://www.drupal.org/project/jsonapi/issues/3032787
+ * @see jsonapi.api.php
+ *
+ * (The spec says the top-level `data` and `errors` members MUST NOT coexist.)
+ * @see http://jsonapi.org/format/#document-top-level
+ *
+ * @see http://jsonapi.org/format/#error-objects
+ */
+class ErrorCollection implements \IteratorAggregate {
+
+  /**
+   * The HTTP exceptions.
+   *
+   * @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface[]
+   */
+  protected $errors;
+
+  /**
+   * Instantiates an ErrorCollection object.
+   *
+   * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface[] $errors
+   *   The errors.
+   */
+  public function __construct(array $errors) {
+    assert(Inspector::assertAll(function ($error) {
+      return $error instanceof HttpExceptionInterface;
+    }, $errors));
+    $this->errors = $errors;
+  }
+
+  /**
+   * Returns an iterator for errors.
+   *
+   * @return \ArrayIterator
+   *   An \ArrayIterator instance
+   */
+  public function getIterator() {
+    return new \ArrayIterator($this->errors);
+  }
+
+}