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