From 0d67be08fdeee2dd8741bfda0c99c6c98bbf0c00 Mon Sep 17 00:00:00 2001 From: Gerrit Addiks Date: Mon, 25 Jul 2016 09:44:39 +0200 Subject: [PATCH] actually added the aliased-iterator... --- .../Iterators/AliasedResourceIterator.php | 237 ++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 src/Addiks/PHPSQL/Iterators/AliasedResourceIterator.php diff --git a/src/Addiks/PHPSQL/Iterators/AliasedResourceIterator.php b/src/Addiks/PHPSQL/Iterators/AliasedResourceIterator.php new file mode 100644 index 0000000..34853c2 --- /dev/null +++ b/src/Addiks/PHPSQL/Iterators/AliasedResourceIterator.php @@ -0,0 +1,237 @@ + or send me a mail so i can send you a copy. + * @license GPL-3.0 + * @author Gerrit Addiks + */ + +namespace Addiks\PHPSQL\Iterators; + +use Iterator; +use Addiks\PHPSQL\Iterators\DataProviderInterface; +use Addiks\PHPSQL\Iterators\UsesBinaryDataInterface; +use IteratorAggregate; + +class AliasedResourceIterator implements Iterator, DataProviderInterface, UsesBinaryDataInterface +{ + + public function __construct( + DataProviderInterface $dataProvider, + $alias + ) { + $this->dataProvider = $dataProvider; + $this->alias = $alias; + } + + /** + * @var DataProviderInterface + */ + protected $dataProvider; + + /** + * @var string + */ + protected $alias; + + /** + * + * @return TableSchema + */ + public function getTableSchema() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->getTableSchema(); + } + + public function doesRowExists($rowId = null) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->doesRowExists($rowId); + } + + public function getRowData($rowId = null) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->getRowData($rowId); + } + + public function getCellData($rowId, $columnId) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->getCellData($rowId, $columnId); + } + + public function tell() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->tell(); + } + + public function seek($rowId) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->seek($rowId); + } + + public function count() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + return $dataProvider->count(); + } + + public function usesBinaryData() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + /* @var $usesBinaryData boolean */ + $usesBinaryData = false; + + if ($dataProvider instanceof UsesBinaryDataInterface) { + $usesBinaryData = $dataProvider->usesBinaryData(); + } + + return $usesBinaryData; + } + + public function convertDataRowToStringRow(array $row) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + /* @var $convertedRow array */ + $convertedRow = $row; + + #if ($dataProvider instanceof UsesBinaryDataInterface && $dataProvider->usesBinaryData()) { + # $convertedRow = $dataProvider->convertDataRowToStringRow($row); + #} + + return $convertedRow; + } + + public function convertStringRowToDataRow(array $row) + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + /* @var $convertedRow array */ + $convertedRow = $row; + + if ($dataProvider instanceof UsesBinaryDataInterface && $dataProvider->usesBinaryData()) { + $convertedRow = $dataProvider->convertStringRowToDataRow($row); + } + + return $convertedRow; + } + + public function rewind() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + if ($dataProvider instanceof IteratorAggregate) { + $dataProvider = $dataProvider->getIterator(); + } + + if ($dataProvider instanceof Iterator) { + $dataProvider->rewind(); + } + } + + public function valid() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + if ($dataProvider instanceof IteratorAggregate) { + $dataProvider = $dataProvider->getIterator(); + } + + if ($dataProvider instanceof Iterator) { + return $dataProvider->valid(); + } + } + + public function key() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + if ($dataProvider instanceof IteratorAggregate) { + $dataProvider = $dataProvider->getIterator(); + } + + if ($dataProvider instanceof Iterator) { + return $dataProvider->key(); + } + } + + public function current() + { + /* @var $row array */ + $row = null; + + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + /* @var $alias string */ + $alias = $this->alias; + + /* @var $iterator Iterator */ + $iterator = $dataProvider; + + if ($iterator instanceof IteratorAggregate) { + $iterator = $iterator->getIterator(); + } + + if ($dataProvider instanceof Iterator) { + $row = $dataProvider->current(); + + if ($dataProvider instanceof UsesBinaryDataInterface && $dataProvider->usesBinaryData()) { + $row = $dataProvider->convertDataRowToStringRow($row); + } + + /* @var $newRow array */ + $newRow = $row; + + foreach ($row as $key => $value) { + $newRow["{$alias}.{$key}"] = $value; + } + + $row = $newRow; + } + + return $row; + } + + public function next() + { + /* @var $dataProvider DataProviderInterface */ + $dataProvider = $this->dataProvider; + + if ($dataProvider instanceof IteratorAggregate) { + $dataProvider = $dataProvider->getIterator(); + } + + if ($dataProvider instanceof Iterator) { + $dataProvider->next(); + } + } +}