mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* @package Grav\Framework\Object
|
|
*
|
|
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
|
|
* @license MIT License; see LICENSE file for details.
|
|
*/
|
|
|
|
namespace Grav\Framework\Object;
|
|
|
|
use Grav\Framework\Collection\ArrayCollection;
|
|
|
|
/**
|
|
* Object Collection
|
|
* @package Grav\Framework\Object
|
|
*/
|
|
class ObjectCollection extends ArrayCollection implements ObjectCollectionInterface
|
|
{
|
|
use ObjectCollectionTrait;
|
|
|
|
static protected $prefix = 'c.';
|
|
|
|
/**
|
|
* @param array $elements
|
|
* @param string $key
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
public function __construct(array $elements = [], $key = null)
|
|
{
|
|
parent::__construct($elements);
|
|
|
|
$this->key = $key !== null ? $key : (string) $this;
|
|
|
|
if ($this->key === null) {
|
|
throw new \InvalidArgumentException('Object cannot be created without assigning a key to it');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $key
|
|
* @return $this
|
|
*/
|
|
public function setKey($key)
|
|
{
|
|
$this->key = $key;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Implements JsonSerializable interface.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function jsonSerialize()
|
|
{
|
|
return ['key' => $this->getKey(), 'objects' => $this->toArray()];
|
|
}
|
|
}
|