[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/tobscure/json-api/src/ -> Document.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of JSON-API.
   5   *
   6   * (c) Toby Zerner <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Tobscure\JsonApi;
  13  
  14  use JsonSerializable;
  15  
  16  class Document implements JsonSerializable
  17  {
  18      use LinksTrait;
  19      use MetaTrait;
  20  
  21      const MEDIA_TYPE = 'application/vnd.api+json';
  22  
  23      /**
  24       * The included array.
  25       *
  26       * @var array
  27       */
  28      protected $included = [];
  29  
  30      /**
  31       * The errors array.
  32       *
  33       * @var array
  34       */
  35      protected $errors;
  36  
  37      /**
  38       * The jsonapi array.
  39       *
  40       * @var array
  41       */
  42      protected $jsonapi;
  43  
  44      /**
  45       * The data object.
  46       *
  47       * @var ElementInterface
  48       */
  49      protected $data;
  50  
  51      /**
  52       * @param ElementInterface $data
  53       */
  54      public function __construct(ElementInterface $data = null)
  55      {
  56          $this->data = $data;
  57      }
  58  
  59      /**
  60       * Get included resources.
  61       *
  62       * @param \Tobscure\JsonApi\ElementInterface $element
  63       * @param bool $includeParent
  64       *
  65       * @return \Tobscure\JsonApi\Resource[]
  66       */
  67      protected function getIncluded(ElementInterface $element, $includeParent = false)
  68      {
  69          $included = [];
  70  
  71          foreach ($element->getResources() as $resource) {
  72              if ($resource->isIdentifier()) {
  73                  continue;
  74              }
  75  
  76              if ($includeParent) {
  77                  $included = $this->mergeResource($included, $resource);
  78              } else {
  79                  $type = $resource->getType();
  80                  $id = $resource->getId();
  81              }
  82  
  83              foreach ($resource->getUnfilteredRelationships() as $relationship) {
  84                  $includedElement = $relationship->getData();
  85  
  86                  if (! $includedElement instanceof ElementInterface) {
  87                      continue;
  88                  }
  89  
  90                  foreach ($this->getIncluded($includedElement, true) as $child) {
  91                      // If this resource is the same as the top-level "data"
  92                      // resource, then we don't want it to show up again in the
  93                      // "included" array.
  94                      if (! $includeParent && $child->getType() === $type && $child->getId() === $id) {
  95                          continue;
  96                      }
  97  
  98                      $included = $this->mergeResource($included, $child);
  99                  }
 100              }
 101          }
 102  
 103          $flattened = [];
 104  
 105          array_walk_recursive($included, function ($a) use (&$flattened) {
 106              $flattened[] = $a;
 107          });
 108  
 109          return $flattened;
 110      }
 111  
 112      /**
 113       * @param \Tobscure\JsonApi\Resource[] $resources
 114       * @param \Tobscure\JsonApi\Resource $newResource
 115       *
 116       * @return \Tobscure\JsonApi\Resource[]
 117       */
 118      protected function mergeResource(array $resources, Resource $newResource)
 119      {
 120          $type = $newResource->getType();
 121          $id = $newResource->getId();
 122  
 123          if (isset($resources[$type][$id])) {
 124              $resources[$type][$id]->merge($newResource);
 125          } else {
 126              $resources[$type][$id] = $newResource;
 127          }
 128  
 129          return $resources;
 130      }
 131  
 132      /**
 133       * Set the data object.
 134       *
 135       * @param \Tobscure\JsonApi\ElementInterface $element
 136       *
 137       * @return $this
 138       */
 139      public function setData(ElementInterface $element)
 140      {
 141          $this->data = $element;
 142  
 143          return $this;
 144      }
 145  
 146      /**
 147       * Set the errors array.
 148       *
 149       * @param array $errors
 150       *
 151       * @return $this
 152       */
 153      public function setErrors($errors)
 154      {
 155          $this->errors = $errors;
 156  
 157          return $this;
 158      }
 159  
 160      /**
 161       * Set the jsonapi array.
 162       *
 163       * @param array $jsonapi
 164       *
 165       * @return $this
 166       */
 167      public function setJsonapi($jsonapi)
 168      {
 169          $this->jsonapi = $jsonapi;
 170  
 171          return $this;
 172      }
 173  
 174      /**
 175       * Map everything to arrays.
 176       *
 177       * @return array
 178       */
 179      public function toArray()
 180      {
 181          $document = [];
 182  
 183          if (! empty($this->links)) {
 184              $document['links'] = $this->links;
 185          }
 186  
 187          if (! empty($this->data)) {
 188              $document['data'] = $this->data->toArray();
 189  
 190              $resources = $this->getIncluded($this->data);
 191  
 192              if (count($resources)) {
 193                  $document['included'] = array_map(function (Resource $resource) {
 194                      return $resource->toArray();
 195                  }, $resources);
 196              }
 197          }
 198  
 199          if (! empty($this->meta)) {
 200              $document['meta'] = $this->meta;
 201          }
 202  
 203          if (! empty($this->errors)) {
 204              $document['errors'] = $this->errors;
 205          }
 206  
 207          if (! empty($this->jsonapi)) {
 208              $document['jsonapi'] = $this->jsonapi;
 209          }
 210  
 211          return $document;
 212      }
 213  
 214      /**
 215       * Map to string.
 216       *
 217       * @return string
 218       */
 219      public function __toString()
 220      {
 221          return json_encode($this->toArray());
 222      }
 223  
 224      /**
 225       * Serialize for JSON usage.
 226       *
 227       * @return array
 228       */
 229      #[\ReturnTypeWillChange]
 230      public function jsonSerialize()
 231      {
 232          return $this->toArray();
 233      }
 234  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer