2021-10-29 18:05:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Kirby\Cms;
|
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
use Kirby\Toolkit\Str;
|
|
|
|
|
2021-10-29 18:05:46 +02:00
|
|
|
/**
|
|
|
|
* The Item class is the foundation
|
|
|
|
* for every object in context with
|
|
|
|
* other objects. I.e.
|
|
|
|
*
|
|
|
|
* - a Block in a collection of Blocks
|
|
|
|
* - a Layout in a collection of Layouts
|
|
|
|
* - a Column in a collection of Columns
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @package Kirby Cms
|
|
|
|
* @author Bastian Allgeier <bastian@getkirby.com>
|
|
|
|
* @link https://getkirby.com
|
2022-03-22 15:39:39 +01:00
|
|
|
* @copyright Bastian Allgeier
|
2021-10-29 18:05:46 +02:00
|
|
|
* @license https://getkirby.com/license
|
|
|
|
*/
|
|
|
|
class Item
|
|
|
|
{
|
2022-08-31 16:08:03 +02:00
|
|
|
use HasSiblings;
|
|
|
|
|
2022-12-19 16:26:24 +01:00
|
|
|
public const ITEMS_CLASS = Items::class;
|
2022-08-31 16:08:03 +02:00
|
|
|
|
2023-04-14 16:30:28 +02:00
|
|
|
protected Field|null $field;
|
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $params;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Kirby\Cms\Page|\Kirby\Cms\Site|\Kirby\Cms\File|\Kirby\Cms\User
|
|
|
|
*/
|
|
|
|
protected $parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Kirby\Cms\Items
|
|
|
|
*/
|
|
|
|
protected $siblings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new item
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function __construct(array $params = [])
|
|
|
|
{
|
|
|
|
$siblingsClass = static::ITEMS_CLASS;
|
|
|
|
|
|
|
|
$this->id = $params['id'] ?? Str::uuid();
|
|
|
|
$this->params = $params;
|
2023-04-14 16:30:28 +02:00
|
|
|
$this->field = $params['field'] ?? null;
|
2022-08-31 16:08:03 +02:00
|
|
|
$this->parent = $params['parent'] ?? App::instance()->site();
|
|
|
|
$this->siblings = $params['siblings'] ?? new $siblingsClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static Item factory
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return \Kirby\Cms\Item
|
|
|
|
*/
|
|
|
|
public static function factory(array $params)
|
|
|
|
{
|
|
|
|
return new static($params);
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:30:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the parent field if known
|
|
|
|
*/
|
|
|
|
public function field(): Field|null
|
|
|
|
{
|
|
|
|
return $this->field;
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:08:03 +02:00
|
|
|
/**
|
|
|
|
* Returns the unique item id (UUID v4)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function id(): string
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the item to another one
|
|
|
|
*
|
|
|
|
* @param \Kirby\Cms\Item $item
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is(Item $item): bool
|
|
|
|
{
|
|
|
|
return $this->id() === $item->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Kirby instance
|
|
|
|
*
|
|
|
|
* @return \Kirby\Cms\App
|
|
|
|
*/
|
|
|
|
public function kirby()
|
|
|
|
{
|
|
|
|
return $this->parent()->kirby();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parent model
|
|
|
|
*
|
|
|
|
* @return \Kirby\Cms\Page|\Kirby\Cms\Site|\Kirby\Cms\File|\Kirby\Cms\User
|
|
|
|
*/
|
|
|
|
public function parent()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the sibling collection
|
|
|
|
* This is required by the HasSiblings trait
|
|
|
|
*
|
|
|
|
* @return \Kirby\Cms\Items
|
|
|
|
* @psalm-return self::ITEMS_CLASS
|
|
|
|
*/
|
|
|
|
protected function siblingsCollection()
|
|
|
|
{
|
|
|
|
return $this->siblings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the item to an array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id(),
|
|
|
|
];
|
|
|
|
}
|
2021-10-29 18:05:46 +02:00
|
|
|
}
|