Completed
Push — master ( 2be816...227471 )
by
unknown
05:40
created

TableMetadata::getColumnDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
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 View Code Duplication
    public function getTableColumnType(string $columnNameOrAlias) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 default value by column name or alias.
104
     *
105
     * @param string $columnNameOrAlias Table column name or alias
106
     *
107
     * @return mixed Table column default value
108
     * @throws \InvalidArgumentException
109
     */
110 View Code Duplication
    public function getColumnDefaultValue(string $columnNameOrAlias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $columnName = $this->getTableColumnName($columnNameOrAlias);
113
114
        if (array_key_exists($columnName, $this->columnDefaults)) {
115
            return $this->columnDefaults[$columnName];
116
        }
117
118
        throw new \InvalidArgumentException(
119
            'Column ' . $columnNameOrAlias . ' type is not defined table ' . $this->tableName
120
        );
121
    }
122
123
    /**
124
     * Get table column name by column name or alias.
125
     *
126
     * @param string $columnNameOrAlias Table column name or alias
127
     *
128
     * @return string Table column name
129
     * @throws \InvalidArgumentException
130
     */
131 View Code Duplication
    public function getTableColumnName(string $columnNameOrAlias) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        // Case insensitive search
134
        if ($this->isColumnAliasExists($columnNameOrAlias)) {
135
            return $this->lowerColumnAliases[strtolower($columnNameOrAlias)];
136
        }
137
138
        // Search real column names
139
        if ($this->isColumnNameExists($columnNameOrAlias)) {
140
            return $columnNameOrAlias;
141
        }
142
143
        throw new \InvalidArgumentException(
144
            'Column ' . $columnNameOrAlias . ' not found in table ' . $this->tableName
145
        );
146
    }
147
148
    /**
149
     * Get table column alias by column name or alias.
150
     *
151
     * @param string $columnNameOrAlias Table column name or alias
152
     *
153
     * @return string Table column alias
154
     * @throws \InvalidArgumentException
155
     */
156 View Code Duplication
    public function getTableColumnAlias(string $columnNameOrAlias) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        // Case insensitive search
159
        if ($this->isColumnAliasExists($columnNameOrAlias)) {
160
            return $columnNameOrAlias;
161
        }
162
163
        // Search real column names
164
        if ($this->isColumnNameExists($columnNameOrAlias)) {
165
            return array_flip($this->columnAliases)[$columnNameOrAlias];
166
        }
167
168
        throw new \InvalidArgumentException(
169
            'Column ' . $columnNameOrAlias . ' not found in table ' . $this->tableName
170
        );
171
    }
172
173
    /**
174
     * Get table primary field name.
175
     *
176
     * @return string Table primary field name
177
     * @throws \InvalidArgumentException
178
     */
179
    public function getTablePrimaryField(): string
180
    {
181
        return $this->getTableColumnName($this->primaryField);
182
    }
183
184
    /**
185
     * Get table column index by column name or alias.
186
     *
187
     * @param string $columnNameOrAlias Table column name or alias
188
     *
189
     * @return int Table column index
190
     * @throws \InvalidArgumentException
191
     */
192
    public function getTableColumnIndex(string $columnNameOrAlias) : int
193
    {
194
        return array_search($this->getTableColumnName($columnNameOrAlias), $this->columns, true);
195
    }
196
197
    /**
198
     * Define if passed column name or alias exists.
199
     *
200
     * @param string $columnNameOrAlias Table column name or alias
201
     * @return bool True if passed column name or alias exists
202
     */
203
    public function isColumnExists(string $columnNameOrAlias): bool
204
    {
205
        return $this->isColumnAliasExists($columnNameOrAlias) || $this->isColumnNameExists($columnNameOrAlias);
206
    }
207
208
    /**
209
     * Is column alias exists using case insensitive search.
210
     *
211
     * @param string $columnAlias Column name alias
212
     * @return bool True if column alias exists otherwise false
213
     */
214
    protected function isColumnAliasExists(string $columnAlias): bool
215
    {
216
//        return array_key_exists($columnAlias, $this->columnAliases);
217
        return array_key_exists(strtolower($columnAlias), $this->lowerColumnAliases);
218
    }
219
220
    /**
221
     * Is column name exists.
222
     *
223
     * @param string $columnName Column name
224
     * @return bool True if column name exists otherwise false
225
     */
226
    protected function isColumnNameExists(string $columnName): bool
227
    {
228
        return in_array($columnName, $this->columns, true);
229
    }
230
231
    /**
232
     * Is column nullable.
233
     *
234
     * @param string $columnNameOrAlias Column name or alias
235
     * @return bool True if column is nullable otherwise false
236
     * @throws \InvalidArgumentException
237
     */
238
    public function isColumnNullable(string $columnNameOrAlias): bool
239
    {
240
        return $this->columnNullable[$this->getTableColumnName($columnNameOrAlias)];
241
    }
242
}
243