1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/old-town-propertyset |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\PropertySet; |
7
|
|
|
|
8
|
|
|
use DateTime; |
9
|
|
|
use DOMDocument; |
10
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractPropertySet |
14
|
|
|
* |
15
|
|
|
* @package OldTown\PropertySet |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractPropertySet implements PropertySetInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PropertySetSchema |
21
|
|
|
*/ |
22
|
|
|
protected $schema; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $property |
26
|
|
|
* |
27
|
|
|
* @return boolean |
28
|
|
|
*/ |
29
|
|
|
public function isSettable($property) |
30
|
|
|
{ |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $config |
36
|
|
|
* @param array $args |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function init(array $config = [], array $args = []) |
41
|
|
|
{ |
42
|
|
|
// TODO: Implement init() method. |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param integer $type |
47
|
|
|
* |
48
|
|
|
* @return boolean |
49
|
|
|
* |
50
|
|
|
* @throws \OldTown\PropertySet\Exception\PropertyException |
51
|
|
|
*/ |
52
|
|
|
public function supportsType($type) |
53
|
|
|
{ |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
public function __toString() |
61
|
|
|
{ |
62
|
|
|
$result = ''; |
63
|
|
|
|
64
|
|
|
$data = ''; |
65
|
|
|
try { |
66
|
|
|
$keys = $this->getKeys(); |
67
|
|
|
foreach ($keys as $key) { |
68
|
|
|
$type = $this->getType($key); |
69
|
|
|
if ($type > 0) { |
70
|
|
|
$value = $this->get($type, $key); |
71
|
|
|
$data .= sprintf("\t %s = %s \n", $key, $value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
$result = sprintf("%s {\n%s}\n", static::class, $data); |
75
|
|
|
} catch (Exception\PropertyException $e) { |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
* |
88
|
|
|
* @throws Exception\PropertyException |
89
|
|
|
*/ |
90
|
|
|
public function setAsActualType($key, $value) |
91
|
|
|
{ |
92
|
|
|
$type = null; |
|
|
|
|
93
|
|
|
if (is_bool($value)) { |
94
|
|
|
$type = self::BOOLEAN; |
95
|
|
|
} elseif (is_int($value)) { |
96
|
|
|
$type = self::INT; |
97
|
|
|
} elseif (is_float($value)) { |
98
|
|
|
$type = self::FLOAT; |
99
|
|
|
} elseif (is_string($value)) { |
100
|
|
|
$type = strlen($value) > 255 ? self::DATA : self::STRING; |
101
|
|
|
} elseif ($value instanceof DateTime) { |
102
|
|
|
$type = self::DATE; |
103
|
|
|
} elseif ($value instanceof DOMDocument) { |
104
|
|
|
$type = self::XML; |
105
|
|
|
} elseif (is_array($value)) { |
106
|
|
|
$type = self::DATA_ARRAY; |
107
|
|
|
} elseif (is_object($value)) { |
108
|
|
|
$type = self::OBJECT; |
109
|
|
|
} else { |
110
|
|
|
$errMsg = 'Неизвестный тип'; |
111
|
|
|
throw new Exception\PropertyException($errMsg); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->set($type, $key, $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $key |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
* |
122
|
|
|
* @throws Exception\PropertyException |
123
|
|
|
*/ |
124
|
|
|
public function getAsActualType($key) |
125
|
|
|
{ |
126
|
|
|
$type = $this->getType($key); |
127
|
|
|
$value = null; |
128
|
|
|
switch ($type) { |
129
|
|
|
case self::BOOLEAN: { |
|
|
|
|
130
|
|
|
$value = $this->getBoolean($key); |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
case self::INT: { |
|
|
|
|
134
|
|
|
$value = $this->getInt($key); |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
case self::FLOAT: { |
|
|
|
|
138
|
|
|
$value = $this->getFloat($key); |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
case self::STRING: { |
|
|
|
|
142
|
|
|
$value = $this->getString($key); |
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
case self::DATE: { |
|
|
|
|
146
|
|
|
$value = $this->getDate($key); |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
case self::XML: { |
|
|
|
|
150
|
|
|
$value = $this->getXML($key); |
151
|
|
|
break; |
152
|
|
|
} |
153
|
|
|
case self::DATA: { |
|
|
|
|
154
|
|
|
$value = $this->getData($key); |
155
|
|
|
break; |
156
|
|
|
} |
157
|
|
|
case self::DATA_ARRAY: { |
|
|
|
|
158
|
|
|
$value = $this->getArray($key); |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
case self::OBJECT: { |
|
|
|
|
162
|
|
|
$value = $this->getArray($key); |
163
|
|
|
break; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $key |
172
|
|
|
* @param object $value |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
* |
176
|
|
|
* @throws Exception\PropertyException |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function setObject($key, $value) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
if (!settype($value, 'object')) { |
181
|
|
|
$errMsg = 'Ошибка при конвертации к типу object'; |
182
|
|
|
throw new Exception\PropertyException($errMsg); |
183
|
|
|
} |
184
|
|
|
$this->set(self::OBJECT, $key, $value); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $key |
191
|
|
|
* |
192
|
|
|
* @return object |
193
|
|
|
* |
194
|
|
|
* @throws Exception\PropertyException |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function getObject($key) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
try { |
199
|
|
|
$result = $this->get(self::OBJECT, $key); |
200
|
|
|
if (!settype($result, 'object')) { |
201
|
|
|
return null; |
202
|
|
|
} |
203
|
|
|
} catch (\Exception $e) { |
204
|
|
|
return null; |
205
|
|
|
} |
206
|
|
|
return $result; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $key |
212
|
|
|
* @param array|Traversable $value |
213
|
|
|
* |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
* |
217
|
|
|
* @throws Exception\PropertyException |
218
|
|
|
*/ |
219
|
|
|
public function setArray($key, $value) |
220
|
|
|
{ |
221
|
|
|
$newValue = null; |
|
|
|
|
222
|
|
|
if ($value instanceof Traversable) { |
223
|
|
|
$newValue = []; |
224
|
|
|
foreach ($value as $k => $v) { |
225
|
|
|
$newValue[$k] = $v; |
226
|
|
|
} |
227
|
|
|
} else { |
228
|
|
|
$newValue = $value; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (!settype($newValue, 'array')) { |
232
|
|
|
$errMsg = 'Ошибка при конвертации к типу array'; |
233
|
|
|
throw new Exception\PropertyException($errMsg); |
234
|
|
|
} |
235
|
|
|
$this->set(self::DATA_ARRAY, $key, $value); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string $key |
242
|
|
|
* |
243
|
|
|
* @return array |
244
|
|
|
* |
245
|
|
|
* @throws Exception\PropertyException |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function getArray($key) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
try { |
250
|
|
|
$result = $this->get(self::DATA_ARRAY, $key); |
251
|
|
|
if (!settype($result, 'array')) { |
252
|
|
|
return null; |
253
|
|
|
} |
254
|
|
|
} catch (\Exception $e) { |
255
|
|
|
return null; |
256
|
|
|
} |
257
|
|
|
return $result; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $key |
263
|
|
|
* @param string $value |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
* |
267
|
|
|
* @throws Exception\PropertyException |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function setData($key, $value) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
if (!settype($value, 'string')) { |
272
|
|
|
$errMsg = 'Ошибка при конвертации к типу data'; |
273
|
|
|
throw new Exception\PropertyException($errMsg); |
274
|
|
|
} |
275
|
|
|
$this->set(self::DATA, $key, $value); |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param string $key |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
* |
285
|
|
|
* @throws Exception\PropertyException |
286
|
|
|
*/ |
287
|
|
View Code Duplication |
public function getData($key) |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
try { |
290
|
|
|
$result = $this->get(self::STRING, $key); |
291
|
|
|
if (!settype($result, 'string')) { |
292
|
|
|
return null; |
293
|
|
|
} |
294
|
|
|
} catch (\Exception $e) { |
295
|
|
|
return null; |
296
|
|
|
} |
297
|
|
|
return $result; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $key |
303
|
|
|
* @param DOMDocument $value |
304
|
|
|
* |
305
|
|
|
* @return $this |
306
|
|
|
* @throws Exception\PropertyException |
307
|
|
|
*/ |
308
|
|
|
public function setXML($key, DOMDocument $value) |
309
|
|
|
{ |
310
|
|
|
$this->set(self::XML, $key, $value); |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param string $key |
317
|
|
|
* |
318
|
|
|
* @return DOMDocument |
319
|
|
|
* |
320
|
|
|
* @throws Exception\PropertyException |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function getXML($key) |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
try { |
325
|
|
|
$result = $this->get(self::XML, $key); |
326
|
|
|
if (!$result instanceof DOMDocument) { |
327
|
|
|
return null; |
328
|
|
|
} |
329
|
|
|
} catch (\Exception $e) { |
330
|
|
|
return null; |
331
|
|
|
} |
332
|
|
|
return $result; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param String $key |
338
|
|
|
* @param DateTime $value |
339
|
|
|
* |
340
|
|
|
* @return $this |
341
|
|
|
* |
342
|
|
|
* @throws Exception\PropertyException |
343
|
|
|
*/ |
344
|
|
|
public function setDate($key, DateTime $value) |
345
|
|
|
{ |
346
|
|
|
$this->set(self::DATE, $key, $value); |
347
|
|
|
|
348
|
|
|
return $this; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $key |
353
|
|
|
* |
354
|
|
|
* @return DateTime |
355
|
|
|
* |
356
|
|
|
* @throws Exception\PropertyException |
357
|
|
|
*/ |
358
|
|
View Code Duplication |
public function getDate($key) |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
try { |
361
|
|
|
$result = $this->get(self::DATE, $key); |
362
|
|
|
if (!$result instanceof DateTime) { |
363
|
|
|
return null; |
364
|
|
|
} |
365
|
|
|
} catch (\Exception $e) { |
366
|
|
|
return null; |
367
|
|
|
} |
368
|
|
|
return $result; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param string $key |
374
|
|
|
* @param string $value |
375
|
|
|
* |
376
|
|
|
* @return $this |
377
|
|
|
* |
378
|
|
|
* @throws Exception\PropertyException |
379
|
|
|
*/ |
380
|
|
|
public function setString($key, $value) |
381
|
|
|
{ |
382
|
|
|
if (!settype($value, 'string')) { |
383
|
|
|
$errMsg = 'Ошибка при конвертации к типу string'; |
384
|
|
|
throw new Exception\PropertyException($errMsg); |
385
|
|
|
} |
386
|
|
|
if (strlen($value) > 255) { |
387
|
|
|
$errMsg = 'Строка не должна быть больше 255 символов'; |
388
|
|
|
throw new Exception\PropertyException($errMsg); |
389
|
|
|
} |
390
|
|
|
$this->set(self::STRING, $key, $value); |
391
|
|
|
|
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @param string $key |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
* |
400
|
|
|
* @throws Exception\PropertyException |
401
|
|
|
*/ |
402
|
|
View Code Duplication |
public function getString($key) |
|
|
|
|
403
|
|
|
{ |
404
|
|
|
try { |
405
|
|
|
$result = $this->get(self::STRING, $key); |
406
|
|
|
if (!settype($result, 'string')) { |
407
|
|
|
return null; |
408
|
|
|
} |
409
|
|
|
} catch (\Exception $e) { |
410
|
|
|
return null; |
411
|
|
|
} |
412
|
|
|
return $result; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @param string $key |
418
|
|
|
* @param boolean $value |
419
|
|
|
* |
420
|
|
|
* @return $this |
421
|
|
|
* |
422
|
|
|
* @throws Exception\PropertyException |
423
|
|
|
*/ |
424
|
|
View Code Duplication |
public function setBoolean($key, $value) |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
if (!settype($value, 'boolean')) { |
427
|
|
|
$errMsg = 'Ошибка при конвертации к типу bool'; |
428
|
|
|
throw new Exception\PropertyException($errMsg); |
429
|
|
|
} |
430
|
|
|
$this->set(self::BOOLEAN, $key, $value); |
431
|
|
|
|
432
|
|
|
return $this; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param String $key |
437
|
|
|
* |
438
|
|
|
* @return boolean |
439
|
|
|
* |
440
|
|
|
* @throws Exception\PropertyException |
441
|
|
|
*/ |
442
|
|
View Code Duplication |
public function getBoolean($key) |
|
|
|
|
443
|
|
|
{ |
444
|
|
|
try { |
445
|
|
|
$result = $this->get(self::BOOLEAN, $key); |
446
|
|
|
if (!settype($result, 'boolean')) { |
447
|
|
|
return false; |
448
|
|
|
} |
449
|
|
|
} catch (\Exception $e) { |
450
|
|
|
return false; |
451
|
|
|
} |
452
|
|
|
return $result; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @param string $key |
457
|
|
|
* @param integer $value |
458
|
|
|
* |
459
|
|
|
* @return $this |
460
|
|
|
* |
461
|
|
|
* @throws Exception\PropertyException |
462
|
|
|
*/ |
463
|
|
View Code Duplication |
public function setInt($key, $value) |
|
|
|
|
464
|
|
|
{ |
465
|
|
|
if (!settype($value, 'integer')) { |
466
|
|
|
$errMsg = 'Ошибка при конвертации к типу integer'; |
467
|
|
|
throw new Exception\PropertyException($errMsg); |
468
|
|
|
} |
469
|
|
|
$this->set(self::INT, $key, $value); |
470
|
|
|
|
471
|
|
|
return $this; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @param string $key |
476
|
|
|
* |
477
|
|
|
* @return integer |
478
|
|
|
* |
479
|
|
|
* @throws Exception\PropertyException |
480
|
|
|
*/ |
481
|
|
View Code Duplication |
public function getInt($key) |
|
|
|
|
482
|
|
|
{ |
483
|
|
|
try { |
484
|
|
|
$result = $this->get(self::INT, $key); |
485
|
|
|
if (!settype($result, 'integer')) { |
486
|
|
|
return 0; |
487
|
|
|
} |
488
|
|
|
} catch (\Exception $e) { |
489
|
|
|
return 0; |
490
|
|
|
} |
491
|
|
|
return $result; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @param string $key |
496
|
|
|
* @param float $value |
497
|
|
|
* |
498
|
|
|
* @return $this |
499
|
|
|
* |
500
|
|
|
* @throws Exception\PropertyException |
501
|
|
|
*/ |
502
|
|
View Code Duplication |
public function setFloat($key, $value) |
|
|
|
|
503
|
|
|
{ |
504
|
|
|
if (!settype($value, 'float')) { |
505
|
|
|
$errMsg = 'Ошибка при конвертации к типу float'; |
506
|
|
|
throw new Exception\PropertyException($errMsg); |
507
|
|
|
} |
508
|
|
|
$this->set(self::FLOAT, $key, $value); |
509
|
|
|
|
510
|
|
|
return $this; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @param string $key |
515
|
|
|
* |
516
|
|
|
* @return float |
517
|
|
|
*/ |
518
|
|
View Code Duplication |
public function getFloat($key) |
|
|
|
|
519
|
|
|
{ |
520
|
|
|
try { |
521
|
|
|
$result = $this->get(self::FLOAT, $key); |
522
|
|
|
if (!settype($result, 'float')) { |
523
|
|
|
return 0.0; |
524
|
|
|
} |
525
|
|
|
} catch (\Exception $e) { |
526
|
|
|
return 0.0; |
527
|
|
|
} |
528
|
|
|
return $result; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* @return PropertySetSchema |
534
|
|
|
*/ |
535
|
|
|
public function getSchema() |
536
|
|
|
{ |
537
|
|
|
return $this->schema; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* @param PropertySetSchema $schema |
543
|
|
|
* |
544
|
|
|
* @return $this |
545
|
|
|
*/ |
546
|
|
|
public function setSchema(PropertySetSchema $schema) |
547
|
|
|
{ |
548
|
|
|
$this->schema = $schema; |
549
|
|
|
|
550
|
|
|
return $this; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @return boolean |
555
|
|
|
*/ |
556
|
|
|
public function supportsTypes() |
557
|
|
|
{ |
558
|
|
|
return true; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param integer $type |
564
|
|
|
* @param string $key |
565
|
|
|
* @param mixed $value |
566
|
|
|
* |
567
|
|
|
* @return $this |
568
|
|
|
* |
569
|
|
|
* @throws Exception\IllegalPropertyException |
570
|
|
|
* @throws Exception\InvalidPropertyTypeException |
571
|
|
|
* @throws \OldTown\PropertySet\Exception\PropertyException |
572
|
|
|
*/ |
573
|
|
|
protected function set($type, $key, $value) |
574
|
|
|
{ |
575
|
|
|
$schema = $this->getSchema(); |
576
|
|
|
if (null !== $schema) { |
577
|
|
|
$ps = $schema->getPropertySchema($key); |
578
|
|
|
|
579
|
|
|
if (null === $ps && $schema->isRestricted()) { |
580
|
|
|
$errMsg = sprintf( |
581
|
|
|
'Свойство %s явно не указано в схеме', |
582
|
|
|
$key |
583
|
|
|
); |
584
|
|
|
throw new Exception\IllegalPropertyException($errMsg); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
if ($this->supportsTypes() && ($type !== $ps->getType())) { |
588
|
|
|
$errMsg = sprintf( |
589
|
|
|
'Свойство %s имеет неверный тип %s. Ожидает что тип будет %s', |
590
|
|
|
$key, |
591
|
|
|
$type, |
592
|
|
|
$ps->getType() |
593
|
|
|
); |
594
|
|
|
throw new Exception\InvalidPropertyTypeException($errMsg); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
$ps->validate($value); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$this->setImpl($type, $key, $value); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* @param integer $type |
605
|
|
|
* @param string $key |
606
|
|
|
* @param mixed $value |
607
|
|
|
* |
608
|
|
|
* @return $this |
609
|
|
|
* |
610
|
|
|
* @throws Exception\PropertyException |
611
|
|
|
*/ |
612
|
|
|
protected function setImpl($type, $key, $value) |
613
|
|
|
{ |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* @param integer $type |
619
|
|
|
* @param string $key |
620
|
|
|
* |
621
|
|
|
* @return mixed |
622
|
|
|
* |
623
|
|
|
* @throws Exception\InvalidPropertyTypeException |
624
|
|
|
* @throws \OldTown\PropertySet\Exception\PropertyException |
625
|
|
|
*/ |
626
|
|
|
protected function get(/** @noinspection PhpUnusedParameterInspection */ $type, $key) |
627
|
|
|
{ |
628
|
|
|
$errMsg = sprintf( |
629
|
|
|
'Метод %s должен быть релизован в классе потомке', |
630
|
|
|
__METHOD__ |
631
|
|
|
); |
632
|
|
|
trigger_error($errMsg, E_USER_ERROR); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @param integer|string $type |
637
|
|
|
* |
638
|
|
|
* @return integer|string |
639
|
|
|
* |
640
|
|
|
* @throws \OldTown\PropertySet\Exception\RuntimeException |
641
|
|
|
*/ |
642
|
|
|
protected function type($type) |
643
|
|
|
{ |
644
|
|
|
if (is_numeric($type)) { |
645
|
|
|
$iniType = (integer)$type; |
646
|
|
|
$result = $this->getTypeByCode($iniType); |
647
|
|
|
return $result; |
648
|
|
|
} elseif (is_string($type)) { |
649
|
|
|
$result = $this->getTypeByName($type); |
650
|
|
|
return $result; |
651
|
|
|
} |
652
|
|
|
$errMsg = 'Некорректное значение для типа'; |
653
|
|
|
throw new Exception\RuntimeException($errMsg); |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* @param integer $type |
658
|
|
|
* |
659
|
|
|
* @return string |
660
|
|
|
*/ |
661
|
|
|
protected function getTypeByCode($type) |
662
|
|
|
{ |
663
|
|
|
switch ($type) { |
664
|
|
|
case self::BOOLEAN: |
665
|
|
|
return 'boolean'; |
666
|
|
|
case self::INT: |
667
|
|
|
return 'int'; |
668
|
|
|
case self::FLOAT: |
669
|
|
|
return 'float'; |
670
|
|
|
case self::STRING: |
671
|
|
|
return 'string'; |
672
|
|
|
case self::DATE: |
673
|
|
|
return 'date'; |
674
|
|
|
case self::OBJECT: |
675
|
|
|
return 'object'; |
676
|
|
|
case self::XML: |
677
|
|
|
return 'xml'; |
678
|
|
|
case self::DATA: |
679
|
|
|
return 'data'; |
680
|
|
|
case self::DATA_ARRAY: |
681
|
|
|
return 'array'; |
682
|
|
|
|
683
|
|
|
default: |
684
|
|
|
return null; |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* @param string|null $type |
690
|
|
|
* |
691
|
|
|
* @return integer |
692
|
|
|
* |
693
|
|
|
* @throws \OldTown\PropertySet\Exception\RuntimeException |
694
|
|
|
*/ |
695
|
|
|
protected function getTypeByName($type = null) |
696
|
|
|
{ |
697
|
|
|
if (null === $type) { |
698
|
|
|
return 0; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
if (!settype($type, 'string')) { |
702
|
|
|
$errMsg = 'Оибка при преобразование в тип string'; |
703
|
|
|
throw new Exception\RuntimeException($errMsg); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
$type = strtolower($type); |
707
|
|
|
|
708
|
|
|
if ('boolean' === $type) { |
709
|
|
|
return self::BOOLEAN; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
if ('int' === $type) { |
713
|
|
|
return self::INT; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
if ('float' === $type) { |
717
|
|
|
return self::FLOAT; |
718
|
|
|
} |
719
|
|
|
if ('string' === $type) { |
720
|
|
|
return self::STRING; |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
if ('date' === $type) { |
724
|
|
|
return self::DATE; |
725
|
|
|
} |
726
|
|
|
if ('xml' === $type) { |
727
|
|
|
return self::XML; |
728
|
|
|
} |
729
|
|
|
if ('array' === $type) { |
730
|
|
|
return self::DATA_ARRAY; |
731
|
|
|
} |
732
|
|
|
if ('object' === $type) { |
733
|
|
|
return self::OBJECT; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
return 0; |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
|