We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace Smr; |
||
4 | |||
5 | use Exception; |
||
6 | use Generator; |
||
7 | use mysqli_result; |
||
8 | use RuntimeException; |
||
9 | |||
10 | /** |
||
11 | * Holds the result of a Database query (e.g. read or write). |
||
12 | */ |
||
13 | class DatabaseResult { |
||
14 | |||
15 | public function __construct( |
||
16 | private readonly mysqli_result $dbResult |
||
17 | ) {} |
||
18 | |||
19 | /** |
||
20 | * Use to iterate over the records from the result set. |
||
21 | * @return \Generator<DatabaseRecord> |
||
22 | */ |
||
23 | public function records(): Generator { |
||
24 | foreach ($this->dbResult as $dbRecord) { |
||
25 | yield new DatabaseRecord($dbRecord); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Use when exactly one record is expected from the result set. |
||
31 | */ |
||
32 | public function record(): DatabaseRecord { |
||
33 | if ($this->getNumRecords() != 1) { |
||
34 | throw new RuntimeException('One record required, but found ' . $this->getNumRecords()); |
||
35 | } |
||
36 | return new DatabaseRecord($this->dbResult->fetch_assoc()); |
||
37 | } |
||
38 | |||
39 | public function getNumRecords(): int { |
||
40 | $numRows = $this->dbResult->num_rows; |
||
41 | if (is_string($numRows)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
42 | throw new Exception('Number of rows is too large to represent as an int: ' . $numRows); |
||
43 | } |
||
44 | return $numRows; |
||
45 | } |
||
46 | |||
47 | public function hasRecord(): bool { |
||
48 | return $this->getNumRecords() > 0; |
||
49 | } |
||
50 | |||
51 | } |
||
52 |