[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/tobscure/json-api/src/ -> Resource.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  class Resource implements ElementInterface
  15  {
  16      use LinksTrait;
  17      use MetaTrait;
  18  
  19      /**
  20       * @var mixed
  21       */
  22      protected $data;
  23  
  24      /**
  25       * @var \Tobscure\JsonApi\SerializerInterface
  26       */
  27      protected $serializer;
  28  
  29      /**
  30       * A list of relationships to include.
  31       *
  32       * @var array
  33       */
  34      protected $includes = [];
  35  
  36      /**
  37       * A list of fields to restrict to.
  38       *
  39       * @var array|null
  40       */
  41      protected $fields;
  42  
  43      /**
  44       * An array of Resources that should be merged into this one.
  45       *
  46       * @var \Tobscure\JsonApi\Resource[]
  47       */
  48      protected $merged = [];
  49  
  50      /**
  51       * @var \Tobscure\JsonApi\Relationship[]
  52       */
  53      private $relationships;
  54  
  55      /**
  56       * @param mixed $data
  57       * @param \Tobscure\JsonApi\SerializerInterface $serializer
  58       */
  59      public function __construct($data, SerializerInterface $serializer)
  60      {
  61          $this->data = $data;
  62          $this->serializer = $serializer;
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function getResources()
  69      {
  70          return [$this];
  71      }
  72  
  73      /**
  74       * {@inheritdoc}
  75       */
  76      public function toArray()
  77      {
  78          $array = $this->toIdentifier();
  79  
  80          if (! $this->isIdentifier()) {
  81              $attributes = $this->getAttributes();
  82              if ($attributes) {
  83                  $array['attributes'] = $attributes;
  84              }
  85          }
  86  
  87          $relationships = $this->getRelationshipsAsArray();
  88  
  89          if (count($relationships)) {
  90              $array['relationships'] = $relationships;
  91          }
  92  
  93          $links = [];
  94          if (! empty($this->links)) {
  95              $links = $this->links;
  96          }
  97          $serializerLinks = $this->serializer->getLinks($this->data);
  98          if (! empty($serializerLinks)) {
  99              $links = array_merge($serializerLinks, $links);
 100          }
 101          if (! empty($links)) {
 102              $array['links'] = $links;
 103          }
 104  
 105          $meta = [];
 106          if (! empty($this->meta)) {
 107              $meta = $this->meta;
 108          }
 109          $serializerMeta = $this->serializer->getMeta($this->data);
 110          if (! empty($serializerMeta)) {
 111              $meta = array_merge($serializerMeta, $meta);
 112          }
 113          if (! empty($meta)) {
 114              $array['meta'] = $meta;
 115          }
 116  
 117          return $array;
 118      }
 119  
 120      /**
 121       * Check whether or not this resource is an identifier (i.e. does it have
 122       * any data attached?).
 123       *
 124       * @return bool
 125       */
 126      public function isIdentifier()
 127      {
 128          return ! is_object($this->data) && ! is_array($this->data);
 129      }
 130  
 131      /**
 132       * {@inheritdoc}
 133       */
 134      public function toIdentifier()
 135      {
 136          if (! $this->data) {
 137              return;
 138          }
 139  
 140          $array = [
 141              'type' => $this->getType(),
 142              'id' => $this->getId()
 143          ];
 144  
 145          if (! empty($this->meta)) {
 146              $array['meta'] = $this->meta;
 147          }
 148  
 149          return $array;
 150      }
 151  
 152      /**
 153       * Get the resource type.
 154       *
 155       * @return string
 156       */
 157      public function getType()
 158      {
 159          return $this->serializer->getType($this->data);
 160      }
 161  
 162      /**
 163       * Get the resource ID.
 164       *
 165       * @return string
 166       */
 167      public function getId()
 168      {
 169          if (! is_object($this->data) && ! is_array($this->data)) {
 170              return (string) $this->data;
 171          }
 172  
 173          return (string) $this->serializer->getId($this->data);
 174      }
 175  
 176      /**
 177       * Get the resource attributes.
 178       *
 179       * @return array
 180       */
 181      public function getAttributes()
 182      {
 183          $attributes = (array) $this->serializer->getAttributes($this->data, $this->getOwnFields());
 184  
 185          $attributes = $this->filterFields($attributes);
 186  
 187          $attributes = $this->mergeAttributes($attributes);
 188  
 189          return $attributes;
 190      }
 191  
 192      /**
 193       * Get the requested fields for this resource type.
 194       *
 195       * @return array|null
 196       */
 197      protected function getOwnFields()
 198      {
 199          $type = $this->getType();
 200  
 201          if (isset($this->fields[$type])) {
 202              return $this->fields[$type];
 203          }
 204      }
 205  
 206      /**
 207       * Filter the given fields array (attributes or relationships) according
 208       * to the requested fieldset.
 209       *
 210       * @param array $fields
 211       *
 212       * @return array
 213       */
 214      protected function filterFields(array $fields)
 215      {
 216          if ($requested = $this->getOwnFields()) {
 217              $fields = array_intersect_key($fields, array_flip($requested));
 218          }
 219  
 220          return $fields;
 221      }
 222  
 223      /**
 224       * Merge the attributes of merged resources into an array of attributes.
 225       *
 226       * @param array $attributes
 227       *
 228       * @return array
 229       */
 230      protected function mergeAttributes(array $attributes)
 231      {
 232          foreach ($this->merged as $resource) {
 233              $attributes = array_replace_recursive($attributes, $resource->getAttributes());
 234          }
 235  
 236          return $attributes;
 237      }
 238  
 239      /**
 240       * Get the resource relationships.
 241       *
 242       * @return \Tobscure\JsonApi\Relationship[]
 243       */
 244      public function getRelationships()
 245      {
 246          $relationships = $this->buildRelationships();
 247  
 248          return $this->filterFields($relationships);
 249      }
 250  
 251      /**
 252       * Get the resource relationships without considering requested ones.
 253       *
 254       * @return \Tobscure\JsonApi\Relationship[]
 255       */
 256      public function getUnfilteredRelationships()
 257      {
 258          return $this->buildRelationships();
 259      }
 260  
 261      /**
 262       * Get the resource relationships as an array.
 263       *
 264       * @return array
 265       */
 266      public function getRelationshipsAsArray()
 267      {
 268          $relationships = $this->getRelationships();
 269  
 270          $relationships = $this->convertRelationshipsToArray($relationships);
 271  
 272          return $this->mergeRelationships($relationships);
 273      }
 274  
 275      /**
 276       * Get an array of built relationships.
 277       *
 278       * @return \Tobscure\JsonApi\Relationship[]
 279       */
 280      protected function buildRelationships()
 281      {
 282          if (isset($this->relationships)) {
 283              return $this->relationships;
 284          }
 285  
 286          $paths = Util::parseRelationshipPaths($this->includes);
 287  
 288          $relationships = [];
 289  
 290          foreach ($paths as $name => $nested) {
 291              $relationship = $this->serializer->getRelationship($this->data, $name);
 292  
 293              if ($relationship) {
 294                  $relationshipData = $relationship->getData();
 295                  if ($relationshipData instanceof ElementInterface) {
 296                      $relationshipData->with($nested)->fields($this->fields);
 297                  }
 298  
 299                  $relationships[$name] = $relationship;
 300              }
 301          }
 302  
 303          return $this->relationships = $relationships;
 304      }
 305  
 306      /**
 307       * Merge the relationships of merged resources into an array of
 308       * relationships.
 309       *
 310       * @param array $relationships
 311       *
 312       * @return array
 313       */
 314      protected function mergeRelationships(array $relationships)
 315      {
 316          foreach ($this->merged as $resource) {
 317              $relationships = array_replace_recursive($relationships, $resource->getRelationshipsAsArray());
 318          }
 319  
 320          return $relationships;
 321      }
 322  
 323      /**
 324       * Convert the given array of Relationship objects into an array.
 325       *
 326       * @param \Tobscure\JsonApi\Relationship[] $relationships
 327       *
 328       * @return array
 329       */
 330      protected function convertRelationshipsToArray(array $relationships)
 331      {
 332          return array_map(function (Relationship $relationship) {
 333              return $relationship->toArray();
 334          }, $relationships);
 335      }
 336  
 337      /**
 338       * Merge a resource into this one.
 339       *
 340       * @param \Tobscure\JsonApi\Resource $resource
 341       *
 342       * @return void
 343       */
 344      public function merge(Resource $resource)
 345      {
 346          $this->merged[] = $resource;
 347      }
 348  
 349      /**
 350       * {@inheritdoc}
 351       */
 352      public function with($relationships)
 353      {
 354          $this->includes = array_unique(array_merge($this->includes, (array) $relationships));
 355  
 356          $this->relationships = null;
 357  
 358          return $this;
 359      }
 360  
 361      /**
 362       * {@inheritdoc}
 363       */
 364      public function fields($fields)
 365      {
 366          $this->fields = $fields;
 367  
 368          return $this;
 369      }
 370  
 371      /**
 372       * @return mixed
 373       */
 374      public function getData()
 375      {
 376          return $this->data;
 377      }
 378  
 379      /**
 380       * @param mixed $data
 381       *
 382       * @return void
 383       */
 384      public function setData($data)
 385      {
 386          $this->data = $data;
 387      }
 388  
 389      /**
 390       * @return \Tobscure\JsonApi\SerializerInterface
 391       */
 392      public function getSerializer()
 393      {
 394          return $this->serializer;
 395      }
 396  
 397      /**
 398       * @param \Tobscure\JsonApi\SerializerInterface $serializer
 399       *
 400       * @return void
 401       */
 402      public function setSerializer(SerializerInterface $serializer)
 403      {
 404          $this->serializer = $serializer;
 405      }
 406  }


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