Files
kiri-databases/Base/CollectionIterator.php
T

63 lines
1.1 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
declare(strict_types=1);
namespace Database\Base;
use Database\ModelInterface;
/**
* Class CollectionIterator
* @package Database\Base
*/
class CollectionIterator extends \ArrayIterator
{
2023-12-18 18:11:58 +08:00
/**
* @var ModelInterface|string
*/
2023-12-12 15:35:35 +08:00
private ModelInterface|string $model;
/**
* CollectionIterator constructor.
* @param $model
* @param array $array
* @param int $flags
* @throws
*/
public function __construct($model, array $array = [], int $flags = 0)
{
$this->model = $model;
parent::__construct($array, $flags);
}
/**
* @param $current
* @return ModelInterface
* @throws
*/
protected function newModel($current): ModelInterface
{
return $this->model->populates($current);
}
/**
* @throws
*/
public function current(): ModelInterface
{
if (is_array($current = parent::current())) {
$current = $this->newModel($current);
}
return $current;
}
2022-01-09 03:49:51 +08:00
}