1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Core component |
4
|
|
|
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/core/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Column Definitions define columns (who would've thought of that?). Such as their name, type, length, etc. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractColumnDefinition implements ColumnDefinitionInterface { |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $database; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $table; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $type; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
protected $charset; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int|null |
43
|
|
|
*/ |
44
|
|
|
protected $length; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $flags; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int|null |
53
|
|
|
*/ |
54
|
|
|
protected $decimals; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
* @param string $database |
59
|
|
|
* @param string $table |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string $type |
62
|
|
|
* @param string|null $charset |
63
|
|
|
* @param int|null $length |
64
|
|
|
* @param int $flags |
65
|
|
|
* @param int|null $decimals |
66
|
|
|
*/ |
67
|
15 |
|
function __construct(string $database, string $table, string $name, string $type, ?string $charset, ?int $length, int $flags, ?int $decimals) { |
68
|
15 |
|
$this->database = $database; |
69
|
15 |
|
$this->table = $table; |
70
|
15 |
|
$this->name = $name; |
71
|
15 |
|
$this->type = $type; |
72
|
15 |
|
$this->charset = $charset; |
73
|
15 |
|
$this->length = $length; |
74
|
15 |
|
$this->flags = $flags; |
75
|
15 |
|
$this->decimals = $decimals; |
76
|
15 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the database name this column is in. |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
function getDatabaseName(): string { |
83
|
1 |
|
return $this->database; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the table name this column is in. |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
1 |
|
function getTableName(): string { |
91
|
1 |
|
return $this->table; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the column name. |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1 |
|
function getName(): string { |
99
|
1 |
|
return $this->name; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the type name, such as `BIGINT`, `VARCHAR`, etc. |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
function getType(): string { |
107
|
1 |
|
return $this->type; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the charset, such as `utf8mb4`. |
112
|
|
|
* @return string|null |
113
|
|
|
*/ |
114
|
1 |
|
function getCharset(): ?string { |
115
|
1 |
|
return $this->charset; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the maximum field length, if any. |
120
|
|
|
* @return int|null |
121
|
|
|
*/ |
122
|
1 |
|
function getLength(): ?int { |
123
|
1 |
|
return $this->length; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the column flags. |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
1 |
|
function getFlags(): int { |
131
|
1 |
|
return $this->flags; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the maximum shown decimal digits. |
136
|
|
|
* @return int|null |
137
|
|
|
*/ |
138
|
1 |
|
function getDecimals(): ?int { |
139
|
1 |
|
return $this->decimals; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Parses the row value into the field type. |
144
|
|
|
* @param mixed $value |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
2 |
|
function parseValue($value) { |
148
|
|
|
try { |
149
|
2 |
|
return \Plasma\Types\TypeExtensionsManager::getManager()->decodeType($this->type, $value)->getValue(); |
150
|
1 |
|
} catch (\Plasma\Exception $e) { |
151
|
|
|
/* Continue regardless of error */ |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return $value; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|