Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DatabaseColumn
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Database;
6
7use PDO;
8use PeServer\Core\Collection\Arr;
9use PeServer\Core\Text;
10
11/**
12 * カラム情報。
13 *
14 * @see https://www.php.net/manual/pdostatement.getcolumnmeta.php
15 */
16readonly class DatabaseColumn
17{
18    /**
19     * 生成。
20     *
21     * @param string $name カラム名(name)。
22     * @param int $length カラム長(len)。
23     * @param int $precision 数値精度(precision)。
24     * @param string $table テーブル名(table)。
25     * @param string $nativeType PHP型(native_type)。
26     * @param string $driverType SQL型(driver:decl_type)。
27     * @param -1|PDO::PARAM_* $pdoType PDO型(pdo_type)。
28     * @param array<mixed> $flags (flags)。
29     */
30    public function __construct(
31        public string $name,
32        public int $length,
33        public int $precision,
34        public string $table,
35        public string $nativeType,
36        public string $driverType,
37        public int $pdoType,
38        public array $flags
39    ) {
40    }
41
42    #region function
43
44    /**
45     * `PDOStatement::getColumnMeta` で取得した配列から `DatabaseColumn` の生成
46     *
47     * @param array<string,mixed> $meta
48     * @return self
49     */
50    public static function create(array $meta): self
51    {
52        return new DatabaseColumn(
53            $meta['name'] ?? Text::EMPTY,
54            $meta['len'] ?? -1,
55            $meta['precision'] ?? 0,
56            $meta['table'] ?? Text::EMPTY,
57            $meta['native_type'] ?? Text::EMPTY,
58            $meta['driver:decl_type'] ?? Text::EMPTY,
59            $meta['pdo_type'] ?? -1,
60            $meta['flags'] ?? []
61        );
62    }
63
64    #endregion
65}