Files
kiri-databases/src/Base/AbstractCollection.php
T

163 lines
3.1 KiB
PHP
Raw Normal View History

2021-08-11 15:03:28 +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;
2021-09-28 18:54:07 +08:00
use Database\ModelInterface;
2021-08-28 02:14:10 +08:00
use Kiri\ToArray;
2021-08-11 15:03:28 +08:00
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
2021-12-17 04:28:23 +08:00
use ReturnTypeWillChange;
2021-08-11 15:03:28 +08:00
use Traversable;
/**
* Class AbstractCollection
* @package Database\Base
*/
2021-08-28 02:14:10 +08:00
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray
2021-08-11 15:03:28 +08:00
{
/**
2021-09-28 18:54:07 +08:00
* @var ModelInterface[]
2021-08-11 15:03:28 +08:00
*/
protected array $_item = [];
2021-09-28 18:54:07 +08:00
protected ModelInterface|string|null $model;
2021-08-11 15:03:28 +08:00
protected ActiveQuery $query;
public function clean()
{
unset($this->query, $this->model, $this->_item);
}
2021-09-28 18:54:07 +08:00
/**
* Collection constructor.
*
* @param $query
* @param array $array
* @param ModelInterface|null $model
* @throws Exception
*/
public function __construct($query, array $array = [], ModelInterface $model = null)
2021-08-11 15:03:28 +08:00
{
$this->_item = $array;
$this->query = $query;
2021-09-28 18:54:07 +08:00
$this->model = $model;
2021-08-11 15:03:28 +08:00
parent::__construct([]);
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @param $item
*/
public function setItems($item)
{
$this->_item = $item;
}
/**
* @param $model
*/
public function setModel($model)
{
$this->model = $model;
}
/**
* @param $item
*/
public function addItem($item)
{
2021-12-17 04:30:21 +08:00
$this->_item[] = $item;
2021-08-11 15:03:28 +08:00
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws Exception
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->query, $this->_item);
}
/**
* @return mixed
* @throws Exception
*/
2021-09-28 18:54:07 +08:00
public function getModel(): ModelInterface
2021-08-11 15:03:28 +08:00
{
return $this->model;
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
2021-09-28 18:54:07 +08:00
* @return ModelInterface|null
2021-08-11 15:03:28 +08:00
* @throws Exception
*/
2021-09-28 18:54:07 +08:00
public function offsetGet(mixed $offset): ?ModelInterface
2021-08-11 15:03:28 +08:00
{
if (!$this->offsetExists($offset)) {
return NULL;
}
2021-09-28 18:54:07 +08:00
if (!($this->_item[$offset] instanceof ModelInterface)) {
2021-11-11 03:45:09 +08:00
return $this->model->populates($this->_item[$offset]);
2021-08-11 15:03:28 +08:00
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
2021-12-17 04:28:23 +08:00
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
2021-08-11 15:03:28 +08:00
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
2021-12-17 04:28:23 +08:00
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
2021-08-11 15:03:28 +08:00
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
}