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
|
|
|
|
12
|
|
|
protected function create(string $table, string $primary) |
13
|
|
|
{ |
14
|
|
|
$this->table = $table; |
15
|
|
|
$this->primary = $primary; |
16
|
|
|
$describe = $this->describe(); |
17
|
|
|
|
18
|
|
|
$this->check_fail(); |
19
|
|
|
|
20
|
|
|
$this->mountData($describe); |
21
|
|
|
$this->full = true; |
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function mountData(array $table): Datamanager |
26
|
|
|
{ |
27
|
|
|
foreach ($table as $column) { |
28
|
|
|
foreach ($column as $propriety => $value) { |
29
|
|
|
$method = "mountTable_{$propriety}"; |
30
|
|
|
$this->$method($column['Field'], $value); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
return $this; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function mountTable_Field(string $field, $value = null) |
37
|
|
|
{ |
38
|
|
|
$this->$field = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function mountTable_Type(string $field, $value = null) |
42
|
|
|
{ |
43
|
|
|
$type = $value; |
44
|
|
|
$maxlength = null; |
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','bigtint','int'])) ? 'int' : $type; |
49
|
|
|
$type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$maxlength = (in_array( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength; |
53
|
|
|
$maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength; |
54
|
|
|
$maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength; |
55
|
|
|
$maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength; |
56
|
|
|
|
57
|
|
|
$this->$field = ['maxlength' => $maxlength]; |
58
|
|
|
$this->$field = ['type' => $type]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function mountTable_Null(string $field, $value = null) |
62
|
|
|
{ |
63
|
|
|
$this->$field = ['null' => ($value === 'YES') ? 1 : 0]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function mountTable_Key(string $field, $value = null) |
67
|
|
|
{ |
68
|
|
|
$this->$field = ['key' => $value]; |
69
|
|
|
$this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function mountTable_Extra(string $field, $value = null) |
73
|
|
|
{ |
74
|
|
|
$this->$field = ['extra' => $value]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function mountTable_Default(string $field, $value = null) |
78
|
|
|
{ |
79
|
|
|
$this->$field = ['default' => $value]; |
80
|
|
|
$this->$field = ['value' => null]; |
81
|
|
|
$this->$field = ['changed' => false]; |
82
|
|
|
$this->select[$field] = true; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |