A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
3.7 KiB

<?php
/**
* Copyright © 2023 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework\Databases\Object;
use Phacil\Framework\Databases\Api\Object\ResultInterface;
use Phacil\Framework\Databases\Object\ResultCacheIterator;
/**
* @since 2.0.0
* @package Phacil\Framework\Databases\Object
*/
class Result extends \ArrayIterator implements ResultInterface {
/**
*
* @var int
*/
public $num_rows;
/**
*
* @var ResultCacheIterator
*/
private $cachedIterator;
/**
*
* @var \Phacil\Framework\Databases\Object\Item[]|\Iterator|null
*/
public $data = null;
public function __construct(array $results = []) {
parent::__construct($results);
//$this->num_rows = $this->count();
return $this;
}
2 years ago
/**
*
* @param string $name
* @return \Phacil\Framework\Databases\Api\Object\ItemInterface[]|\Phacil\Framework\Databases\Api\Object\ItemInterface|null
* @throws \Phacil\Framework\Exception\RuntimeException
2 years ago
*/
public function __get($name) {
switch ($name) {
case 'rows':
return $this;
break;
case 'row':
return $this->offsetGet(0);
break;
default:
throw new \Phacil\Framework\Exception\RuntimeException("Undefined property: $name");
break;
}
}
/**
*
* {@inheritdoc}
*/
public function getData($numRow = false) {
return $numRow ? $this->getRow($numRow) : $this->getRows();
}
2 years ago
/**
* {@inheritdoc}
*/
public function getItems() {
return $this->__toObject();
}
/**
* {@inheritdoc}
*/
public function setRows($rows){
$this->rows = $rows;
$this->row = isset($rows[0]) ? $this->rows[0] : null;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRows(){
return $this->rows;
}
/**
* {@inheritdoc}
*/
public function setRow($row){
$this->row = $row;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRow($numRow = false){
return ($numRow ? ($this->offsetGet($numRow - 1) ? : null) : $this->row);
}
/**
*
* {@inheritdoc}
*/
public function setNumRows($num){
$this->num_rows = $num;
return $this;
}
/**
* {@inheritdoc}
*/
public function getNumRows(){
if(!$this->num_rows)
$this->num_rows = $this->count();
return $this->num_rows;
}
2 years ago
/**
* {@inheritdoc}
*/
public function __toObject() {
return $this;
}
/**
* {@inheritdoc}
*/
public function __toArray() {
return iterator_to_array($this);
$this->cachedIterator = new ResultCacheIterator($this);
foreach ($this->cachedIterator as $val) {
# nothing
}
return $this->cachedIterator->getCache();
2 years ago
}
/**
*
* @param mixed $array
* @return \Phacil\Framework\Databases\Api\Object\ItemInterface[]
2 years ago
*/
protected function loop($array)
{
if($this->data) return $this->data;
$this->data = [];
2 years ago
foreach ($array as $key => $value) {
$this->data[] = $value;
2 years ago
}
return $this->data;
}
/**
* {@inheritdoc}
*/
11 months ago
#[\ReturnTypeWillChange]
public function offsetGet($index)
{
if(!$this->offsetExists($index)) return null;
$data = parent::offsetGet($index);
/** @var \Phacil\Framework\Databases\Api\Object\ItemInterface */
$item = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Api\Object\ItemInterface::class, [$data]);
return $item;
}
/**
* {@inheritdoc}
*/
11 months ago
#[\ReturnTypeWillChange]
public function current()
{
/** @var \Phacil\Framework\Databases\Api\Object\ItemInterface */
$item = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Api\Object\ItemInterface::class, [parent::current()]);
return $item;
}
}