Files
kiri-core/Database/Base/AbstractCollection.php
T

168 lines
2.7 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/9 0009
* Time: 9:44
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-12-15 14:04:02 +08:00
2020-08-31 12:38:32 +08:00
namespace Database\Base;
2020-09-08 01:13:09 +08:00
use ArrayIterator;
2020-10-29 18:17:25 +08:00
use Database\ActiveQuery;
2021-04-25 17:10:57 +08:00
use Database\ActiveRecord;
2021-02-22 17:44:24 +08:00
use Exception;
use JetBrains\PhpStorm\Pure;
2020-08-31 12:38:32 +08:00
use Snowflake\Abstracts\Component;
2021-04-28 15:50:21 +08:00
use Snowflake\Snowflake;
2020-09-08 01:13:09 +08:00
use Traversable;
2020-08-31 12:38:32 +08:00
/**
* Class AbstractCollection
* @package Database\Base
*/
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess
{
/**
* @var ActiveRecord[]
*/
2020-10-29 18:17:25 +08:00
protected array $_item = [];
2020-08-31 12:38:32 +08:00
2021-03-29 17:47:13 +08:00
protected ActiveRecord|string|null $model;
2020-08-31 12:38:32 +08:00
2020-10-29 18:17:25 +08:00
protected ActiveQuery $query;
2020-09-08 01:09:24 +08:00
2021-02-23 17:30:10 +08:00
public function clean()
{
unset($this->query, $this->model, $this->_item);
}
2020-08-31 12:38:32 +08:00
/**
* Collection constructor.
*
2020-09-08 01:09:24 +08:00
* @param $query
2020-08-31 12:38:32 +08:00
* @param array $array
2021-07-21 13:49:55 +08:00
* @param string|ActiveRecord|null $model
2021-03-29 17:29:39 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2021-07-21 13:49:55 +08:00
public function __construct($query, array $array = [], string|ActiveRecord $model = null)
2020-08-31 12:38:32 +08:00
{
$this->_item = $array;
2020-09-08 01:09:24 +08:00
$this->query = $query;
2020-09-08 01:17:35 +08:00
$this->model = $model;
2020-08-31 12:38:32 +08:00
parent::__construct([]);
}
/**
* @return int
*/
2021-02-22 17:44:24 +08:00
#[Pure] public function getLength(): int
2020-08-31 12:38:32 +08:00
{
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)
{
array_push($this->_item, $item);
}
/**
2020-12-15 14:04:02 +08:00
* @return Traversable|CollectionIterator|ArrayIterator
2021-02-22 17:44:24 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-15 14:04:02 +08:00
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
2020-08-31 12:38:32 +08:00
{
2020-09-08 01:24:09 +08:00
return new CollectionIterator($this->model, $this->query, $this->_item);
2020-08-31 12:38:32 +08:00
}
2021-02-22 17:44:24 +08:00
/**
* @return mixed
* @throws Exception
*/
public function getModel(): ActiveRecord
{
2021-04-25 17:01:57 +08:00
if (!is_object($this->model)) {
2021-07-21 13:49:55 +08:00
$this->model = duplicate($this->model);
2021-04-25 18:09:24 +08:00
$this->model->setIsCreate(false);
2021-02-24 14:22:56 +08:00
}
2021-04-25 17:10:57 +08:00
return $this->model;
2021-02-22 17:44:24 +08:00
}
2020-08-31 12:38:32 +08:00
/**
* @param mixed $offset
* @return bool
*/
2020-12-15 14:04:02 +08:00
public function offsetExists(mixed $offset): bool
2020-08-31 12:38:32 +08:00
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
2020-12-15 14:04:02 +08:00
* @return ActiveRecord|null
2021-04-25 17:10:57 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-15 14:04:02 +08:00
public function offsetGet(mixed $offset): ?ActiveRecord
2020-08-31 12:38:32 +08:00
{
if (!$this->offsetExists($offset)) {
return NULL;
}
2020-09-08 10:26:34 +08:00
if ($this->_item[$offset] instanceof ActiveRecord) {
return $this->_item[$offset];
}
2021-04-28 15:50:21 +08:00
return $this->model::populate($this->_item[$offset]);
2020-08-31 12:38:32 +08:00
}
/**
* @param mixed $offset
* @param mixed $value
*/
2020-12-15 14:04:02 +08:00
public function offsetSet(mixed $offset, mixed $value)
2020-08-31 12:38:32 +08:00
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
2020-12-15 14:04:02 +08:00
public function offsetUnset(mixed $offset)
2020-08-31 12:38:32 +08:00
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
}