2020-09-08 00:49:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2021-02-22 19:52:04 +08:00
|
|
|
|
2020-09-08 00:49:15 +08:00
|
|
|
namespace Database\Base;
|
|
|
|
|
|
|
|
|
|
|
2020-09-08 01:21:07 +08:00
|
|
|
use Database\ActiveQuery;
|
2020-09-08 00:49:15 +08:00
|
|
|
use Database\ActiveRecord;
|
2020-12-15 14:04:02 +08:00
|
|
|
use Exception;
|
2020-09-08 01:13:09 +08:00
|
|
|
use ReflectionException;
|
2020-09-08 00:57:42 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
|
|
|
|
use Snowflake\Snowflake;
|
2020-09-08 00:49:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class CollectionIterator
|
|
|
|
|
* @package Database\Base
|
|
|
|
|
*/
|
|
|
|
|
class CollectionIterator extends \ArrayIterator
|
|
|
|
|
{
|
|
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
private ActiveRecord $model;
|
2020-09-08 00:49:15 +08:00
|
|
|
|
|
|
|
|
|
2020-09-08 01:21:07 +08:00
|
|
|
/** @var ActiveQuery */
|
2020-10-29 18:17:25 +08:00
|
|
|
private ActiveQuery $query;
|
2020-09-08 01:21:07 +08:00
|
|
|
|
|
|
|
|
|
2021-02-23 17:30:10 +08:00
|
|
|
public function clean()
|
|
|
|
|
{
|
|
|
|
|
unset($this->query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-08 00:57:42 +08:00
|
|
|
/**
|
|
|
|
|
* CollectionIterator constructor.
|
|
|
|
|
* @param $model
|
2020-09-08 01:21:07 +08:00
|
|
|
* @param $query
|
2020-09-08 00:57:42 +08:00
|
|
|
* @param array $array
|
|
|
|
|
* @param int $flags
|
2021-02-22 17:44:24 +08:00
|
|
|
* @throws Exception
|
2020-09-08 00:57:42 +08:00
|
|
|
*/
|
2021-02-22 19:52:04 +08:00
|
|
|
public function __construct($model, $query, $array = array(), $flags = 0)
|
2020-09-08 00:49:15 +08:00
|
|
|
{
|
|
|
|
|
$this->model = $model;
|
2020-09-08 01:21:07 +08:00
|
|
|
$this->query = $query;
|
2020-09-08 00:49:15 +08:00
|
|
|
parent::__construct($array, $flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-08 01:13:09 +08:00
|
|
|
/**
|
|
|
|
|
* @param $current
|
|
|
|
|
* @return ActiveRecord
|
2020-12-15 14:04:02 +08:00
|
|
|
* @throws Exception
|
2020-09-08 01:13:09 +08:00
|
|
|
*/
|
2020-12-15 14:04:02 +08:00
|
|
|
protected function newModel($current): ActiveRecord
|
2020-09-08 01:13:09 +08:00
|
|
|
{
|
2021-02-24 14:22:56 +08:00
|
|
|
$model = $this->model;
|
|
|
|
|
if (is_object($model)) {
|
|
|
|
|
return $model->setAttributes($current);
|
|
|
|
|
}
|
|
|
|
|
return objectPool($model, function () use ($model) {
|
|
|
|
|
return new $model();
|
|
|
|
|
})->setAttributes($current);
|
2020-09-08 01:21:07 +08:00
|
|
|
|
2020-09-08 01:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-08 00:49:15 +08:00
|
|
|
/**
|
2020-12-15 14:04:02 +08:00
|
|
|
* @throws Exception
|
2020-09-08 00:49:15 +08:00
|
|
|
*/
|
2021-03-05 18:34:31 +08:00
|
|
|
public function current(): ActiveRecord
|
2020-09-08 00:49:15 +08:00
|
|
|
{
|
2021-03-05 18:34:31 +08:00
|
|
|
if (!(($current = parent::current()) instanceof ActiveRecord)) {
|
2020-09-08 01:21:07 +08:00
|
|
|
$current = $this->newModel($current);
|
2020-09-08 01:13:09 +08:00
|
|
|
}
|
2020-09-08 01:21:07 +08:00
|
|
|
return $this->query->getWith($current);
|
2020-09-08 00:49:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|