translator::to_constant()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
namespace midgard\portable\mgdschema;
9
10
class translator
11
{
12
    const TYPE_NONE = 4;
13
    const TYPE_BOOLEAN = 20;
14
    const TYPE_INT = 24;
15
    const TYPE_UINT = 28;
16
    const TYPE_FLOAT = 56;
17
    const TYPE_STRING = 64;
18
    const TYPE_LONGTEXT = 196;
19
    const TYPE_TIMESTAMP = 139645924049440;
20
    const TYPE_GUID = 139645923896704;
21
22
    private static array $typemap = [
23
        'unsigned integer' => self::TYPE_UINT,
24
        'integer' => self::TYPE_INT,
25
        'boolean' => self::TYPE_BOOLEAN,
26
        'bool' => self::TYPE_BOOLEAN,
27
        'guid' => self::TYPE_GUID,
28
        //'varchar(80)' => self::TYPE_GUID, // <== true for all cases?
29
        'string' => self::TYPE_STRING,
30
        'datetime' => self::TYPE_TIMESTAMP,
31
        'date' => self::TYPE_TIMESTAMP,
32
        'text' => self::TYPE_LONGTEXT,
33
        'longtext' => self::TYPE_LONGTEXT,
34
        'float' => self::TYPE_FLOAT
35
    ];
36
37 8
    public static function to_phptype(string $typeattribute) : string
38
    {
39 8
        if (!isset(self::$typemap[$typeattribute])) {
40
            throw new \Exception('unknown type ' . $typeattribute);
41
        }
42 8
        $search = ['unsigned ', 'guid', 'datetime', 'longtext', 'text'];
43 8
        $replace = ['', 'string', 'midgard_datetime', 'string', 'string'];
44
45 8
        return str_replace($search, $replace, $typeattribute);
46
    }
47
48 15
    public static function to_constant(string $typeattribute) : int
49
    {
50 15
        if (!isset(self::$typemap[$typeattribute])) {
51
            throw new \Exception('unknown type ' . $typeattribute);
52
        }
53 15
        return self::$typemap[$typeattribute];
54
    }
55
}
56