@@ -4,36 +4,36 @@ discard block |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Datamanager\DatamanagerException; |
6 | 6 | |
7 | -trait CheckTrait{ |
|
7 | +trait CheckTrait { |
|
8 | 8 | |
9 | 9 | protected function check_where_array(array $where) |
10 | 10 | { |
11 | - if(count($where) != 3){ |
|
12 | - throw new DatamanagerException("Condition where set incorrectly: ".implode(' ',$where)); |
|
11 | + if (count($where) != 3) { |
|
12 | + throw new DatamanagerException("Condition where set incorrectly: ".implode(' ', $where)); |
|
13 | 13 | } |
14 | 14 | |
15 | - if(!array_key_exists($where[0],$this->data) && $this->full){ |
|
15 | + if (!array_key_exists($where[0], $this->data) && $this->full) { |
|
16 | 16 | throw new DatamanagerException("{$where[0]} field does not exist in the table {$this->table}."); |
17 | 17 | } |
18 | 18 | } |
19 | 19 | |
20 | 20 | protected function isSettable(string $prop) |
21 | 21 | { |
22 | - if($this->full && !array_key_exists($prop,$this->data)){ |
|
22 | + if ($this->full && !array_key_exists($prop, $this->data)) { |
|
23 | 23 | throw new DatamanagerException("{$prop} field does not exist in the table {$this->table}."); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | 27 | protected function checkLimit() |
28 | 28 | { |
29 | - if(is_null($this->limit)){ |
|
29 | + if (is_null($this->limit)) { |
|
30 | 30 | throw new DatamanagerException("The limit must be set before the offset."); |
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | - protected function checkMaxlength(string $field, $val , $max) |
|
34 | + protected function checkMaxlength(string $field, $val, $max) |
|
35 | 35 | { |
36 | - if(strlen($val) > $max){ |
|
36 | + if (strlen($val) > $max) { |
|
37 | 37 | throw new DatamanagerException("The information provided for column {$field} of table {$this->table} exceeded that allowed."); |
38 | 38 | } |
39 | 39 | } |
@@ -45,18 +45,18 @@ discard block |
||
45 | 45 | |
46 | 46 | protected function isIncremented(string $field): bool |
47 | 47 | { |
48 | - return ( strstr($this->data[$field]['extra'],'auto_increment') && $field === $this->primary ); |
|
48 | + return (strstr($this->data[$field]['extra'], 'auto_increment') && $field === $this->primary); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | protected function checkForChanges(): bool |
52 | 52 | { |
53 | 53 | $hasChanges = false; |
54 | - foreach($this->data as $data){ |
|
55 | - if($data['changed']){ |
|
54 | + foreach ($this->data as $data) { |
|
55 | + if ($data['changed']) { |
|
56 | 56 | $hasChanges = true; |
57 | 57 | } |
58 | 58 | } |
59 | - if(!$hasChanges){ |
|
59 | + if (!$hasChanges) { |
|
60 | 60 | throw new DatamanagerException('There were no changes to be saved in the database.'); |
61 | 61 | } |
62 | 62 | return true; |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | |
65 | 65 | protected function checkUniques($data) |
66 | 66 | { |
67 | - foreach($this->data as $d => $dd){ |
|
68 | - if($dd['key'] === 'UNI'){ |
|
69 | - $exist = $this->find()->where([$d,'=',$data[$d]])->only('id')->execute()->getCount(); |
|
70 | - if($exist > 0){ |
|
67 | + foreach ($this->data as $d => $dd) { |
|
68 | + if ($dd['key'] === 'UNI') { |
|
69 | + $exist = $this->find()->where([$d, '=', $data[$d]])->only('id')->execute()->getCount(); |
|
70 | + if ($exist > 0) { |
|
71 | 71 | throw new DatamanagerException("A record with the same {$this->getField($d)} already exists."); |
72 | 72 | } |
73 | 73 | } |
@@ -5,12 +5,12 @@ |
||
5 | 5 | use HnrAzevedo\Datamanager\Model as Entity; |
6 | 6 | |
7 | 7 | /** |
8 | - * @property string $name |
|
9 | - * @property string $email |
|
10 | - * @property string $password |
|
11 | - * @property string birth |
|
12 | - * @property string register |
|
13 | - */ |
|
8 | + * @property string $name |
|
9 | + * @property string $email |
|
10 | + * @property string $password |
|
11 | + * @property string birth |
|
12 | + * @property string register |
|
13 | + */ |
|
14 | 14 | class User extends Entity{ |
15 | 15 | |
16 | 16 | public function __construct() |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | * @property string birth |
12 | 12 | * @property string register |
13 | 13 | */ |
14 | -class User extends Entity{ |
|
14 | +class User extends Entity { |
|
15 | 15 | |
16 | 16 | public function __construct() |
17 | 17 | { |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * @param string Table name |
24 | 24 | * @param string Primary key column |
25 | 25 | */ |
26 | - parent::create('user','id'); |
|
26 | + parent::create('user', 'id'); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -2,20 +2,20 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Datamanager; |
4 | 4 | |
5 | -trait MagicsTrait{ |
|
5 | +trait MagicsTrait { |
|
6 | 6 | |
7 | - public function __set(string $prop,$value) |
|
7 | + public function __set(string $prop, $value) |
|
8 | 8 | { |
9 | - if(is_array($value)){ |
|
9 | + if (is_array($value)) { |
|
10 | 10 | $attr = array_keys($value)[0]; |
11 | 11 | $this->data[$prop][$attr] = $value[$attr]; |
12 | 12 | return $this; |
13 | 13 | } |
14 | 14 | |
15 | - if($this->full){ |
|
16 | - switch($this->data[$prop]['type']){ |
|
15 | + if ($this->full) { |
|
16 | + switch ($this->data[$prop]['type']) { |
|
17 | 17 | case 'date': |
18 | - $value = (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value) , 'Y-m-d')); |
|
18 | + $value = (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value), 'Y-m-d')); |
|
19 | 19 | break; |
20 | 20 | } |
21 | 21 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | public function getVars(): array |
32 | 32 | { |
33 | 33 | $vars = []; |
34 | - foreach($this->data as $var => $value){ |
|
34 | + foreach ($this->data as $var => $value) { |
|
35 | 35 | $vars[$var] = null; |
36 | 36 | } |
37 | 37 | return $vars; |
@@ -41,13 +41,13 @@ discard block |
||
41 | 41 | { |
42 | 42 | $this->isSettable($field); |
43 | 43 | |
44 | - if($this->full){ |
|
45 | - switch($this->data[$field]['type']){ |
|
44 | + if ($this->full) { |
|
45 | + switch ($this->data[$field]['type']) { |
|
46 | 46 | case 'date': |
47 | - return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['dateformat'])) : null ; |
|
47 | + return (!empty($this->data[$field]['value'])) ? (@date_format(@date_create_from_format('Y-m-d', $this->data[$field]['value']), DATAMANAGER_CONFIG['dateformat'])) : null; |
|
48 | 48 | break; |
49 | 49 | case 'datetime': |
50 | - return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d H:i:s' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['datetimeformat'])) : null ; |
|
50 | + return (!empty($this->data[$field]['value'])) ? (@date_format(@date_create_from_format('Y-m-d H:i:s', $this->data[$field]['value']), DATAMANAGER_CONFIG['datetimeformat'])) : null; |
|
51 | 51 | break; |
52 | 52 | } |
53 | 53 | } |
@@ -2,13 +2,13 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Datamanager; |
4 | 4 | |
5 | -trait SynchronizeTrait{ |
|
5 | +trait SynchronizeTrait { |
|
6 | 6 | use CrudTrait; |
7 | 7 | |
8 | 8 | protected ?string $table = null; |
9 | 9 | protected ?string $primary = null; |
10 | 10 | protected bool $full = false; |
11 | - protected static ?array $describe = null; |
|
11 | + protected static ? array $describe = null; |
|
12 | 12 | |
13 | 13 | protected function synchronize(string $table, ?string $primary = null) |
14 | 14 | { |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | { |
44 | 44 | $type = $value; |
45 | 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; |
|
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 | 50 | } |
51 | 51 | |
52 | 52 | $this->mountTable_Maxlength($field, $type, $value); |
@@ -55,10 +55,10 @@ discard block |
||
55 | 55 | |
56 | 56 | protected function mountTable_Maxlength(string $field, string $type, $default = null) |
57 | 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; |
|
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 | 62 | $this->$field = ['maxlength' => $maxlength]; |
63 | 63 | } |
64 | 64 |