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;
|
2020-12-17 14:12:44 +08:00
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
use Exception;
|
|
|
|
|
use JetBrains\PhpStorm\Pure;
|
2020-12-17 14:09:14 +08:00
|
|
|
use ReflectionException;
|
2020-08-31 12:38:32 +08:00
|
|
|
use Snowflake\Abstracts\Component;
|
|
|
|
|
use Database\ActiveRecord;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2020-12-15 14:04:02 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2021-02-22 17:44:24 +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
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
protected ?ActiveRecord $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
|
2020-09-08 01:17:35 +08:00
|
|
|
* @param null $model
|
2021-03-29 17:29:39 +08:00
|
|
|
* @throws Exception
|
2020-08-31 12:38:32 +08:00
|
|
|
*/
|
2021-02-22 19:52:04 +08:00
|
|
|
public function __construct($query, array $array = [], $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 ComponentException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getModel(): ActiveRecord
|
|
|
|
|
{
|
2021-02-24 14:22:56 +08:00
|
|
|
$model = $this->model;;
|
|
|
|
|
if (is_object($model)) {
|
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
return objectPool($model, function () use ($model) {
|
|
|
|
|
return new $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
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 12:38:32 +08:00
|
|
|
/** @var ActiveRecord $model */
|
2020-09-08 10:26:34 +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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|