julienmonnerie/kirby/src/Cms/Items.php

101 lines
2.1 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cms;
use Closure;
2025-04-21 18:57:21 +02:00
use Kirby\Content\Field;
use Kirby\Exception\InvalidArgumentException;
2022-06-17 17:51:59 +02:00
/**
* A collection of items
* @since 3.5.0
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Items extends Collection
{
2022-12-19 14:56:05 +01:00
public const ITEM_CLASS = Item::class;
2022-06-17 17:51:59 +02:00
2023-04-14 16:34:06 +02:00
protected Field|null $field;
2023-06-01 16:54:20 +02:00
/**
* All registered items methods
*/
2025-04-21 18:57:21 +02:00
public static array $methods = [];
2023-06-01 16:54:20 +02:00
2025-04-21 18:57:21 +02:00
protected array $options;
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* @var \Kirby\Cms\ModelWithContent
*/
protected $parent;
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
public function __construct($objects = [], array $options = [])
{
$this->options = $options;
$this->parent = $options['parent'] ?? App::instance()->site();
2023-04-14 16:34:06 +02:00
$this->field = $options['field'] ?? null;
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
parent::__construct($objects, $this->parent);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
/**
* Creates a new item collection from a
* an array of item props
*/
2025-04-21 18:57:21 +02:00
public static function factory(
array|null $items = null,
array $params = []
): static {
2022-08-31 15:02:43 +02:00
if (empty($items) === true || is_array($items) === false) {
return new static();
}
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
if (is_array($params) === false) {
throw new InvalidArgumentException('Invalid item options');
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// create a new collection of blocks
2025-04-21 18:57:21 +02:00
$collection = new static([], $params);
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
foreach ($items as $item) {
if (is_array($item) === false) {
throw new InvalidArgumentException('Invalid data for ' . static::ITEM_CLASS);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
// inject properties from the parent
$item['field'] = $collection->field();
$item['options'] = $params['options'] ?? [];
$item['parent'] = $collection->parent();
$item['siblings'] = $collection;
$item['params'] = $item;
2022-08-31 15:02:43 +02:00
$class = static::ITEM_CLASS;
2025-04-21 18:57:21 +02:00
$item = $class::factory($item);
2022-08-31 15:02:43 +02:00
$collection->append($item->id(), $item);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $collection;
}
2022-06-17 17:51:59 +02:00
2023-04-14 16:34:06 +02:00
/**
* Returns the parent field if known
*/
public function field(): Field|null
{
return $this->field;
}
2022-08-31 15:02:43 +02:00
/**
* Convert the items to an array
*/
2025-04-21 18:57:21 +02:00
public function toArray(Closure|null $map = null): array
2022-08-31 15:02:43 +02:00
{
return array_values(parent::toArray($map));
}
2022-06-17 17:51:59 +02:00
}