1 | <?php |
||
2 | /** |
||
3 | * Created by Gorlum 13.11.2018 15:50 |
||
4 | */ |
||
5 | |||
6 | namespace DBAL; |
||
7 | |||
8 | /** |
||
9 | * Class PropertyDescription |
||
10 | * |
||
11 | * @package DBAL |
||
12 | */ |
||
13 | class PropertyDescription { |
||
14 | const SQL_DATE_TIME = 'Y-m-d H:i:s'; |
||
15 | |||
16 | /** |
||
17 | * Field name |
||
18 | * |
||
19 | * @var string $field |
||
20 | */ |
||
21 | public $field = ''; |
||
22 | /** |
||
23 | * Property name |
||
24 | * |
||
25 | * @var string $name |
||
26 | */ |
||
27 | public $name = ''; |
||
28 | |||
29 | /** |
||
30 | * Property type |
||
31 | * |
||
32 | * @var string $type |
||
33 | */ |
||
34 | public $type = ''; |
||
35 | public $default = null; |
||
36 | /** |
||
37 | * Is value in this field is mandatory |
||
38 | * |
||
39 | * @var bool $mandatory |
||
40 | */ |
||
41 | public $mandatory = false; |
||
42 | |||
43 | /** |
||
44 | * Callback to convert from field to property |
||
45 | * |
||
46 | * function (mixed $value) : mixed; |
||
47 | * |
||
48 | * @var callable $toProperty |
||
49 | */ |
||
50 | public $toProperty = null; |
||
51 | /** |
||
52 | * Callback to convert from property to field |
||
53 | * |
||
54 | * function (mixed $value) : mixed; |
||
55 | * |
||
56 | * @var callable $fromProperty |
||
57 | */ |
||
58 | public $fromProperty = null; |
||
59 | |||
60 | /** |
||
61 | * Callback to convert from user input to property |
||
62 | * |
||
63 | * If no $description specified - some defaults are applied |
||
64 | * |
||
65 | * function (mixed $value, PropertyDescription $description = null) : mixed; |
||
66 | * |
||
67 | * @var callable $fromUser |
||
68 | */ |
||
69 | public $fromUser = null; |
||
70 | |||
71 | // /** |
||
72 | // * function (string $name, mixed $value) : [string $ptlName, mixed $ptlValue]; |
||
73 | // * |
||
74 | // * @var callable $toPtl |
||
75 | // * @deprecated |
||
76 | // */ |
||
77 | // public $toPtl = null; |
||
78 | |||
79 | /** |
||
80 | * Callback to add extra information/convert data from property for PTL |
||
81 | * |
||
82 | * function (string $name, mixed $value, array $ptlResult) : array $ptlResult; |
||
83 | * |
||
84 | * @var callable $toPtl |
||
85 | */ |
||
86 | public $toPtl = null; |
||
87 | |||
88 | /** |
||
89 | * @param string $propertyName |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setName($propertyName) { |
||
94 | $this->name = $propertyName; |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param DbFieldDescription $fieldDescription |
||
101 | */ |
||
102 | public function fromDbFieldDescription(DbFieldDescription $fieldDescription) { |
||
103 | $this->field = $fieldDescription->Field; |
||
104 | $this->setName($fieldDescription->Field); |
||
105 | |||
106 | $nonMandatory = false; |
||
107 | |||
108 | switch (true) { |
||
109 | case strpos($fieldDescription->Type, 'int') === 0: |
||
110 | case strpos($fieldDescription->Type, 'tinyint') === 0: |
||
111 | $this->type = 'integer'; |
||
112 | $this->default = !empty($fieldDescription->Default) ? intval($fieldDescription->Default) : 0; |
||
113 | $this->toProperty = [self::class, 'toInteger']; |
||
114 | $this->fromUser = [self::class, 'intFromUser']; |
||
115 | break; |
||
116 | |||
117 | /** @noinspection PhpMissingBreakStatementInspection */ |
||
118 | case strpos($fieldDescription->Type, 'mediumtext') === 0: |
||
119 | $nonMandatory = true; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||
120 | case strpos($fieldDescription->Type, 'varchar') === 0: |
||
121 | $this->type = 'string'; |
||
122 | $this->default = !empty($fieldDescription->Default) || $fieldDescription->Default === null |
||
123 | ? $fieldDescription->Default : ''; |
||
124 | break; |
||
125 | |||
126 | case strpos($fieldDescription->Type, 'datetime') === 0: |
||
127 | $this->type = 'datetime'; |
||
128 | // $this->default = !empty($fieldDescription->Default) || $fieldDescription->Default === null |
||
129 | // ? $fieldDescription->Default : '0000-00-00 00:00:00'; |
||
130 | // TODO - current timestamp ???? |
||
131 | if ($fieldDescription->Default === null) { |
||
132 | $this->default = null; |
||
133 | } elseif (!empty($fieldDescription->Default)) { |
||
134 | $this->default = strtotime($fieldDescription->Default); |
||
135 | } else { |
||
136 | $this->default = 0; |
||
137 | } |
||
138 | $this->toProperty = [self::class, 'toUnixTime']; |
||
139 | $this->fromProperty = [self::class, 'fromUnixTime']; |
||
140 | $this->fromUser = [self::class, 'datetimeFromUser']; |
||
141 | // $this->toPtl = [self::class, 'ptlUnixTime']; |
||
142 | break; |
||
143 | |||
144 | default: |
||
145 | die("Unsupported field type '{$fieldDescription->Type}' in " . get_called_class()); |
||
0 ignored issues
–
show
|
|||
146 | break; |
||
147 | } |
||
148 | |||
149 | // Main index is non mandatory |
||
150 | if ($fieldDescription->Extra === 'auto_increment' && $fieldDescription->Key === 'PRI') { |
||
151 | $nonMandatory = true; |
||
152 | } |
||
153 | |||
154 | |||
155 | // If this field is not null and default value is not set - then this field is mandatory |
||
156 | if ($fieldDescription->Null === 'NO' && $fieldDescription->Default === null && !$nonMandatory) { |
||
157 | $this->mandatory = true; |
||
158 | } |
||
159 | |||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param string|null $value |
||
164 | * |
||
165 | * @return int |
||
166 | */ |
||
167 | public static function toInteger($value) { |
||
168 | return $value === null ? null : intval($value); |
||
169 | } |
||
170 | public static function intFromUser($value, PropertyDescription $description = null) { |
||
171 | if ($value === null) { |
||
172 | $value = $description->default; |
||
173 | } else { |
||
174 | $value = intval($value); |
||
175 | } |
||
176 | |||
177 | return $value; |
||
178 | } |
||
179 | |||
180 | public static function toUnixTime($value) { |
||
181 | return $value !== null ? strtotime($value) : $value; |
||
182 | } |
||
183 | |||
184 | public static function fromUnixTime($value) { |
||
185 | return $value !== null ? date(self::SQL_DATE_TIME, $value) : $value; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Converts input from user to datetime (unix timestamp internally) |
||
190 | * |
||
191 | * @param mixed $value |
||
192 | * @param PropertyDescription|null $description |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public static function datetimeFromUser($value, PropertyDescription $description = null) { |
||
197 | $value = strtotime($value); |
||
198 | if ($value === null) { |
||
199 | $value = $description->default; |
||
200 | } |
||
201 | |||
202 | return $value; |
||
203 | } |
||
204 | |||
205 | public static function jsonDecode($value) { |
||
206 | $value = json_decode($value, true); |
||
207 | |||
208 | return $value; |
||
209 | } |
||
210 | |||
211 | public static function jsonEncode($value) { |
||
212 | $value = json_encode($value); |
||
213 | |||
214 | return $value; |
||
215 | } |
||
216 | |||
217 | // /** |
||
218 | // * @param string $name |
||
219 | // * @param mixed $value |
||
220 | // * |
||
221 | // * @return array |
||
222 | // */ |
||
223 | // public static function ptlUnixTime($name, $value) { |
||
224 | // return [$name . '_STRING', empty($value) ? '' : date(self::SQL_DATE_TIME, $value)]; |
||
225 | // } |
||
226 | |||
227 | } |
||
228 |