Files
database/src/Database/ResultSet.php

66 lines
1.5 KiB
PHP
Raw Normal View History

2022-09-08 20:02:05 -04:00
<?php
namespace ProVM\Database;
use PDO;
use PDOStatement;
use ProVM\Concept\Database\ResultSet as RSInterface;
2022-12-22 22:24:55 -03:00
use ProVM\Exception\BlankResult;
2022-09-08 20:02:05 -04:00
class ResultSet implements RSInterface
{
public function __construct(PDOStatement $statement)
{
$this->setStatement($statement);
}
protected PDOStatement $statement;
public function setStatement(PDOStatement $statement): RSInterface
{
$this->statement = $statement;
return $this;
}
public function getStatement(): PDOStatement
{
return $this->statement;
}
public function execute(array $values): RSInterface
{
$this->getStatement()->execute($values);
return $this;
}
public function getAsArray(): array
{
$rs = $this->getStatement()->fetchAll(PDO::FETCH_ASSOC);
if (!$rs) {
2022-12-22 22:24:55 -03:00
throw new BlankResult();
}
return $rs;
2022-09-08 20:02:05 -04:00
}
public function getAsObject(): array
{
$rs = $this->getStatement()->fetchAll(PDO::FETCH_OBJ);
if (!$rs) {
2022-12-22 22:24:55 -03:00
throw new BlankResult();
}
return $rs;
2022-09-08 20:02:05 -04:00
}
public function getFirstAsArray(): array
{
$rs = $this->getStatement()->fetch(PDO::FETCH_ASSOC);
if (!$rs or count($rs) === 0) {
2022-12-22 22:24:55 -03:00
throw new BlankResult();
}
return $rs;
}
public function getFirstAsObject(): object
2022-09-08 20:02:05 -04:00
{
$rs = $this->getStatement()->fetch(PDO::FETCH_OBJ);
if (!$rs or count($rs) === 0) {
2022-12-22 22:24:55 -03:00
throw new BlankResult();
}
return $rs;
2022-09-08 20:02:05 -04:00
}
}