DmsColumn   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 197
rs 10
c 0
b 0
f 0
wmc 28

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A getPhpName() 0 8 1
A getName() 0 3 1
C getPhpType() 0 28 14
A getKey() 0 3 1
A getExtra() 0 3 1
A getDefault() 0 3 1
A __construct() 0 14 1
A getPhpDefaultType() 0 18 6
A isNullable() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\Generator\Dms;
4
5
use Janisbiz\LightOrm\Generator\Dms\DmsColumnInterface;
6
7
class DmsColumn implements DmsColumnInterface
8
{
9
    const TYPE_BIGINT = 'bigint';
10
    const TYPE_CHAR = 'char';
11
    const TYPE_DATE = 'date';
12
    const TYPE_DATETIME = 'datetime';
13
    const TYPE_FLOAT = 'float';
14
    const TYPE_INT = 'int';
15
    const TYPE_JSON = 'json';
16
    const TYPE_LONGTEXT = 'longtext';
17
    const TYPE_MEDIUMTEXT = 'mediumtext';
18
    const TYPE_TEXT = 'text';
19
    const TYPE_TIMESTAMP = 'timestamp';
20
    const TYPE_TINYINT = 'tinyint';
21
    const TYPE_VARCHAR = 'varchar';
22
23
    const PHP_TYPE_ARRAY = 'array';
24
    const PHP_TYPE_BOOLEAN = 'bool';
25
    const PHP_TYPE_FLOAT = 'float';
26
    const PHP_TYPE_INTEGER = 'int';
27
    const PHP_TYPE_NULL = 'null';
28
    const PHP_TYPE_OBJECT = 'object';
29
    const PHP_TYPE_RESOURCE = 'resource';
30
    const PHP_TYPE_STRING = 'string';
31
32
    /**
33
     * @var string
34
     */
35
    protected $name;
36
37
    /**
38
     * @var string
39
     */
40
    protected $type;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $nullable;
46
47
    /**
48
     * @var string
49
     */
50
    protected $key;
51
52
    /**
53
     * @var string
54
     */
55
    protected $default;
56
57
    /**
58
     * @var string
59
     */
60
    protected $extra;
61
62
    /**
63
     * @param string $name
64
     * @param string $type
65
     * @param bool $nullable
66
     * @param string $key
67
     * @param string $default
68
     * @param null|string $extra
69
     */
70
    public function __construct(
71
        string $name,
72
        string $type,
73
        bool $nullable,
74
        string $key,
75
        string $default,
76
        ?string $extra
77
    ) {
78
        $this->name = $name;
79
        $this->type = $type;
80
        $this->nullable = $nullable;
81
        $this->key = $key;
82
        $this->default = $default;
83
        $this->extra = $extra;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getName(): string
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getPhpName(): string
98
    {
99
        return \ucfirst(\preg_replace_callback(
100
            '/[^a-z0-9]+(?<name>\w{1})/i',
101
            function ($matches) {
102
                return \strtoupper($matches['name']);
103
            },
104
            $this->getName()
105
        ));
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getType(): string
112
    {
113
        return $this->type;
114
    }
115
116
    /**
117
     * @return string
118
     * @throws DmsException
119
     */
120
    public function getPhpType(): string
121
    {
122
        \preg_match('/^(?<type>\w+)/', $this->getType(), $matches);
123
        switch ($matches['type']) {
124
            case static::TYPE_BIGINT:
125
            case static::TYPE_INT:
126
            case static::TYPE_TIMESTAMP:
127
            case static::TYPE_TINYINT:
128
                return static::PHP_TYPE_INTEGER;
129
130
            case static::TYPE_FLOAT:
131
                return static::PHP_TYPE_FLOAT;
132
133
            case static::TYPE_CHAR:
134
            case static::TYPE_DATE:
135
            case static::TYPE_DATETIME:
136
            case static::TYPE_JSON:
137
            case static::TYPE_LONGTEXT:
138
            case static::TYPE_MEDIUMTEXT:
139
            case static::TYPE_TEXT:
140
            case static::TYPE_VARCHAR:
141
                return static::PHP_TYPE_STRING;
142
        }
143
144
        throw new DmsException(\sprintf(
145
            'Could not determine type for column "%s" with type "%s"',
146
            $this->getName(),
147
            $this->getType()
148
        ));
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function isNullable(): bool
155
    {
156
        return $this->nullable;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getKey(): string
163
    {
164
        return $this->key;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getDefault(): string
171
    {
172
        return $this->default;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getPhpDefaultType(): string
179
    {
180
        $default = empty($this->getDefault()) ? null : $this->getDefault();
181
182
        if (null !== $default) {
183
            switch ($this->getPhpType()) {
184
                case static::PHP_TYPE_INTEGER:
185
                    return static::PHP_TYPE_INTEGER;
186
187
                case static::PHP_TYPE_FLOAT:
188
                    return static::PHP_TYPE_FLOAT;
189
190
                case static::PHP_TYPE_STRING:
191
                    return static::PHP_TYPE_STRING;
192
            }
193
        }
194
195
        return \mb_strtolower(\gettype($default));
196
    }
197
198
    /**
199
     * @return null|string
200
     */
201
    public function getExtra(): ?string
202
    {
203
        return $this->extra;
204
    }
205
}
206