Completed
Push — master ( be1dc2...d56e9e )
by Vitaly
06:38
created

TableMetadata::fromClassName()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 28
rs 8.5806
1
<?php declare(strict_types=1);
2
namespace samsonframework\orm;
3
4
/**
5
 * Base table value class.
6
 *
7
 * @author Vitaly Iegorov <[email protected]>
8
 */
9
class TableMetadata
10
{
11
    /** @var string Database table name */
12
    public $tableName;
13
14
    /** @var string Entity class name */
15
    public $className;
16
17
    /** @var array Collection of database table columns */
18
    public $columns = [];
19
20
    /** @var array Collection of database table columns types */
21
    public $columnTypes = [];
22
23
    /** @var array Collection of database table columns aliases to real column names */
24
    public $columnAliases = [];
25
26
    /** @var array Collection of lower case table column aliases to real column names */
27
    public $lowerColumnAliases = [];
28
29
    /** @var string Database table primary field */
30
    public $primaryField;
31
32
    /** @var array Collection of database UNIQUE table columns */
33
    public $uniqueColumns = [];
34
35
    /** @var array Collection of database INDEXED table columns */
36
    public $indexColumns = [];
37
38
    /** @var array Collection of database table columns default values */
39
    public $columnDefaults = [];
40
41
    /** @var array Collection of database table columns is nullable status */
42
    public $columnNullable = [];
43
44
    /**
45
     * Create metadata instance from entity class name
46
     * @param string $className Entity class name
47
     * @deprecated  This is temporary old approach
48
     * @return TableMetadata Metadata instance
49
     *
50
     * @throws \InvalidArgumentException If entity class not found
51
     */
52
    public static function fromClassName(string $className) : TableMetadata
53
    {
54
        $queryClassName = $className . 'Query';
55
        if (class_exists($queryClassName)) {
56
            $metadata = new TableMetadata();
57
            $metadata->primaryField = $queryClassName::$primaryFieldName;
58
            $metadata->className = $queryClassName::$identifier;
59
            $metadata->columnAliases = $queryClassName::$fieldNames;
60
            $metadata->columns = array_values($queryClassName::$fieldNames);
61
            $metadata->tableName = $queryClassName::$tableName;
62
            $metadata->columnTypes = $queryClassName::$fieldDataTypes;
63
            $metadata->columnDefaults = $queryClassName::$fieldDefaults;
64
65
            // Fill in nullables
66
            foreach ($queryClassName::$fieldNullable as $columnName => $nullable) {
67
                $metadata->columnNullable[$columnName] = $nullable === 'YES';
68
            }
69
70
            // Store lower case aliases
71
            foreach ($metadata->columnAliases as $alias => $name) {
72
                $metadata->lowerColumnAliases[strtolower($alias)] = $name;
73
            }
74
75
            return $metadata;
76
        }
77
78
        throw new \InvalidArgumentException('Cannot create metadata for entity ' . $className);
79
    }
80
81
    /**
82
     * Get table column type by column name or alias.
83
     *
84
     * @param string $columnNameOrAlias Table column name or alias
85
     *
86
     * @return string Table column type
87
     * @throws \InvalidArgumentException
88
     */
89
    public function getTableColumnType(string $columnNameOrAlias) : string
90
    {
91
        $columnName = $this->getTableColumnName($columnNameOrAlias);
92
93
        if (array_key_exists($columnName, $this->columnTypes)) {
94
            return $this->columnTypes[$columnName];
95
        }
96
97
        throw new \InvalidArgumentException(
98
            'Column ' . $columnNameOrAlias . ' type is not defined table ' . $this->tableName
99
        );
100
    }
101
102
    /**
103
     * Get table column name by column name or alias.
104
     *
105
     * @param string $columnNameOrAlias Table column name or alias
106
     *
107
     * @return string Table column name
108
     * @throws \InvalidArgumentException
109
     */
110
    public function getTableColumnName(string $columnNameOrAlias) : string
111
    {
112
        // Case insensitive search
113
        $lowerAlias = strtolower($columnNameOrAlias);
114
        if (array_key_exists($lowerAlias, $this->lowerColumnAliases)) {
115
            return $this->lowerColumnAliases[$lowerAlias];
116
        }
117
118
        // Search real column names
119
        if (in_array($columnNameOrAlias, $this->columns, true)) {
120
            return $columnNameOrAlias;
121
        }
122
123
        // Search column aliases
124
        if (array_key_exists($columnNameOrAlias, $this->columnAliases)) {
125
            return $this->columnAliases[$columnNameOrAlias];
126
        }
127
128
        throw new \InvalidArgumentException(
129
            'Column ' . $columnNameOrAlias . ' not found in table ' . $this->tableName
130
        );
131
    }
132
133
    /**
134
     * Get table column index by column name or alias.
135
     *
136
     * @param string $columnNameOrAlias Table column name or alias
137
     *
138
     * @return int Table column index
139
     * @throws \InvalidArgumentException
140
     */
141
    public function getTableColumnIndex(string $columnNameOrAlias) : int
142
    {
143
        return array_search($this->getTableColumnName($columnNameOrAlias), $this->columns, true);
144
    }
145
}
146