Files
kiri-core/Database/Base/CollectionIterator.php
T
2020-09-08 01:21:07 +08:00

74 lines
1.2 KiB
PHP

<?php
namespace Database\Base;
use Database\ActiveQuery;
use Database\ActiveRecord;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class CollectionIterator
* @package Database\Base
*/
class CollectionIterator extends \ArrayIterator
{
/** @var ActiveRecord */
private $model;
/** @var ActiveQuery */
private $query;
/**
* CollectionIterator constructor.
* @param $model
* @param $query
* @param array $array
* @param int $flags
* @throws NotFindClassException
* @throws ReflectionException
*/
public function __construct($model, $query, $array = array(), $flags = 0)
{
$this->model = $model;
if (is_string($model)) {
$this->model = Snowflake::createObject($model);
}
$this->query = $query;
parent::__construct($array, $flags);
}
/**
* @param $current
* @return ActiveRecord
*/
protected function newModel($current)
{
return (clone $this->model)->setAttributes($current);
}
/**
* @return ActiveRecord|mixed
*/
public function current()
{
$current = parent::current();
if (!($current instanceof ActiveRecord)) {
$current = $this->newModel($current);
}
return $this->query->getWith($current);
}
}