[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/tobscure/json-api/src/ -> Collection.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 Collection implements ElementInterface
  15  {
  16      /**
  17       * @var array
  18       */
  19      protected $resources = [];
  20  
  21      /**
  22       * Create a new collection instance.
  23       *
  24       * @param mixed $data
  25       * @param \Tobscure\JsonApi\SerializerInterface $serializer
  26       */
  27      public function __construct($data, SerializerInterface $serializer)
  28      {
  29          $this->resources = $this->buildResources($data, $serializer);
  30      }
  31  
  32      /**
  33       * Convert an array of raw data to Resource objects.
  34       *
  35       * @param mixed $data
  36       * @param SerializerInterface $serializer
  37       *
  38       * @return \Tobscure\JsonApi\Resource[]
  39       */
  40      protected function buildResources($data, SerializerInterface $serializer)
  41      {
  42          $resources = [];
  43  
  44          foreach ($data as $resource) {
  45              if (! ($resource instanceof Resource)) {
  46                  $resource = new Resource($resource, $serializer);
  47              }
  48  
  49              $resources[] = $resource;
  50          }
  51  
  52          return $resources;
  53      }
  54  
  55      /**
  56       * {@inheritdoc}
  57       */
  58      public function getResources()
  59      {
  60          return $this->resources;
  61      }
  62  
  63      /**
  64       * Set the resources array.
  65       *
  66       * @param array $resources
  67       *
  68       * @return void
  69       */
  70      public function setResources($resources)
  71      {
  72          $this->resources = $resources;
  73      }
  74  
  75      /**
  76       * Request a relationship to be included for all resources.
  77       *
  78       * @param string|array $relationships
  79       *
  80       * @return $this
  81       */
  82      public function with($relationships)
  83      {
  84          foreach ($this->resources as $resource) {
  85              $resource->with($relationships);
  86          }
  87  
  88          return $this;
  89      }
  90  
  91      /**
  92       * Request a restricted set of fields.
  93       *
  94       * @param array|null $fields
  95       *
  96       * @return $this
  97       */
  98      public function fields($fields)
  99      {
 100          foreach ($this->resources as $resource) {
 101              $resource->fields($fields);
 102          }
 103  
 104          return $this;
 105      }
 106  
 107      /**
 108       * {@inheritdoc}
 109       */
 110      public function toArray()
 111      {
 112          return array_map(function (Resource $resource) {
 113              return $resource->toArray();
 114          }, $this->resources);
 115      }
 116  
 117      /**
 118       * {@inheritdoc}
 119       */
 120      public function toIdentifier()
 121      {
 122          return array_map(function (Resource $resource) {
 123              return $resource->toIdentifier();
 124          }, $this->resources);
 125      }
 126  }


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