1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
4
|
|
|
|
5
|
|
|
trait SynchronizeTrait{ |
6
|
|
|
use CrudTrait; |
7
|
|
|
|
8
|
|
|
protected ?string $table = null; |
9
|
|
|
protected ?string $primary = null; |
10
|
|
|
protected bool $full = false; |
11
|
|
|
protected static ?array $describe = null; |
12
|
|
|
|
13
|
|
|
protected function synchronize(string $table, ?string $primary = null) |
14
|
|
|
{ |
15
|
|
|
$this->table = $table; |
16
|
|
|
$this->primary = $primary; |
17
|
|
|
self::$describe[$table] ??= $this->describe(); |
18
|
|
|
|
19
|
|
|
$this->check_fail(); |
20
|
|
|
|
21
|
|
|
$this->mountData(self::$describe[$table]); |
22
|
|
|
$this->full = true; |
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function mountData(array $table): Datamanager |
27
|
|
|
{ |
28
|
|
|
foreach ($table as $column) { |
29
|
|
|
foreach ($column as $propriety => $value) { |
30
|
|
|
$method = "mountTable_{$propriety}"; |
31
|
|
|
$this->$method($column['Field'], $value); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function mountTable_Field(string $field, $value = null) |
38
|
|
|
{ |
39
|
|
|
$this->$field = null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function mountTable_Type(string $field, $value = null) |
43
|
|
|
{ |
44
|
|
|
$type = $value; |
45
|
|
|
|
46
|
|
|
if(strpos($value,'(')){ |
47
|
|
|
$type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type; |
48
|
|
|
$type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigint','int'])) ? 'int' : $type; |
49
|
|
|
$type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->mountTable_Maxlength($field, $type, $value); |
53
|
|
|
$this->$field = ['type' => $type]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function mountTable_Maxlength(string $field, string $type, $default = null) |
57
|
|
|
{ |
58
|
|
|
$maxlength = (in_array( $type , ['string','float','int'])) ? substr($default,(strpos($default,'(')+1),-1) : 0; |
59
|
|
|
$maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength; |
60
|
|
|
$maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength; |
61
|
|
|
$maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength; |
62
|
|
|
$this->$field = ['maxlength' => $maxlength]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function mountTable_Null(string $field, $value = null) |
66
|
|
|
{ |
67
|
|
|
$this->$field = ['null' => ($value === 'YES') ? 1 : 0]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function mountTable_Key(string $field, $value = null) |
71
|
|
|
{ |
72
|
|
|
$this->$field = ['key' => $value]; |
73
|
|
|
$this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function mountTable_Extra(string $field, $value = null) |
77
|
|
|
{ |
78
|
|
|
$this->$field = ['extra' => $value]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function mountTable_Default(string $field, $value = null) |
82
|
|
|
{ |
83
|
|
|
$this->$field = ['default' => $value]; |
84
|
|
|
$this->$field = ['value' => null]; |
85
|
|
|
$this->$field = ['changed' => false]; |
86
|
|
|
$this->select[$field] = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|