From 0f62f1f8da8f9e9793dfbb9b13d2a5a31659768a Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Tue, 30 Jan 2024 23:18:02 -0300 Subject: [PATCH] DB result object refactored --- .../Databases/Object/Aux/ComplementResult.php | 8 +++-- .../Object/Aux/ComplementResultLegacy.php | 6 ++-- system/database/Databases/Object/Result.php | 20 ++++++++++++- .../Databases/Object/ResultInterface.php | 4 +-- .../Databases/Object/ResultIterator.php | 30 +++++++++++++++++++ 5 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 system/database/Databases/Object/ResultIterator.php diff --git a/system/database/Databases/Object/Aux/ComplementResult.php b/system/database/Databases/Object/Aux/ComplementResult.php index e0920db..53a6b0f 100644 --- a/system/database/Databases/Object/Aux/ComplementResult.php +++ b/system/database/Databases/Object/Aux/ComplementResult.php @@ -7,8 +7,10 @@ namespace Phacil\Framework\Databases\Object\Aux; -abstract class ComplementResult +abstract class ComplementResult { + protected $customStorage; + /** * {@inheritdoc} * @return int<0, \max> @@ -23,8 +25,8 @@ abstract class ComplementResult */ public function getIterator(): \Traversable { - $this->loop($this->rows); - return $this->data; + $this->customStorage = new \Phacil\Framework\Databases\Object\ResultIterator($this->rows); + return $this->customStorage; //return new \ArrayIterator($this->loop($this->rows)); } } \ No newline at end of file diff --git a/system/database/Databases/Object/Aux/ComplementResultLegacy.php b/system/database/Databases/Object/Aux/ComplementResultLegacy.php index 8aca2ee..68cfa78 100644 --- a/system/database/Databases/Object/Aux/ComplementResultLegacy.php +++ b/system/database/Databases/Object/Aux/ComplementResultLegacy.php @@ -10,6 +10,8 @@ namespace Phacil\Framework\Databases\Object\Aux; abstract class ComplementResultLegacy { + protected $customStorage; + /** * {@inheritdoc} * @return int<0, \max> @@ -24,8 +26,8 @@ abstract class ComplementResultLegacy */ public function getIterator() { - $this->loop($this->rows); - return $this->data; + $this->customStorage = new \Phacil\Framework\Databases\Object\ResultIterator($this->rows); + return $this->customStorage; //return new \ArrayIterator($this->loop($this->rows)); } } \ No newline at end of file diff --git a/system/database/Databases/Object/Result.php b/system/database/Databases/Object/Result.php index 0a61547..dfa5397 100644 --- a/system/database/Databases/Object/Result.php +++ b/system/database/Databases/Object/Result.php @@ -10,6 +10,7 @@ namespace Phacil\Framework\Databases\Object; use Phacil\Framework\Databases\Object\ResultInterface; use SplObjectStorage; +use Traversable; if (version_compare(phpversion(), "7.1.0", ">=")) { class ComplementResult extends \Phacil\Framework\Databases\Object\Aux\ComplementResult @@ -49,6 +50,10 @@ class Result extends ComplementResult implements ResultInterface { */ public $data = null; + public function __construct(array $array = []) { + //return parent::__construct($array, 0, "\Phacil\Framework\Databases\Object\ResultIterator"); + } + /** * * {@inheritdoc} @@ -92,7 +97,9 @@ class Result extends ComplementResult implements ResultInterface { * {@inheritdoc} */ public function getRow($numRow = false){ - return $numRow ? (isset($this->rows[$numRow + 1])?$this->rows[$numRow + 1] : null ) : $this->row; + $obj = new \Phacil\Framework\Databases\Object\Item(); + $this->data->attach($obj->setData($numRow ? (isset($this->rows[$numRow + 1]) ? $this->rows[$numRow + 1] : null) : $this->row)); + return $obj; } /** @@ -136,5 +143,16 @@ class Result extends ComplementResult implements ResultInterface { return $this->data; } + + /** + * {@inheritdoc} + */ + public function offsetGet($index) + { + $data = parent::offsetGet($index); + $item = new \Phacil\Framework\Databases\Object\Item(); + $item->setData($data); + return $item; + } } \ No newline at end of file diff --git a/system/database/Databases/Object/ResultInterface.php b/system/database/Databases/Object/ResultInterface.php index c0bf19a..200d617 100644 --- a/system/database/Databases/Object/ResultInterface.php +++ b/system/database/Databases/Object/ResultInterface.php @@ -39,7 +39,7 @@ interface ResultInterface extends \Countable, \IteratorAggregate { /** * * @param int $numRow - * @return array + * @return \Phacil\Framework\Databases\Object\Item */ public function getRow($numRow = false); @@ -59,7 +59,7 @@ interface ResultInterface extends \Countable, \IteratorAggregate { /** * * @param int $numRow - * @return array + * @return \Phacil\Framework\Databases\Object\Item[]|\Phacil\Framework\Databases\Object\Item */ public function getData($numRow = false); diff --git a/system/database/Databases/Object/ResultIterator.php b/system/database/Databases/Object/ResultIterator.php new file mode 100644 index 0000000..c1ef7fe --- /dev/null +++ b/system/database/Databases/Object/ResultIterator.php @@ -0,0 +1,30 @@ +setData($data); + return $item; + } + + public function current() + { + $item = new \Phacil\Framework\Databases\Object\Item(); + $item->setData(parent::current()); + return $item; + } +} \ No newline at end of file