forked from Public/pics
AssetIterator: rewrite to standard Iterator interface
This commit is contained in:
@@ -1,42 +1,50 @@
|
||||
<?php
|
||||
/*****************************************************************************
|
||||
* AssetIterator.php
|
||||
* Contains key class AssetIterator.
|
||||
* Contains model class AssetIterator.
|
||||
*
|
||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||
* Kabuki CMS (C) 2013-2025, Aaron van Geffen
|
||||
*****************************************************************************/
|
||||
|
||||
class AssetIterator extends Asset
|
||||
class AssetIterator implements Iterator
|
||||
{
|
||||
private Database $db;
|
||||
private $direction;
|
||||
|
||||
private $return_format;
|
||||
private $res_assets;
|
||||
private $res_meta;
|
||||
private $res_thumbs;
|
||||
private $rowCount;
|
||||
|
||||
protected function __construct($res_assets, $res_meta, $res_thumbs, $return_format, $direction)
|
||||
private $assets_iterator;
|
||||
private $meta_iterator;
|
||||
private $thumbs_iterator;
|
||||
|
||||
protected function __construct(PDOStatement $stmt_assets, PDOStatement $stmt_meta, PDOStatement $stmt_thumbs,
|
||||
$return_format, $direction)
|
||||
{
|
||||
$this->db = Registry::get('db');
|
||||
$this->direction = $direction;
|
||||
$this->res_assets = $res_assets;
|
||||
$this->res_meta = $res_meta;
|
||||
$this->res_thumbs = $res_thumbs;
|
||||
$this->return_format = $return_format;
|
||||
$this->rowCount = $stmt_assets->rowCount();
|
||||
|
||||
$this->assets_iterator = new CachedPDOIterator($stmt_assets);
|
||||
$this->assets_iterator->rewind();
|
||||
|
||||
$this->meta_iterator = new CachedPDOIterator($stmt_meta);
|
||||
$this->thumbs_iterator = new CachedPDOIterator($stmt_thumbs);
|
||||
}
|
||||
|
||||
public function next()
|
||||
public static function all()
|
||||
{
|
||||
$row = $this->db->fetchAssoc($this->res_assets);
|
||||
return self::getByOptions();
|
||||
}
|
||||
|
||||
// No more rows?
|
||||
public function current(): mixed
|
||||
{
|
||||
$row = $this->assets_iterator->current();
|
||||
if (!$row)
|
||||
return false;
|
||||
return $row;
|
||||
|
||||
// Looks up metadata.
|
||||
// Collect metadata
|
||||
$row['meta'] = [];
|
||||
while ($meta = $this->db->fetchAssoc($this->res_meta))
|
||||
$this->meta_iterator->rewind();
|
||||
foreach ($this->meta_iterator as $meta)
|
||||
{
|
||||
if ($meta['id_asset'] != $row['id_asset'])
|
||||
continue;
|
||||
@@ -44,54 +52,23 @@ class AssetIterator extends Asset
|
||||
$row['meta'][$meta['variable']] = $meta['value'];
|
||||
}
|
||||
|
||||
// Reset internal pointer for next asset.
|
||||
$this->db->data_seek($this->res_meta, 0);
|
||||
|
||||
// Looks up thumbnails.
|
||||
// Collect thumbnails
|
||||
$row['thumbnails'] = [];
|
||||
while ($thumbs = $this->db->fetchAssoc($this->res_thumbs))
|
||||
$this->thumbs_iterator->rewind();
|
||||
foreach ($this->thumbs_iterator as $thumb)
|
||||
{
|
||||
if ($thumbs['id_asset'] != $row['id_asset'])
|
||||
if ($thumb['id_asset'] != $row['id_asset'])
|
||||
continue;
|
||||
|
||||
$row['thumbnails'][$thumbs['selector']] = $thumbs['filename'];
|
||||
$row['thumbnails'][$thumb['selector']] = $thumb['filename'];
|
||||
}
|
||||
|
||||
// Reset internal pointer for next asset.
|
||||
$this->db->data_seek($this->res_thumbs, 0);
|
||||
|
||||
if ($this->return_format === 'object')
|
||||
return new Asset($row);
|
||||
else
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->db->data_seek($this->res_assets, 0);
|
||||
$this->db->data_seek($this->res_meta, 0);
|
||||
$this->db->data_seek($this->res_thumbs, 0);
|
||||
}
|
||||
|
||||
public function clean()
|
||||
{
|
||||
if (!$this->res_assets)
|
||||
return;
|
||||
|
||||
$this->db->free($this->res_assets);
|
||||
$this->res_assets = null;
|
||||
}
|
||||
|
||||
public function num()
|
||||
{
|
||||
return $this->db->rowCount($this->res_assets);
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
return self::getByOptions();
|
||||
}
|
||||
|
||||
public static function getByOptions(array $options = [], $return_count = false, $return_format = 'object')
|
||||
{
|
||||
$params = [
|
||||
@@ -210,13 +187,38 @@ class AssetIterator extends Asset
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
public function isAscending()
|
||||
public function key(): mixed
|
||||
{
|
||||
return $this->assets_iterator->key();
|
||||
}
|
||||
|
||||
public function isAscending(): bool
|
||||
{
|
||||
return $this->direction === 'asc';
|
||||
}
|
||||
|
||||
public function isDescending()
|
||||
public function isDescending(): bool
|
||||
{
|
||||
return $this->direction === 'desc';
|
||||
}
|
||||
|
||||
public function next(): void
|
||||
{
|
||||
$this->assets_iterator->next();
|
||||
}
|
||||
|
||||
public function num(): int
|
||||
{
|
||||
return $this->rowCount;
|
||||
}
|
||||
|
||||
public function rewind(): void
|
||||
{
|
||||
$this->assets_iterator->rewind();
|
||||
}
|
||||
|
||||
public function valid(): bool
|
||||
{
|
||||
return $this->assets_iterator->valid();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user