Files
kiri-databases/Base/AbstractCollection.php
T

168 lines
3.3 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/9 0009
* Time: 9:44
*/
declare(strict_types=1);
namespace Database\Base;
use ArrayIterator;
use Database\ActiveQuery;
use Database\ModelInterface;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
2022-06-30 16:40:28 +08:00
use ReturnTypeWillChange;
2022-01-09 03:49:51 +08:00
use Traversable;
/**
* Class AbstractCollection
* @package Database\Base
*/
2023-11-13 17:52:38 +08:00
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable
2022-01-09 03:49:51 +08:00
{
2023-12-12 15:35:35 +08:00
/**
* @var ModelInterface[]
*/
2023-12-13 14:47:23 +08:00
private array $_item;
2023-12-12 15:35:35 +08:00
/**
2023-12-13 14:47:23 +08:00
* @return array
2023-12-12 15:35:35 +08:00
*/
2023-12-13 14:47:23 +08:00
public function getItems(): array
2023-12-12 15:35:35 +08:00
{
2023-12-13 14:47:23 +08:00
// TODO: Change the autogenerated stub
return $this->_item;
2023-12-12 15:35:35 +08:00
}
/**
* Collection constructor.
*
2023-12-13 14:47:23 +08:00
* @param ActiveQuery $query
2023-12-12 15:35:35 +08:00
* @param array $array
* @param ModelInterface|null $model
*/
2023-12-13 14:47:23 +08:00
public function __construct(public ActiveQuery $query, public ?ModelInterface $model = null, array $array = [])
2023-12-12 15:35:35 +08:00
{
$this->_item = $array;
parent::__construct();
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @param $item
*/
public function setItems($item): void
{
$this->_item = $item;
}
/**
* @param $model
*/
public function setModel($model): void
{
$this->model = $model;
}
/**
* @param $item
*/
public function addItem($item): void
{
$this->_item[] = $item;
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->_item);
}
/**
* @return mixed
* @throws
*/
public function getModel(): ModelInterface
{
return $this->model;
}
/**
* @return ActiveQuery
*/
public function makeNewQuery(): ActiveQuery
{
return $this->model::query();
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset): void
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
2022-01-09 03:49:51 +08:00
}