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