- md5
- 8b0081a94ef4990628896937b87d0352
- sha1
- 1d7cc4afa3daf759bdca141fff855b5ac5249db1
- sha256
- 1cbeec26e12dd66b45499bc804dfe4dc2e044f551c2f7606640d544ccac822b9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
<?php
/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tobscure\JsonApi;
class Collection implements ElementInterface
{
/**
* @var array
*/
protected $resources = [];
/**
* Create a new collection instance.
*
* @param mixed $data
* @param \Tobscure\JsonApi\SerializerInterface $serializer
*/
public function __construct($data, SerializerInterface $serializer)
{
$this->resources = $this->buildResources($data, $serializer);
}
/**
* Convert an array of raw data to Resource objects.
*
* @param mixed $data
* @param SerializerInterface $serializer
*
* @return \Tobscure\JsonApi\Resource[]
*/
protected function buildResources($data, SerializerInterface $serializer)
{
$resources = [];
foreach ($data as $resource) {
if (! ($resource instanceof Resource)) {
$resource = new Resource($resource, $serializer);
}
$resources[] = $resource;
}
return $resources;
}
/**
* {@inheritdoc}
*/
public function getResources()
{
return $this->resources;
}
/**
* Set the resources array.
*
* @param array $resources
*
* @return void
*/
public function setResources($resources)
{
$this->resources = $resources;
}
/**
* Request a relationship to be included for all resources.
*
* @param string|array $relationships
*
* @return $this
*/
public function with($relationships)
{
foreach ($this->resources as $resource) {
$resource->with($relationships);
}
return $this;
}
/**
* Request a relationship to be identified for all resources.
*
* @param string|array $relationships
*
* @return $this
*/
public function identify($relationships)
{
foreach ($this->resources as $resource) {
$resource->identify($relationships);
}
return $this;
}
/**
* Request a restricted set of fields.
*
* @param array|null $fields
*
* @return $this
*/
public function fields($fields)
{
foreach ($this->resources as $resource) {
$resource->fields($fields);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return array_map(function (Resource $resource) {
return $resource->toArray();
}, $this->resources);
}
/**
* {@inheritdoc}
*/
public function toIdentifier()
{
return array_map(function (Resource $resource) {
return $resource->toIdentifier();
}, $this->resources);
}
}
The same content is in 37 releases, Joomla 4.0.0-alpha7 to 4.1.0-beta3. See them all, or every version of this file.