Completed
Push — master ( 06e1ac...896e66 )
by SignpostMarv
03:50
created

TestObjectRepository::QueryPartFromRefReturn()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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