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

76 lines
1.1 KiB
PHP
Raw Normal View History

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 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-04-25 17:01:57 +08:00
private ?ActiveRecord $_clone = null;
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-04-28 15:50:21 +08:00
return $this->model::populate($current);
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:53 +08:00
if (is_array($current = parent::current())) {
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
}
}