. */ namespace SP\Infrastructure\Database; use SP\Domain\Core\Exceptions\SPException; use function SP\__u; /** * Class QueryResult * * @package SP\Infrastructure\Database */ class QueryResult { private ?array $data = null; private ?string $dataType = 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); if ($this->numRows > 0 && is_object($data[0])) { $this->dataType = get_class($data[0]); } } } public static function withTotalNumRows( array $data, ?int $totalNumRows = null ): QueryResult { $result = new self($data); $result->totalNumRows = (int)$totalNumRows; return $result; } /** * @template T * * @param class-string|null $dataType * * @return T|mixed|null * * @throws SPException */ public function getData(?string $dataType = null): mixed { $this->checkDataType($dataType); return $this->numRows === 1 ? $this->data[0] : null; } /** * @param string|null $dataType * * @return void * @throws SPException */ private function checkDataType(?string $dataType = null): void { if (null !== $dataType && $this->dataType !== null && $dataType !== $this->dataType) { throw new SPException(sprintf(__u('Invalid data\'s type: %s - Current: %s'), $dataType, $this->dataType)); } } /** * @template T * * @param class-string|null $dataType * * @return T[] * @throws SPException */ public function getDataAsArray(?string $dataType = null): array { $this->checkDataType($dataType); 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; } public function getDataType(): ?string { return $this->dataType; } }