|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject\EasyDB; |
|
10
|
|
|
|
|
11
|
|
|
use ParagonIE\EasyDB\EasyDB; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
use ReflectionType; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
use SignpostMarv\DaftObject\AbstractDaftObjectEasyDBRepository; |
|
16
|
|
|
|
|
17
|
|
|
class TestObjectRepository extends AbstractDaftObjectEasyDBRepository |
|
18
|
|
|
{ |
|
19
|
6 |
|
protected function __construct(string $type, EasyDB $db) |
|
20
|
|
|
{ |
|
21
|
6 |
|
parent::__construct($type, $db); |
|
22
|
|
|
$query = |
|
23
|
|
|
'CREATE TABLE ' . |
|
24
|
6 |
|
$db->escapeIdentifier($this->DaftObjectDatabaseTable()) . |
|
25
|
6 |
|
' ('; |
|
26
|
|
|
|
|
27
|
6 |
|
$queryParts = []; |
|
28
|
|
|
|
|
29
|
6 |
|
$ref = new ReflectionClass($type); |
|
30
|
6 |
|
$nullables = $type::DaftObjectNullableProperties(); |
|
31
|
|
|
|
|
32
|
6 |
|
foreach ($type::DaftObjectProperties() as $i => $prop) { |
|
33
|
6 |
|
$methodName = 'Get' . ucfirst($prop); |
|
34
|
6 |
|
if ($ref->hasMethod($methodName) === true) { |
|
35
|
6 |
|
$refReturn = $ref->getMethod($methodName)->getReturnType(); |
|
36
|
|
|
|
|
37
|
|
|
if ( |
|
38
|
6 |
|
($refReturn instanceof ReflectionType) && |
|
39
|
6 |
|
$refReturn->isBuiltin() |
|
40
|
|
|
) { |
|
41
|
6 |
|
$queryPart = $db->escapeIdentifier($prop); |
|
42
|
6 |
|
switch ($refReturn->__toString()) { |
|
43
|
6 |
|
case 'string': |
|
44
|
6 |
|
$queryPart .= ' VARCHAR(255)'; |
|
45
|
6 |
|
break; |
|
46
|
6 |
|
case 'float': |
|
47
|
6 |
|
$queryPart .= ' REAL'; |
|
48
|
6 |
|
break; |
|
49
|
6 |
|
case 'int': |
|
50
|
6 |
|
case 'bool': |
|
51
|
6 |
|
$queryPart .= ' INTEGER'; |
|
52
|
6 |
|
break; |
|
53
|
|
|
default: |
|
54
|
|
|
throw new RuntimeException( |
|
55
|
|
|
sprintf( |
|
56
|
|
|
'Unsupported data type! (%s)', |
|
57
|
|
|
$refReturn->__toString() |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
6 |
|
if (in_array($prop, $nullables, true) === false) { |
|
62
|
6 |
|
$queryPart .= ' NOT NULL'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
$queryParts[] = $queryPart; |
|
66
|
|
|
} else { |
|
67
|
|
|
throw new RuntimeException('Only supports builtins'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
$primaryKeyCols = []; |
|
73
|
6 |
|
foreach ($type::DaftObjectIdProperties() as $col) { |
|
74
|
6 |
|
$primaryKeyCols[] = $db->escapeIdentifier($col); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
6 |
|
if (count($primaryKeyCols) > 0) { |
|
78
|
6 |
|
$queryParts[] = |
|
79
|
|
|
'PRIMARY KEY (' . |
|
80
|
6 |
|
implode(',', $primaryKeyCols) . |
|
81
|
6 |
|
')'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$query .= |
|
85
|
6 |
|
implode(',', $queryParts) . |
|
86
|
6 |
|
');'; |
|
87
|
|
|
|
|
88
|
6 |
|
$db->safeQuery($query); |
|
89
|
6 |
|
} |
|
90
|
|
|
|
|
91
|
6 |
|
protected function DaftObjectDatabaseTable() : string |
|
92
|
|
|
{ |
|
93
|
6 |
|
return preg_replace('/[^a-z]+/', '_', mb_strtolower($this->type)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|