Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
DatabaseTableResult | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRowsCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mapping | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Database; |
6 | |
7 | use PeServer\Core\Collection\Arr; |
8 | use PeServer\Core\Database\DatabaseResultBase; |
9 | use PeServer\Core\Serialization\IMapper; |
10 | use PeServer\Core\Serialization\Mapper; |
11 | |
12 | /** |
13 | * 問い合わせ結果。 |
14 | * |
15 | * @template TFieldArray of FieldArrayAlias |
16 | * @immutable |
17 | */ |
18 | class DatabaseTableResult extends DatabaseResultBase |
19 | { |
20 | /** |
21 | * 生成。 |
22 | * |
23 | * @param array<array<string|int,mixed>> $rows レコード一覧。各レコードにフィールド配列が格納されている。 |
24 | * @phpstan-param TFieldArray[] $rows |
25 | */ |
26 | public function __construct( |
27 | array $columns, |
28 | int $resultCount, |
29 | public array $rows |
30 | ) { |
31 | parent::__construct($columns, $resultCount); |
32 | } |
33 | |
34 | #region function |
35 | |
36 | /** |
37 | * レコード件数。 |
38 | * |
39 | * @return int |
40 | * @phpstan-return non-negative-int |
41 | */ |
42 | public function getRowsCount(): int |
43 | { |
44 | return Arr::getCount($this->rows); |
45 | } |
46 | |
47 | /** |
48 | * 結果をマッピング。 |
49 | * |
50 | * @template TObject of object |
51 | * @param string $className |
52 | * @phpstan-param class-string<TObject> $className |
53 | * @param IMapper|null $mapper |
54 | * @return array |
55 | * @phpstan-return TObject[] |
56 | */ |
57 | public function mapping(string $className, IMapper $mapper = null): array |
58 | { |
59 | /** @var TObject[] */ |
60 | $result = []; |
61 | $instanceMapper = $mapper ?? new Mapper(); |
62 | |
63 | foreach ($this->rows as $fields) { |
64 | /** @phpstan-var TObject */ |
65 | $object = $this->mappingImpl($fields, $className, $instanceMapper); |
66 | $result[] = $object; |
67 | } |
68 | |
69 | return $result; |
70 | } |
71 | |
72 | |
73 | #endregion |
74 | } |