summaryrefslogtreecommitdiff
path: root/engine/Model.php
blob: e80b09df9adc6bef430f33af464df2745b4f5681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php

enum Type {
    case STRING;
    case INTEGER;
    case FLOAT;
    case ARRAY;
    case BOOLEAN;
    case JSON;
    case SERIALIZED;
}

abstract class Model {

    const DB_TABLE = null;
    const DB_KEY = 'id';

    protected static array $SpecCache = [];

    public static function create_instance(...$args) {
        $cl = get_called_class();
        return new $cl(...$args);
    }

    public function __construct(array $raw) {
        if (!isset(self::$SpecCache[static::class])) {
            list($fields, $model_name_map, $db_name_map) = static::get_spec();
            self::$SpecCache[static::class] = [
                'fields' => $fields,
                'model_name_map' => $model_name_map,
                'db_name_map' => $db_name_map
            ];
        }

        foreach (self::$SpecCache[static::class]['fields'] as $field)
            $this->{$field['model_name']} = self::cast_to_type($field['type'], $raw[$field['db_name']]);

        if (is_null(static::DB_TABLE))
            trigger_error('class '.get_class($this).' doesn\'t have DB_TABLE defined');
    }

    public function edit(array $fields) {
        $db = getDb();

        $model_upd = [];
        $db_upd = [];

        foreach ($fields as $name => $value) {
            $index = self::$SpecCache[static::class]['db_name_map'][$name] ?? null;
            if (is_null($index)) {
                logError(__METHOD__.': field `'.$name.'` not found in '.static::class);
                continue;
            }

            $field = self::$SpecCache[static::class]['fields'][$index];
            switch ($field['type']) {
                case Type::ARRAY:
                    if (is_array($value)) {
                        $db_upd[$name] = implode(',', $value);
                        $model_upd[$field['model_name']] = $value;
                    } else {
                        logError(__METHOD__.': field `'.$name.'` is expected to be array. skipping.');
                    }
                    break;

                case Type::INTEGER:
                    $value = (int)$value;
                    $db_upd[$name] = $value;
                    $model_upd[$field['model_name']] = $value;
                    break;

                case Type::FLOAT:
                    $value = (float)$value;
                    $db_upd[$name] = $value;
                    $model_upd[$field['model_name']] = $value;
                    break;

                case Type::BOOLEAN:
                    $db_upd[$name] = $value ? 1 : 0;
                    $model_upd[$field['model_name']] = $value;
                    break;

                case Type::JSON:
                    $db_upd[$name] = json_encode($value, JSON_UNESCAPED_UNICODE);
                    $model_upd[$field['model_name']] = $value;
                    break;

                case Type::SERIALIZED:
                    $db_upd[$name] = serialize($value);
                    $model_upd[$field['model_name']] = $value;
                    break;

                default:
                    $value = (string)$value;
                    $db_upd[$name] = $value;
                    $model_upd[$field['model_name']] = $value;
                    break;
            }
        }

        if (!empty($db_upd) && !$db->update(static::DB_TABLE, $db_upd, static::DB_KEY."=?", $this->get_id())) {
            logError(__METHOD__.': failed to update database');
            return;
        }

        if (!empty($model_upd)) {
            foreach ($model_upd as $name => $value)
                $this->{$name} = $value;
        }
    }

    public function get_id() {
        return $this->{to_camel_case(static::DB_KEY)};
    }

    public function as_array(array $fields = [], array $custom_getters = []): array {
        if (empty($fields))
            $fields = array_keys(static::$SpecCache[static::class]['db_name_map']);

        $array = [];
        foreach ($fields as $field) {
            if (isset($custom_getters[$field]) && is_callable($custom_getters[$field])) {
                $array[$field] = $custom_getters[$field]();
            } else {
                $array[$field] = $this->{to_camel_case($field)};
            }
        }

        return $array;
    }

    protected static function cast_to_type(Type $type, $value) {
        switch ($type) {
            case Type::BOOLEAN:
                return (bool)$value;

            case Type::INTEGER:
                return (int)$value;

            case Type::FLOAT:
                return (float)$value;

            case Type::ARRAY:
                return array_filter(explode(',', $value));

            case Type::JSON:
                $val = json_decode($value, true);
                if (!$val)
                    $val = null;
                return $val;

            case Type::SERIALIZED:
                $val = unserialize($value);
                if ($val === false)
                    $val = null;
                return $val;

            default:
                return (string)$value;
        }
    }

    protected static function get_spec(): array {
        $rc = new ReflectionClass(static::class);
        $props = $rc->getProperties(ReflectionProperty::IS_PUBLIC);

        $list = [];
        $index = 0;

        $model_name_map = [];
        $db_name_map = [];

        foreach ($props as $prop) {
            if ($prop->isStatic())
                continue;

            $name = $prop->getName();
            if (str_starts_with($name, '_'))
                continue;

            $type = $prop->getType();
            $phpdoc = $prop->getDocComment();

            $mytype = null;
            if (!$prop->hasType() && !$phpdoc)
                $mytype = Type::STRING;
            else {
                $typename = $type->getName();
                switch ($typename) {
                    case 'string':
                        $mytype = Type::STRING;
                        break;
                    case 'int':
                        $mytype = Type::INTEGER;
                        break;
                    case 'float':
                        $mytype = Type::FLOAT;
                        break;
                    case 'array':
                        $mytype = Type::ARRAY;
                        break;
                    case 'bool':
                        $mytype = Type::BOOLEAN;
                        break;
                }

                if ($phpdoc != '') {
                    $pos = strpos($phpdoc, '@');
                    if ($pos === false)
                        continue;

                    if (substr($phpdoc, $pos+1, 4) == 'json')
                        $mytype = Type::JSON;
                    else if (substr($phpdoc, $pos+1, 5) == 'array')
                        $mytype = Type::ARRAY;
                    else if (substr($phpdoc, $pos+1, 10) == 'serialized')
                        $mytype = Type::SERIALIZED;
                }
            }

            if (is_null($mytype))
                logError(__METHOD__.": ".$name." is still null in ".static::class);

            $dbname = from_camel_case($name);
            $list[] = [
                'type' => $mytype,
                'model_name' => $name,
                'db_name' => $dbname
            ];

            $model_name_map[$name] = $index;
            $db_name_map[$dbname] = $index;

            $index++;
        }

        return [$list, $model_name_map, $db_name_map];
    }

}