. */ namespace SP\Storage\Database; /** * Class QueryResult * * @package SP\Storage\Database */ final class QueryResult { /** * @var array */ private $data; /** * @var int */ private $numRows = 0; /** * @var int */ private $totalNumRows; /** * @var int */ private $affectedNumRows; /** * @var int */ private $statusCode; /** * @var int */ private $lastId = 0; /** * QueryResult constructor. * * @param array|null $data */ public function __construct(?array $data = null) { if ($data !== null) { $this->data = $data; $this->numRows = count($data); } } /** * @param array $data * @param null $totalNumRows * * @return QueryResult */ public static function fromResults(array $data, $totalNumRows = null): QueryResult { $result = new self($data); if ($totalNumRows !== null) { $result->totalNumRows = $totalNumRows; } return $result; } /** * @return mixed|null */ public function getData() { if ($this->numRows === 1) { return $this->data[0]; } return null; } /** * Always returns an array * * @return array */ public function getDataAsArray(): array { return (array)$this->data; } /** * @return int */ public function getNumRows(): int { return $this->numRows; } /** * @return int */ public function getTotalNumRows(): int { return $this->totalNumRows; } /** * @param int $totalNumRows * * @return QueryResult */ public function setTotalNumRows(int $totalNumRows): QueryResult { $this->totalNumRows = $totalNumRows; return $this; } /** * @return int */ public function getStatusCode(): int { return $this->statusCode; } /** * @param int $statusCode * * @return QueryResult */ public function setStatusCode(int $statusCode): QueryResult { $this->statusCode = $statusCode; return $this; } /** * @return int */ public function getAffectedNumRows(): int { return $this->affectedNumRows; } /** * @param int $affectedNumRows * * @return QueryResult */ public function setAffectedNumRows(int $affectedNumRows): QueryResult { $this->affectedNumRows = $affectedNumRows; return $this; } /** * @return int */ public function getLastId(): int { return $this->lastId; } /** * @param int $lastId * * @return QueryResult */ public function setLastId(int $lastId): QueryResult { $this->lastId = $lastId; return $this; } }