Passed
Push — main ( faeaf0...5f5b5b )
by Thierry
36:26 queued 28:38
created

TableFieldEntity::fromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Entity;
4
5
class TableFieldEntity
6
{
7
    /**
8
     * The field name
9
     *
10
     * @var string
11
     */
12
    public $name = '';
13
14
    /**
15
     * The field type
16
     *
17
     * @var string
18
     */
19
    public $type = '';
20
21
    /**
22
     * The field full type
23
     *
24
     * @var string
25
     */
26
    public $fullType = '';
27
28
    /**
29
     * The field sign
30
     *
31
     * @var string
32
     */
33
    public $unsigned = '';
34
35
    /**
36
     * The field length
37
     *
38
     * @var integer
39
     */
40
    public $length = 0;
41
42
    /**
43
     * If the field has a default value
44
     *
45
     * @var boolean
46
     */
47
    public $hasDefault = false;
48
49
    /**
50
     * The field default value
51
     *
52
     * @var mixed
53
     */
54
    public $default = null;
55
56
    /**
57
     * If the field is null
58
     *
59
     * @var boolean
60
     */
61
    public $null = false;
62
63
    /**
64
     * If the field is auto increment
65
     *
66
     * @var boolean
67
     */
68
    public $autoIncrement = false;
69
70
    /**
71
     * The action on update
72
     *
73
     * @var string
74
     */
75
    public $onUpdate = '';
76
77
    /**
78
     * The action on delete
79
     *
80
     * @var string
81
     */
82
    public $onDelete = '';
83
84
    /**
85
     * The field collation
86
     *
87
     * @var string
88
     */
89
    public $collation = '';
90
91
    /**
92
     * The field privileges
93
     *
94
     * @var array
95
     */
96
    public $privileges = [];
97
98
    /**
99
     * The field comment
100
     *
101
     * @var string
102
     */
103
    public $comment = '';
104
105
    /**
106
     * If the field is primary key
107
     *
108
     * @var boolean
109
     */
110
    public $primary = false;
111
112
    /**
113
     * If the field is generated
114
     *
115
     * @var boolean
116
     */
117
    public $generated = false;
118
119
    /**
120
     * The field types
121
     *
122
     * @var array
123
     */
124
    public $types;
125
126
    /**
127
     * If the field length is required
128
     *
129
     * @var boolean
130
     */
131
    public $lengthRequired = false;
132
133
    /**
134
     * If the field collation is hidden
135
     *
136
     * @var boolean
137
     */
138
    public $collationHidden = true;
139
140
    /**
141
     * If the field sign id idden
142
     *
143
     * @var boolean
144
     */
145
    public $unsignedHidden = false;
146
147
    /**
148
     * If the field on update trigger is hidden
149
     *
150
     * @var boolean
151
     */
152
    public $onUpdateHidden = true;
153
154
    /**
155
     * If the field on delete trigger is hidden
156
     *
157
     * @var boolean
158
     */
159
    public $onDeleteHidden = true;
160
161
    /**
162
     * Create an entity from array data
163
     *
164
     * @param array $field
165
     *
166
     * @return TableFieldEntity
167
     */
168
    public static function make(array $field)
169
    {
170
        $entity = new static();
171
172
        $attrs = ['primary', 'length', 'autoIncrement', 'privileges', 'generated', 'lengthRequired',
173
            'collationHidden', 'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden', 'default'];
174
        $strings = ['name', 'type', 'fullType', 'unsigned', 'onUpdate', 'onDelete', 'collation', 'comment'];
175
        foreach ($attrs as $attr) {
176
            if (isset($field[$attr])) {
177
                $entity->$attr = $field[$attr];
178
            }
179
        }
180
        foreach ($strings as $attr) {
181
            if (isset($field[$attr])) {
182
                $entity->$attr = $field[$attr] ?? '';
183
            }
184
        }
185
        $entity->null = isset($field['null']);
186
        if (!empty($field['default'])) {
187
            $entity->hasDefault = true;
188
        }
189
190
        return $entity;
191
    }
192
193
    public static function fromArray(array $field): self
194
    {
195
        $attrs = ['name', 'type', 'fullType', 'unsigned', 'length', 'hasDefault', 'default',
196
            'null', 'autoIncrement', 'onUpdate', 'onDelete', 'collation', 'privileges',
197
            'comment', 'primary', 'generated', 'types', 'lengthRequired', 'collationHidden',
198
            'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden'];
199
        $entity = new static();
200
        foreach ($attrs as $attr) {
201
            $entity->$attr = $field[$attr];
202
        }
203
        return $entity;
204
    }
205
206
    public function toArray(): array
207
    {
208
        $attrs = ['name', 'type', 'fullType', 'unsigned', 'length', 'hasDefault', 'default',
209
            'null', 'autoIncrement', 'onUpdate', 'onDelete', 'collation', 'privileges',
210
            'comment', 'primary', 'generated', 'types', 'lengthRequired', 'collationHidden',
211
            'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden'];
212
        return array_map(function($attr) {
213
            return $this->$attr;
214
        }, $attrs);
215
    }
216
217
    /**
218
     * Check if the values of the field are changed
219
     *
220
     * @param TableFieldEntity $field
221
     *
222
     * @return bool
223
     */
224
    public function changed(TableFieldEntity $field)
225
    {
226
        $attrs = ['type', 'primary', 'autoIncrement', 'unsigned', 'length', 'comment',
227
            'collation', 'generated', 'lengthRequired', 'onUpdate', 'onDelete',
228
            'collationHidden', 'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden'];
229
        foreach ($attrs as $attr) {
230
            if ($field->$attr != $this->$attr) {
231
                return true;
232
            }
233
        }
234
        return false;
235
    }
236
}
237