TableFieldEntity::make()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 4
b 0
f 0
nc 8
nop 1
dl 0
loc 16
rs 9.2222
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
     * The field status when the table is edited
163
     *
164
     * @var string
165
     */
166
    public $editStatus = 'unchanged';
167
168
    /**
169
     * The field position in the edit form
170
     *
171
     * @var string
172
     */
173
    public $editPosition = 0;
174
175
    /**
176
     * The entity attributes
177
     *
178
     * @var array
179
     */
180
    private static $attrs = ['name', 'type', 'fullType', 'primary', 'null', 'length',
181
        'unsigned', 'hasDefault', 'default', 'autoIncrement', 'collation', 'comment',
182
        'collationHidden', 'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden',
183
        'generated', 'lengthRequired', 'onUpdate', 'onDelete', 'editStatus', 'editPosition'];
184
185
    /**
186
     * The entity attributes
187
     *
188
     * @var array
189
     */
190
    private static $fields = ['name', 'type', 'primary', 'autoIncrement', 'unsigned',
191
        'length', 'comment', 'collation', 'generated', 'lengthRequired', 'onUpdate',
192
        'onDelete', 'collationHidden', 'unsignedHidden', 'onUpdateHidden', 'onDeleteHidden'];
193
194
    /**
195
     * Create an entity from database data
196
     *
197
     * @param array $field
198
     *
199
     * @return TableFieldEntity
200
     */
201
    public static function make(array $field): self
202
    {
203
        $entity = new static();
204
        $entity->null = isset($field['null']);
205
        if (!empty($field['default'])) {
206
            $entity->hasDefault = true;
207
        }
208
        foreach (self::$attrs as $attr) {
209
            if ($attr !== 'null' && $attr !== 'hasDefault') {
210
                // Attributes are set only when present.
211
                if (isset($field[$attr])) {
212
                    $entity->$attr = $field[$attr];
213
                }
214
            }
215
        }
216
        return $entity;
217
    }
218
219
    /**
220
     * Create an entity from js app data
221
     *
222
     * @param array $field
223
     *
224
     * @return TableFieldEntity
225
     */
226
    public static function fromArray(array $field): self
227
    {
228
        $entity = new static();
229
        foreach (self::$attrs as $attr) {
230
            $entity->$attr = $field[$attr];
231
        }
232
        return $entity;
233
    }
234
235
    /**
236
     * @return array
237
     */
238
    public function toArray(): array
239
    {
240
        $array = [];
241
        foreach (self::$attrs as $attr) {
242
            $array[$attr] = $this->$attr;
243
        }
244
        return $array;
245
    }
246
247
    /**
248
     * @param array $values
249
     *
250
     * @return TableFieldEntity
251
     */
252
    public function update(array $values): self
253
    {
254
        foreach (self::$fields as $attr) {
255
            isset($values[$attr]) && $this->$attr = $values[$attr];
256
        }
257
        return $this;
258
    }
259
260
    /**
261
     * @param array $values
262
     *
263
     * @return bool
264
     */
265
    public function edited(array $values): bool
266
    {
267
        foreach (self::$fields as $attr) {
268
            if (isset($values[$attr]) && $this->$attr != $values[$attr]) {
269
                return true;
270
            }
271
        }
272
        return false;
273
    }
274
275
    /**
276
     * @param array $values
277
     *
278
     * @return TableFieldEntity
279
     */
280
    public function updateStatus(array $values): self
281
    {
282
        $this->editStatus = $this->edited($values) ? 'edited' : 'unchanged';
283
        return $this;
284
    }
285
286
    /**
287
     * Check if the values in two fields are equals
288
     *
289
     * @param TableFieldEntity $field
290
     *
291
     * @return bool
292
     */
293
    public function equals(TableFieldEntity $field)
294
    {
295
        foreach (self::$fields as $attr) {
296
            if ($field->$attr != $this->$attr) {
297
                return false;
298
            }
299
        }
300
        return true;
301
    }
302
}
303