@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | public function query_code($sql_statement) : string |
| 33 | 33 | { |
| 34 | - $first_five = strtolower(substr($sql_statement, 0,6)); |
|
| 34 | + $first_five = strtolower(substr($sql_statement, 0, 6)); |
|
| 35 | 35 | |
| 36 | - if(!isset(self::$query_codes[$first_five])) |
|
| 36 | + if (!isset(self::$query_codes[$first_five])) |
|
| 37 | 37 | throw new \Exception('KADRO_ONLY_TRACES_CRUD'); |
| 38 | 38 | |
| 39 | 39 | return self::$query_codes[$first_five]; |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | $trace['query_id'] = $model_id; |
| 48 | 48 | $trace['query_by'] = $operator_id; |
| 49 | 49 | |
| 50 | - try{ |
|
| 50 | + try { |
|
| 51 | 51 | $this->tracing_table()->connection()->transact(); |
| 52 | 52 | $query = $this->tracing_table()->insert($trace)->run(); |
| 53 | 53 | |
| 54 | 54 | // if we delete a record, we remove all traces of update |
| 55 | - if($query->is_success() && $trace['query_type'] === self::CODE_DELETE) |
|
| 55 | + if ($query->is_success() && $trace['query_type'] === self::CODE_DELETE) |
|
| 56 | 56 | { |
| 57 | 57 | $trace['query_type'] = self::CODE_UPDATE; |
| 58 | 58 | unset($trace['query_by']); |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | $this->tracing_table()->connection()->commit(); |
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | - catch(\Exception $e) |
|
| 64 | + catch (\Exception $e) |
|
| 65 | 65 | { |
| 66 | 66 | $this->tracing_table()->connection()->rollback(); |
| 67 | 67 | return false; |
@@ -87,16 +87,16 @@ discard block |
||
| 87 | 87 | return $this->traces(['id' => $m->get_id(), 'table' => get_class($m)::table_name()]); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - public function traces($options=[]) |
|
| 90 | + public function traces($options = []) |
|
| 91 | 91 | { |
| 92 | 92 | |
| 93 | - if(!isset($options['limit']) || empty($options['limit'])) |
|
| 93 | + if (!isset($options['limit']) || empty($options['limit'])) |
|
| 94 | 94 | $limit = 1000; |
| 95 | 95 | else |
| 96 | 96 | $limit = intval($options['limit']); |
| 97 | 97 | |
| 98 | 98 | // TODO SELECT field order can't change without adapting the result parsing code (foreach $res) |
| 99 | - $select_fields = ['SUBSTR(query_on, 1, 10) AS working_day', 'query_table', 'query_id', 'GROUP_CONCAT(DISTINCT query_type, "-", query_by) as action_by']; |
|
| 99 | + $select_fields = ['SUBSTR(query_on, 1, 10) AS working_day', 'query_table', 'query_id', 'GROUP_CONCAT(DISTINCT query_type, "-", query_by) as action_by']; |
|
| 100 | 100 | $q = $this->tracing_table()->select($select_fields); |
| 101 | 101 | $q->order_by(['', 'working_day', 'DESC']); |
| 102 | 102 | $q->order_by([$this->tracing_table()->name(), 'query_table', 'DESC']); |
@@ -108,21 +108,21 @@ discard block |
||
| 108 | 108 | $q->having("action_by NOT LIKE '%D%'"); |
| 109 | 109 | $q->limit($limit); |
| 110 | 110 | |
| 111 | - foreach($options as $o => $v) |
|
| 111 | + foreach ($options as $o => $v) |
|
| 112 | 112 | { |
| 113 | - if(preg_match('/id/', $o)) $q->aw_eq('query_id', $v); |
|
| 114 | - elseif(preg_match('/tables/', $o)) $q->aw_string_in('query_table', is_array($v) ? $v : [$v]); |
|
| 115 | - elseif(preg_match('/table/', $o)) $q->aw_eq('query_table', $v); |
|
| 116 | - elseif(preg_match('/(type|action)/', $o)) $q->aw_string_in('query_type', is_array($v) ? $v : [$v]); |
|
| 117 | - elseif(preg_match('/(date|query_on)/', $o)) $q->aw_like('query_on', "$v%"); |
|
| 118 | - elseif(preg_match('/(oper|user|query_by)/', $o)) $q->aw_eq('query_by', $v); |
|
| 113 | + if (preg_match('/id/', $o)) $q->aw_eq('query_id', $v); |
|
| 114 | + elseif (preg_match('/tables/', $o)) $q->aw_string_in('query_table', is_array($v) ? $v : [$v]); |
|
| 115 | + elseif (preg_match('/table/', $o)) $q->aw_eq('query_table', $v); |
|
| 116 | + elseif (preg_match('/(type|action)/', $o)) $q->aw_string_in('query_type', is_array($v) ? $v : [$v]); |
|
| 117 | + elseif (preg_match('/(date|query_on)/', $o)) $q->aw_like('query_on', "$v%"); |
|
| 118 | + elseif (preg_match('/(oper|user|query_by)/', $o)) $q->aw_eq('query_by', $v); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // vd($q); |
| 122 | - try{ |
|
| 122 | + try { |
|
| 123 | 123 | $q->run(); |
| 124 | 124 | } |
| 125 | - catch(\Exception $e) |
|
| 125 | + catch (\Exception $e) |
|
| 126 | 126 | { |
| 127 | 127 | return false; |
| 128 | 128 | } |
@@ -131,13 +131,13 @@ discard block |
||
| 131 | 131 | // ddt($res); |
| 132 | 132 | $ret = []; |
| 133 | 133 | |
| 134 | - foreach($res as $r) |
|
| 134 | + foreach ($res as $r) |
|
| 135 | 135 | { |
| 136 | 136 | list($working_day, $class, $instance_id, $logs) = $r; |
| 137 | 137 | |
| 138 | - if(!isset($ret[$working_day])) |
|
| 138 | + if (!isset($ret[$working_day])) |
|
| 139 | 139 | $ret[$working_day] = []; |
| 140 | - if(!isset($ret[$working_day][$class])) |
|
| 140 | + if (!isset($ret[$working_day][$class])) |
|
| 141 | 141 | $ret[$working_day][$class] = []; |
| 142 | 142 | |
| 143 | 143 | $ret[$working_day][$class][$instance_id] = $logs; |
@@ -33,8 +33,9 @@ discard block |
||
| 33 | 33 | { |
| 34 | 34 | $first_five = strtolower(substr($sql_statement, 0,6)); |
| 35 | 35 | |
| 36 | - if(!isset(self::$query_codes[$first_five])) |
|
| 37 | - throw new \Exception('KADRO_ONLY_TRACES_CRUD'); |
|
| 36 | + if(!isset(self::$query_codes[$first_five])) { |
|
| 37 | + throw new \Exception('KADRO_ONLY_TRACES_CRUD'); |
|
| 38 | + } |
|
| 38 | 39 | |
| 39 | 40 | return self::$query_codes[$first_five]; |
| 40 | 41 | } |
@@ -60,8 +61,7 @@ discard block |
||
| 60 | 61 | } |
| 61 | 62 | $this->tracing_table()->connection()->commit(); |
| 62 | 63 | return true; |
| 63 | - } |
|
| 64 | - catch(\Exception $e) |
|
| 64 | + } catch(\Exception $e) |
|
| 65 | 65 | { |
| 66 | 66 | $this->tracing_table()->connection()->rollback(); |
| 67 | 67 | return false; |
@@ -90,10 +90,11 @@ discard block |
||
| 90 | 90 | public function traces($options=[]) |
| 91 | 91 | { |
| 92 | 92 | |
| 93 | - if(!isset($options['limit']) || empty($options['limit'])) |
|
| 94 | - $limit = 1000; |
|
| 95 | - else |
|
| 96 | - $limit = intval($options['limit']); |
|
| 93 | + if(!isset($options['limit']) || empty($options['limit'])) { |
|
| 94 | + $limit = 1000; |
|
| 95 | + } else { |
|
| 96 | + $limit = intval($options['limit']); |
|
| 97 | + } |
|
| 97 | 98 | |
| 98 | 99 | // TODO SELECT field order can't change without adapting the result parsing code (foreach $res) |
| 99 | 100 | $select_fields = ['SUBSTR(query_on, 1, 10) AS working_day', 'query_table', 'query_id', 'GROUP_CONCAT(DISTINCT query_type, "-", query_by) as action_by']; |
@@ -110,19 +111,25 @@ discard block |
||
| 110 | 111 | |
| 111 | 112 | foreach($options as $o => $v) |
| 112 | 113 | { |
| 113 | - if(preg_match('/id/', $o)) $q->aw_eq('query_id', $v); |
|
| 114 | - elseif(preg_match('/tables/', $o)) $q->aw_string_in('query_table', is_array($v) ? $v : [$v]); |
|
| 115 | - elseif(preg_match('/table/', $o)) $q->aw_eq('query_table', $v); |
|
| 116 | - elseif(preg_match('/(type|action)/', $o)) $q->aw_string_in('query_type', is_array($v) ? $v : [$v]); |
|
| 117 | - elseif(preg_match('/(date|query_on)/', $o)) $q->aw_like('query_on', "$v%"); |
|
| 118 | - elseif(preg_match('/(oper|user|query_by)/', $o)) $q->aw_eq('query_by', $v); |
|
| 114 | + if(preg_match('/id/', $o)) { |
|
| 115 | + $q->aw_eq('query_id', $v); |
|
| 116 | + } elseif(preg_match('/tables/', $o)) { |
|
| 117 | + $q->aw_string_in('query_table', is_array($v) ? $v : [$v]); |
|
| 118 | + } elseif(preg_match('/table/', $o)) { |
|
| 119 | + $q->aw_eq('query_table', $v); |
|
| 120 | + } elseif(preg_match('/(type|action)/', $o)) { |
|
| 121 | + $q->aw_string_in('query_type', is_array($v) ? $v : [$v]); |
|
| 122 | + } elseif(preg_match('/(date|query_on)/', $o)) { |
|
| 123 | + $q->aw_like('query_on', "$v%"); |
|
| 124 | + } elseif(preg_match('/(oper|user|query_by)/', $o)) { |
|
| 125 | + $q->aw_eq('query_by', $v); |
|
| 126 | + } |
|
| 119 | 127 | } |
| 120 | 128 | |
| 121 | 129 | // vd($q); |
| 122 | 130 | try{ |
| 123 | 131 | $q->run(); |
| 124 | - } |
|
| 125 | - catch(\Exception $e) |
|
| 132 | + } catch(\Exception $e) |
|
| 126 | 133 | { |
| 127 | 134 | return false; |
| 128 | 135 | } |
@@ -135,10 +142,12 @@ discard block |
||
| 135 | 142 | { |
| 136 | 143 | list($working_day, $class, $instance_id, $logs) = $r; |
| 137 | 144 | |
| 138 | - if(!isset($ret[$working_day])) |
|
| 139 | - $ret[$working_day] = []; |
|
| 140 | - if(!isset($ret[$working_day][$class])) |
|
| 141 | - $ret[$working_day][$class] = []; |
|
| 145 | + if(!isset($ret[$working_day])) { |
|
| 146 | + $ret[$working_day] = []; |
|
| 147 | + } |
|
| 148 | + if(!isset($ret[$working_day][$class])) { |
|
| 149 | + $ret[$working_day][$class] = []; |
|
| 150 | + } |
|
| 142 | 151 | |
| 143 | 152 | $ret[$working_day][$class][$instance_id] = $logs; |
| 144 | 153 | } |
@@ -1,10 +1,10 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Crudités, it's a cup of carrots sticks (but are they organic ?) |
|
| 4 | - * Codd's Relational model, Unicity, Definitions, Introspection, Tests, Execution & Sets |
|
| 5 | - * Create - Retrieve - Update - Delete |
|
| 6 | - * API for writing and running SQL queries |
|
| 7 | - */ |
|
| 3 | + * Crudités, it's a cup of carrots sticks (but are they organic ?) |
|
| 4 | + * Codd's Relational model, Unicity, Definitions, Introspection, Tests, Execution & Sets |
|
| 5 | + * Create - Retrieve - Update - Delete |
|
| 6 | + * API for writing and running SQL queries |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | namespace HexMakina\Crudites; |
| 10 | 10 | |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | namespace HexMakina\Crudites; |
| 10 | 10 | |
| 11 | -use \HexMakina\Crudites\Queries\{BaseQuery,Select}; |
|
| 11 | +use \HexMakina\Crudites\Queries\{BaseQuery, Select}; |
|
| 12 | 12 | use \HexMakina\Crudites\Interfaces\DatabaseInterface; |
| 13 | 13 | use \HexMakina\Crudites\CruditesException; |
| 14 | 14 | |
@@ -23,31 +23,31 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | public static function inspect($table_name) |
| 25 | 25 | { |
| 26 | - if(is_null(self::$database)) |
|
| 26 | + if (is_null(self::$database)) |
|
| 27 | 27 | throw new CruditesException('NO_DATABASE'); |
| 28 | 28 | |
| 29 | 29 | try |
| 30 | 30 | { |
| 31 | 31 | return self::$database->inspect($table_name); |
| 32 | 32 | } |
| 33 | - catch(\Exception $e) |
|
| 33 | + catch (\Exception $e) |
|
| 34 | 34 | { |
| 35 | 35 | throw new CruditesException('TABLE_INTROSPECTION'); |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public static function connect($props=null) |
|
| 39 | + public static function connect($props = null) |
|
| 40 | 40 | { |
| 41 | 41 | // no props, means connection already exists, verify and return |
| 42 | - if(!isset($props['host'],$props['port'],$props['name'],$props['char'],$props['user'],$props['pass'])) |
|
| 42 | + if (!isset($props['host'], $props['port'], $props['name'], $props['char'], $props['user'], $props['pass'])) |
|
| 43 | 43 | { |
| 44 | - if(is_null(self::$database)) |
|
| 44 | + if (is_null(self::$database)) |
|
| 45 | 45 | throw new CruditesException('CONNECTION_MISSING'); |
| 46 | 46 | |
| 47 | 47 | return self::$database->contentConnection(); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - $conx = new Connection($props['host'],$props['port'],$props['name'],$props['char'],$props['user'],$props['pass']); |
|
| 50 | + $conx = new Connection($props['host'], $props['port'], $props['name'], $props['char'], $props['user'], $props['pass']); |
|
| 51 | 51 | return $conx; |
| 52 | 52 | } |
| 53 | 53 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | { |
| 58 | 58 | $Query->select_also(['COUNT(*) as count']); |
| 59 | 59 | $res = $Query->ret_col(); |
| 60 | - if(is_array($res)) |
|
| 60 | + if (is_array($res)) |
|
| 61 | 61 | return intval(current($res)); |
| 62 | 62 | return null; |
| 63 | 63 | } |
@@ -69,19 +69,19 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | $ret = []; |
| 71 | 71 | |
| 72 | - if($Query->run()->is_success()) |
|
| 72 | + if ($Query->run()->is_success()) |
|
| 73 | 73 | { |
| 74 | - foreach($Query->ret_ass() as $rec) |
|
| 74 | + foreach ($Query->ret_ass() as $rec) |
|
| 75 | 75 | $ret[$rec[$pk_name]] = $rec; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | return $ret; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - public static function raw($sql, $dat_ass=[]) |
|
| 81 | + public static function raw($sql, $dat_ass = []) |
|
| 82 | 82 | { |
| 83 | 83 | $conx = self::connect(); |
| 84 | - if(empty($dat_ass)) |
|
| 84 | + if (empty($dat_ass)) |
|
| 85 | 85 | { |
| 86 | 86 | $res = $conx->query($sql); |
| 87 | 87 | //TODO query | alter ! |
@@ -97,16 +97,16 @@ discard block |
||
| 97 | 97 | return $res; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - public static function distinct_for($table, $column_name, $filter_by_value=null) |
|
| 100 | + public static function distinct_for($table, $column_name, $filter_by_value = null) |
|
| 101 | 101 | { |
| 102 | 102 | $table = self::table_name_to_Table($table); |
| 103 | 103 | |
| 104 | - if(is_null($table->column($column_name))) |
|
| 104 | + if (is_null($table->column($column_name))) |
|
| 105 | 105 | throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
| 106 | 106 | |
| 107 | 107 | $Query = $table->select(["DISTINCT `$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']); |
| 108 | 108 | |
| 109 | - if(!is_null($filter_by_value)) |
|
| 109 | + if (!is_null($filter_by_value)) |
|
| 110 | 110 | $Query->aw_like($column_name, "%$filter_by_value%"); |
| 111 | 111 | |
| 112 | 112 | $Query->order_by($column_name, 'DESC'); |
@@ -114,16 +114,16 @@ discard block |
||
| 114 | 114 | return $Query->ret_col(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - public static function distinct_for_with_id($table, $column_name, $filter_by_value=null) |
|
| 117 | + public static function distinct_for_with_id($table, $column_name, $filter_by_value = null) |
|
| 118 | 118 | { |
| 119 | 119 | $table = self::table_name_to_Table($table); |
| 120 | 120 | |
| 121 | - if(is_null($table->column($column_name))) |
|
| 121 | + if (is_null($table->column($column_name))) |
|
| 122 | 122 | throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
| 123 | 123 | |
| 124 | 124 | $Query = $table->select(["DISTINCT `id`,`$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']); |
| 125 | 125 | |
| 126 | - if(!is_null($filter_by_value)) |
|
| 126 | + if (!is_null($filter_by_value)) |
|
| 127 | 127 | $Query->aw_like($column_name, "%$filter_by_value%"); |
| 128 | 128 | |
| 129 | 129 | return $Query->ret_par(); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | |
| 138 | 138 | $table = self::table_name_to_Table($table); |
| 139 | 139 | |
| 140 | - if(is_null($column = $table->column($boolean_column_name)) || !$column->type()->is_boolean()) |
|
| 140 | + if (is_null($column = $table->column($boolean_column_name)) || !$column->type()->is_boolean()) |
|
| 141 | 141 | return false; |
| 142 | 142 | |
| 143 | 143 | // TODO: still using 'id' instead of table->primaries |
@@ -23,14 +23,14 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | public static function inspect($table_name) |
| 25 | 25 | { |
| 26 | - if(is_null(self::$database)) |
|
| 27 | - throw new CruditesException('NO_DATABASE'); |
|
| 26 | + if(is_null(self::$database)) { |
|
| 27 | + throw new CruditesException('NO_DATABASE'); |
|
| 28 | + } |
|
| 28 | 29 | |
| 29 | 30 | try |
| 30 | 31 | { |
| 31 | 32 | return self::$database->inspect($table_name); |
| 32 | - } |
|
| 33 | - catch(\Exception $e) |
|
| 33 | + } catch(\Exception $e) |
|
| 34 | 34 | { |
| 35 | 35 | throw new CruditesException('TABLE_INTROSPECTION'); |
| 36 | 36 | } |
@@ -41,8 +41,9 @@ discard block |
||
| 41 | 41 | // no props, means connection already exists, verify and return |
| 42 | 42 | if(!isset($props['host'],$props['port'],$props['name'],$props['char'],$props['user'],$props['pass'])) |
| 43 | 43 | { |
| 44 | - if(is_null(self::$database)) |
|
| 45 | - throw new CruditesException('CONNECTION_MISSING'); |
|
| 44 | + if(is_null(self::$database)) { |
|
| 45 | + throw new CruditesException('CONNECTION_MISSING'); |
|
| 46 | + } |
|
| 46 | 47 | |
| 47 | 48 | return self::$database->contentConnection(); |
| 48 | 49 | } |
@@ -57,8 +58,9 @@ discard block |
||
| 57 | 58 | { |
| 58 | 59 | $Query->select_also(['COUNT(*) as count']); |
| 59 | 60 | $res = $Query->ret_col(); |
| 60 | - if(is_array($res)) |
|
| 61 | - return intval(current($res)); |
|
| 61 | + if(is_array($res)) { |
|
| 62 | + return intval(current($res)); |
|
| 63 | + } |
|
| 62 | 64 | return null; |
| 63 | 65 | } |
| 64 | 66 | |
@@ -71,8 +73,9 @@ discard block |
||
| 71 | 73 | |
| 72 | 74 | if($Query->run()->is_success()) |
| 73 | 75 | { |
| 74 | - foreach($Query->ret_ass() as $rec) |
|
| 75 | - $ret[$rec[$pk_name]] = $rec; |
|
| 76 | + foreach($Query->ret_ass() as $rec) { |
|
| 77 | + $ret[$rec[$pk_name]] = $rec; |
|
| 78 | + } |
|
| 76 | 79 | } |
| 77 | 80 | |
| 78 | 81 | return $ret; |
@@ -87,9 +90,7 @@ discard block |
||
| 87 | 90 | //TODO query | alter ! |
| 88 | 91 | //$res = $conx->alter($sql); |
| 89 | 92 | |
| 90 | - } |
|
| 91 | - |
|
| 92 | - else |
|
| 93 | + } else |
|
| 93 | 94 | { |
| 94 | 95 | $stmt = $conx->prepare($sql); |
| 95 | 96 | $res = $stmt->execute($dat_ass); |
@@ -101,13 +102,15 @@ discard block |
||
| 101 | 102 | { |
| 102 | 103 | $table = self::table_name_to_Table($table); |
| 103 | 104 | |
| 104 | - if(is_null($table->column($column_name))) |
|
| 105 | - throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
|
| 105 | + if(is_null($table->column($column_name))) { |
|
| 106 | + throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
|
| 107 | + } |
|
| 106 | 108 | |
| 107 | 109 | $Query = $table->select(["DISTINCT `$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']); |
| 108 | 110 | |
| 109 | - if(!is_null($filter_by_value)) |
|
| 110 | - $Query->aw_like($column_name, "%$filter_by_value%"); |
|
| 111 | + if(!is_null($filter_by_value)) { |
|
| 112 | + $Query->aw_like($column_name, "%$filter_by_value%"); |
|
| 113 | + } |
|
| 111 | 114 | |
| 112 | 115 | $Query->order_by($column_name, 'DESC'); |
| 113 | 116 | // ddt($Query); |
@@ -118,13 +121,15 @@ discard block |
||
| 118 | 121 | { |
| 119 | 122 | $table = self::table_name_to_Table($table); |
| 120 | 123 | |
| 121 | - if(is_null($table->column($column_name))) |
|
| 122 | - throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
|
| 124 | + if(is_null($table->column($column_name))) { |
|
| 125 | + throw new CruditesException('TABLE_REQUIRES_COLUMN'); |
|
| 126 | + } |
|
| 123 | 127 | |
| 124 | 128 | $Query = $table->select(["DISTINCT `id`,`$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']); |
| 125 | 129 | |
| 126 | - if(!is_null($filter_by_value)) |
|
| 127 | - $Query->aw_like($column_name, "%$filter_by_value%"); |
|
| 130 | + if(!is_null($filter_by_value)) { |
|
| 131 | + $Query->aw_like($column_name, "%$filter_by_value%"); |
|
| 132 | + } |
|
| 128 | 133 | |
| 129 | 134 | return $Query->ret_par(); |
| 130 | 135 | } |
@@ -137,8 +142,9 @@ discard block |
||
| 137 | 142 | |
| 138 | 143 | $table = self::table_name_to_Table($table); |
| 139 | 144 | |
| 140 | - if(is_null($column = $table->column($boolean_column_name)) || !$column->type()->is_boolean()) |
|
| 141 | - return false; |
|
| 145 | + if(is_null($column = $table->column($boolean_column_name)) || !$column->type()->is_boolean()) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 142 | 148 | |
| 143 | 149 | // TODO: still using 'id' instead of table->primaries |
| 144 | 150 | $Query = $table->update(); |
@@ -2,14 +2,14 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HexMakina\Crudites\Table; |
| 4 | 4 | |
| 5 | -use \HexMakina\Crudites\Interfaces\{TableManipulationInterface,SelectInterface}; |
|
| 6 | -use \HexMakina\Crudites\Queries\{Select,Insert,Update,Delete}; |
|
| 5 | +use \HexMakina\Crudites\Interfaces\{TableManipulationInterface, SelectInterface}; |
|
| 6 | +use \HexMakina\Crudites\Queries\{Select, Insert, Update, Delete}; |
|
| 7 | 7 | |
| 8 | 8 | class Manipulation extends Description implements TableManipulationInterface |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | 11 | // creates a new Row based on the table |
| 12 | - public function produce($dat_ass=[]) : Row |
|
| 12 | + public function produce($dat_ass = []) : Row |
|
| 13 | 13 | { |
| 14 | 14 | return new Row($this, $dat_ass); |
| 15 | 15 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | return $row; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - public function insert($values=[]) : Insert |
|
| 24 | + public function insert($values = []) : Insert |
|
| 25 | 25 | { |
| 26 | 26 | $q = new Insert($this, $values); |
| 27 | 27 | $q->connection($this->connection()); |
@@ -35,14 +35,14 @@ discard block |
||
| 35 | 35 | return $q; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - public function delete($conditions=[]) : Delete |
|
| 38 | + public function delete($conditions = []) : Delete |
|
| 39 | 39 | { |
| 40 | 40 | $q = new Delete($this, $conditions); |
| 41 | 41 | $q->connection($this->connection()); |
| 42 | 42 | return $q; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - public function select($columns=null, $table_alias=null) : SelectInterface |
|
| 45 | + public function select($columns = null, $table_alias = null) : SelectInterface |
|
| 46 | 46 | { |
| 47 | 47 | $table_alias = $table_alias ?? $this->name(); |
| 48 | 48 | $q = new Select($columns ?? [$table_alias.'.*'], $this, $table_alias); |
@@ -64,17 +64,17 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * loads row content from database, |
|
| 68 | - * |
|
| 69 | - * looks for primary key matching data in $dat_ass and sets the $load variable |
|
| 70 | - * $load stays null if |
|
| 71 | - * 1. not match is found in $dat_ass |
|
| 72 | - * 2. multiple records are returned |
|
| 73 | - * 3. no record is found |
|
| 74 | - * |
|
| 75 | - * @param Array $dat_ass an associative array containing primary key data matches |
|
| 76 | - * @return $this |
|
| 77 | - */ |
|
| 67 | + * loads row content from database, |
|
| 68 | + * |
|
| 69 | + * looks for primary key matching data in $dat_ass and sets the $load variable |
|
| 70 | + * $load stays null if |
|
| 71 | + * 1. not match is found in $dat_ass |
|
| 72 | + * 2. multiple records are returned |
|
| 73 | + * 3. no record is found |
|
| 74 | + * |
|
| 75 | + * @param Array $dat_ass an associative array containing primary key data matches |
|
| 76 | + * @return $this |
|
| 77 | + */ |
|
| 78 | 78 | public function load($dat_ass) |
| 79 | 79 | { |
| 80 | 80 | $pks = $this->table()->primary_keys_match($dat_ass); |
@@ -181,8 +181,8 @@ discard block |
||
| 181 | 181 | |
| 182 | 182 | //------------------------------------------------------------ type:data validation |
| 183 | 183 | /** |
| 184 | - * @return array containing all invalid data, indexed by field name, or empty if all valid |
|
| 185 | - */ |
|
| 184 | + * @return array containing all invalid data, indexed by field name, or empty if all valid |
|
| 185 | + */ |
|
| 186 | 186 | public function validate() : array |
| 187 | 187 | { |
| 188 | 188 | $errors = []; |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | public function __toString() |
| 28 | 28 | { |
| 29 | - return PHP_EOL .'load: '.json_encode($this->load) . PHP_EOL.'alterations: '.json_encode(array_keys($this->alterations)); |
|
| 29 | + return PHP_EOL.'load: '.json_encode($this->load).PHP_EOL.'alterations: '.json_encode(array_keys($this->alterations)); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | public function __debugInfo() : array |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | { |
| 90 | 90 | $pks = $this->table()->primary_keys_match($dat_ass); |
| 91 | 91 | |
| 92 | - if(empty($pks)) |
|
| 92 | + if (empty($pks)) |
|
| 93 | 93 | return $this; |
| 94 | 94 | |
| 95 | 95 | $this->last_query = $this->table()->select()->aw_primary($pks); |
@@ -109,20 +109,20 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | public function alter($dat_ass) |
| 111 | 111 | { |
| 112 | - foreach($dat_ass as $field_name => $value) |
|
| 112 | + foreach ($dat_ass as $field_name => $value) |
|
| 113 | 113 | { |
| 114 | 114 | $column = $this->table->column($field_name); |
| 115 | 115 | |
| 116 | 116 | // skips non exisint field name and A_I column |
| 117 | - if(is_null($column) || $column->is_auto_incremented()) |
|
| 117 | + if (is_null($column) || $column->is_auto_incremented()) |
|
| 118 | 118 | continue; |
| 119 | 119 | |
| 120 | 120 | // replaces empty strings with null or default value |
| 121 | - if(trim($dat_ass[$field_name]) === '') |
|
| 121 | + if (trim($dat_ass[$field_name]) === '') |
|
| 122 | 122 | $dat_ass[$field_name] = $column->is_nullable() ? null : $column->default(); |
| 123 | 123 | |
| 124 | 124 | // checks for changes with loaded data. using == instead of === is risky but needed |
| 125 | - if(!is_array($this->load) || $this->load[$field_name] != $dat_ass[$field_name]) |
|
| 125 | + if (!is_array($this->load) || $this->load[$field_name] != $dat_ass[$field_name]) |
|
| 126 | 126 | $this->alterations[$field_name] = $dat_ass[$field_name]; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -132,20 +132,20 @@ discard block |
||
| 132 | 132 | public function persist() : array |
| 133 | 133 | { |
| 134 | 134 | |
| 135 | - if(!$this->is_new() && !$this->is_altered()) // existing record with no alterations |
|
| 135 | + if (!$this->is_new() && !$this->is_altered()) // existing record with no alterations |
|
| 136 | 136 | return []; |
| 137 | 137 | |
| 138 | - if(!empty($errors = $this->validate())) // Table level validation |
|
| 138 | + if (!empty($errors = $this->validate())) // Table level validation |
|
| 139 | 139 | return $errors; |
| 140 | 140 | |
| 141 | 141 | try |
| 142 | 142 | { |
| 143 | - if($this->is_new()) |
|
| 143 | + if ($this->is_new()) |
|
| 144 | 144 | { |
| 145 | 145 | $this->last_alter_query = $this->table()->insert($this->export()); |
| 146 | 146 | $this->last_alter_query->run(); |
| 147 | - if($this->last_alter_query->is_success() && !is_null($aipk = $this->last_alter_query->table()->auto_incremented_primary_key())) |
|
| 148 | - $this->alterations[$aipk->name()]=$this->last_alter_query->inserted_id(); |
|
| 147 | + if ($this->last_alter_query->is_success() && !is_null($aipk = $this->last_alter_query->table()->auto_incremented_primary_key())) |
|
| 148 | + $this->alterations[$aipk->name()] = $this->last_alter_query->inserted_id(); |
|
| 149 | 149 | } |
| 150 | 150 | else |
| 151 | 151 | { |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | |
| 157 | 157 | $this->last_query = $this->last_alter_query; |
| 158 | 158 | } |
| 159 | - catch(CruditesException $e) |
|
| 159 | + catch (CruditesException $e) |
|
| 160 | 160 | { |
| 161 | 161 | return [$e->getMessage()]; |
| 162 | 162 | } |
@@ -169,13 +169,13 @@ discard block |
||
| 169 | 169 | $dat_ass = $this->load ?? $this->fresh ?? $this->alterations; |
| 170 | 170 | |
| 171 | 171 | // need The Primary key, then you can wipe at ease |
| 172 | - if(!empty($pk_match = $this->table()->primary_keys_match($dat_ass))) |
|
| 172 | + if (!empty($pk_match = $this->table()->primary_keys_match($dat_ass))) |
|
| 173 | 173 | { |
| 174 | 174 | $this->last_alter_query = $this->table->delete($pk_match); |
| 175 | - try{ |
|
| 175 | + try { |
|
| 176 | 176 | $this->last_alter_query->run(); |
| 177 | 177 | } |
| 178 | - catch(CruditesException $e){ |
|
| 178 | + catch (CruditesException $e) { |
|
| 179 | 179 | return false; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -196,12 +196,12 @@ discard block |
||
| 196 | 196 | $dat_ass = $this->export(); |
| 197 | 197 | |
| 198 | 198 | // vdt($this->table); |
| 199 | - foreach($this->table->columns() as $column_name => $column) |
|
| 199 | + foreach ($this->table->columns() as $column_name => $column) |
|
| 200 | 200 | { |
| 201 | 201 | $field_value = $dat_ass[$column_name] ?? null; |
| 202 | 202 | |
| 203 | 203 | $validation = $column->validate_value($field_value); |
| 204 | - if($validation !== true) |
|
| 204 | + if ($validation !== true) |
|
| 205 | 205 | $errors[$column_name] = $validation; |
| 206 | 206 | } |
| 207 | 207 | |
@@ -89,8 +89,9 @@ discard block |
||
| 89 | 89 | { |
| 90 | 90 | $pks = $this->table()->primary_keys_match($dat_ass); |
| 91 | 91 | |
| 92 | - if(empty($pks)) |
|
| 93 | - return $this; |
|
| 92 | + if(empty($pks)) { |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 94 | 95 | |
| 95 | 96 | $this->last_query = $this->table()->select()->aw_primary($pks); |
| 96 | 97 | $res = $this->last_query->ret_ass(); |
@@ -114,16 +115,19 @@ discard block |
||
| 114 | 115 | $column = $this->table->column($field_name); |
| 115 | 116 | |
| 116 | 117 | // skips non exisint field name and A_I column |
| 117 | - if(is_null($column) || $column->is_auto_incremented()) |
|
| 118 | - continue; |
|
| 118 | + if(is_null($column) || $column->is_auto_incremented()) { |
|
| 119 | + continue; |
|
| 120 | + } |
|
| 119 | 121 | |
| 120 | 122 | // replaces empty strings with null or default value |
| 121 | - if(trim($dat_ass[$field_name]) === '') |
|
| 122 | - $dat_ass[$field_name] = $column->is_nullable() ? null : $column->default(); |
|
| 123 | + if(trim($dat_ass[$field_name]) === '') { |
|
| 124 | + $dat_ass[$field_name] = $column->is_nullable() ? null : $column->default(); |
|
| 125 | + } |
|
| 123 | 126 | |
| 124 | 127 | // checks for changes with loaded data. using == instead of === is risky but needed |
| 125 | - if(!is_array($this->load) || $this->load[$field_name] != $dat_ass[$field_name]) |
|
| 126 | - $this->alterations[$field_name] = $dat_ass[$field_name]; |
|
| 128 | + if(!is_array($this->load) || $this->load[$field_name] != $dat_ass[$field_name]) { |
|
| 129 | + $this->alterations[$field_name] = $dat_ass[$field_name]; |
|
| 130 | + } |
|
| 127 | 131 | } |
| 128 | 132 | |
| 129 | 133 | return $this; |
@@ -132,11 +136,15 @@ discard block |
||
| 132 | 136 | public function persist() : array |
| 133 | 137 | { |
| 134 | 138 | |
| 135 | - if(!$this->is_new() && !$this->is_altered()) // existing record with no alterations |
|
| 139 | + if(!$this->is_new() && !$this->is_altered()) { |
|
| 140 | + // existing record with no alterations |
|
| 136 | 141 | return []; |
| 142 | + } |
|
| 137 | 143 | |
| 138 | - if(!empty($errors = $this->validate())) // Table level validation |
|
| 144 | + if(!empty($errors = $this->validate())) { |
|
| 145 | + // Table level validation |
|
| 139 | 146 | return $errors; |
| 147 | + } |
|
| 140 | 148 | |
| 141 | 149 | try |
| 142 | 150 | { |
@@ -144,10 +152,10 @@ discard block |
||
| 144 | 152 | { |
| 145 | 153 | $this->last_alter_query = $this->table()->insert($this->export()); |
| 146 | 154 | $this->last_alter_query->run(); |
| 147 | - if($this->last_alter_query->is_success() && !is_null($aipk = $this->last_alter_query->table()->auto_incremented_primary_key())) |
|
| 148 | - $this->alterations[$aipk->name()]=$this->last_alter_query->inserted_id(); |
|
| 149 | - } |
|
| 150 | - else |
|
| 155 | + if($this->last_alter_query->is_success() && !is_null($aipk = $this->last_alter_query->table()->auto_incremented_primary_key())) { |
|
| 156 | + $this->alterations[$aipk->name()]=$this->last_alter_query->inserted_id(); |
|
| 157 | + } |
|
| 158 | + } else |
|
| 151 | 159 | { |
| 152 | 160 | $pk_match = $this->table()->primary_keys_match($this->load); |
| 153 | 161 | $this->last_alter_query = $this->table()->update($this->alterations, $pk_match); |
@@ -155,8 +163,7 @@ discard block |
||
| 155 | 163 | } |
| 156 | 164 | |
| 157 | 165 | $this->last_query = $this->last_alter_query; |
| 158 | - } |
|
| 159 | - catch(CruditesException $e) |
|
| 166 | + } catch(CruditesException $e) |
|
| 160 | 167 | { |
| 161 | 168 | return [$e->getMessage()]; |
| 162 | 169 | } |
@@ -174,8 +181,7 @@ discard block |
||
| 174 | 181 | $this->last_alter_query = $this->table->delete($pk_match); |
| 175 | 182 | try{ |
| 176 | 183 | $this->last_alter_query->run(); |
| 177 | - } |
|
| 178 | - catch(CruditesException $e){ |
|
| 184 | + } catch(CruditesException $e){ |
|
| 179 | 185 | return false; |
| 180 | 186 | } |
| 181 | 187 | |
@@ -201,8 +207,9 @@ discard block |
||
| 201 | 207 | $field_value = $dat_ass[$column_name] ?? null; |
| 202 | 208 | |
| 203 | 209 | $validation = $column->validate_value($field_value); |
| 204 | - if($validation !== true) |
|
| 205 | - $errors[$column_name] = $validation; |
|
| 210 | + if($validation !== true) { |
|
| 211 | + $errors[$column_name] = $validation; |
|
| 212 | + } |
|
| 206 | 213 | } |
| 207 | 214 | |
| 208 | 215 | return $errors; |
@@ -50,40 +50,40 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | public function __construct($specs_type) |
| 52 | 52 | { |
| 53 | - foreach(self::$types_rx as $type => $rx) |
|
| 53 | + foreach (self::$types_rx as $type => $rx) |
|
| 54 | 54 | { |
| 55 | - if(preg_match("/$rx/i", $specs_type, $m) === 1) |
|
| 55 | + if (preg_match("/$rx/i", $specs_type, $m) === 1) |
|
| 56 | 56 | { |
| 57 | 57 | $this->name = $type; |
| 58 | 58 | |
| 59 | - if($this->is_enum()) |
|
| 60 | - $this->enum_values = explode('\',\'',$m[1]); |
|
| 61 | - elseif(preg_match('/([\d]+)/', $specs_type, $m) === 1) |
|
| 59 | + if ($this->is_enum()) |
|
| 60 | + $this->enum_values = explode('\',\'', $m[1]); |
|
| 61 | + elseif (preg_match('/([\d]+)/', $specs_type, $m) === 1) |
|
| 62 | 62 | $this->length = (int)$m[0]; |
| 63 | 63 | break; |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if(is_null($this->name)) |
|
| 67 | + if (is_null($this->name)) |
|
| 68 | 68 | throw new CruditesException('FIELD_TYPE_UNKNOWN'); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function is_text() : bool { return $this->name === self::TYPE_TEXT;} |
|
| 72 | - public function is_string() : bool { return $this->name === self::TYPE_STRING;} |
|
| 71 | + public function is_text() : bool { return $this->name === self::TYPE_TEXT; } |
|
| 72 | + public function is_string() : bool { return $this->name === self::TYPE_STRING; } |
|
| 73 | 73 | |
| 74 | - public function is_boolean() : bool { return $this->name === self::TYPE_BOOLEAN;} |
|
| 74 | + public function is_boolean() : bool { return $this->name === self::TYPE_BOOLEAN; } |
|
| 75 | 75 | |
| 76 | - public function is_integer() : bool { return $this->name === self::TYPE_INTEGER;} |
|
| 77 | - public function is_float() : bool { return $this->name === self::TYPE_FLOAT;} |
|
| 78 | - public function is_decimal() : bool { return $this->name === self::TYPE_DECIMAL;} |
|
| 76 | + public function is_integer() : bool { return $this->name === self::TYPE_INTEGER; } |
|
| 77 | + public function is_float() : bool { return $this->name === self::TYPE_FLOAT; } |
|
| 78 | + public function is_decimal() : bool { return $this->name === self::TYPE_DECIMAL; } |
|
| 79 | 79 | |
| 80 | - public function is_enum() : bool { return $this->name === self::TYPE_ENUM;} |
|
| 80 | + public function is_enum() : bool { return $this->name === self::TYPE_ENUM; } |
|
| 81 | 81 | |
| 82 | - public function is_year() : bool { return $this->name === self::TYPE_YEAR;} |
|
| 83 | - public function is_date() : bool { return $this->name === self::TYPE_DATE;} |
|
| 84 | - public function is_time() : bool { return $this->name === self::TYPE_TIME;} |
|
| 85 | - public function is_timestamp() : bool { return $this->name === self::TYPE_TIMESTAMP;} |
|
| 86 | - public function is_datetime() : bool { return $this->name === self::TYPE_DATETIME;} |
|
| 82 | + public function is_year() : bool { return $this->name === self::TYPE_YEAR; } |
|
| 83 | + public function is_date() : bool { return $this->name === self::TYPE_DATE; } |
|
| 84 | + public function is_time() : bool { return $this->name === self::TYPE_TIME; } |
|
| 85 | + public function is_timestamp() : bool { return $this->name === self::TYPE_TIMESTAMP; } |
|
| 86 | + public function is_datetime() : bool { return $this->name === self::TYPE_DATETIME; } |
|
| 87 | 87 | |
| 88 | 88 | public function is_date_or_time() : bool |
| 89 | 89 | { |
@@ -108,22 +108,22 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | public function validate_value($field_value) |
| 110 | 110 | { |
| 111 | - if($this->is_text()) |
|
| 111 | + if ($this->is_text()) |
|
| 112 | 112 | return true; |
| 113 | 113 | |
| 114 | - if($this->is_date_or_time()) |
|
| 114 | + if ($this->is_date_or_time()) |
|
| 115 | 115 | return date_create($field_value) === false ? 'ERR_FIELD_FORMAT' : true; |
| 116 | 116 | |
| 117 | - if($this->is_year()) |
|
| 117 | + if ($this->is_year()) |
|
| 118 | 118 | return preg_match('/^[0-9]{4}$/', $field_value) !== 1 ? 'ERR_FIELD_FORMAT' : true; |
| 119 | 119 | |
| 120 | - if($this->is_numeric()) |
|
| 120 | + if ($this->is_numeric()) |
|
| 121 | 121 | return (!is_numeric($field_value)) ? 'ERR_FIELD_FORMAT' : true; |
| 122 | 122 | |
| 123 | - if($this->is_string()) |
|
| 123 | + if ($this->is_string()) |
|
| 124 | 124 | return ($this->length() < strlen($field_value)) ? 'ERR_FIELD_TOO_LONG' : true; |
| 125 | 125 | |
| 126 | - if($this->is_enum()) |
|
| 126 | + if ($this->is_enum()) |
|
| 127 | 127 | return !in_array($field_value, $this->enum_values()) ? 'ERR_FIELD_VALUE_RESTRICTED_BY_ENUM' : true; |
| 128 | 128 | |
| 129 | 129 | return true; |
@@ -56,16 +56,18 @@ discard block |
||
| 56 | 56 | { |
| 57 | 57 | $this->name = $type; |
| 58 | 58 | |
| 59 | - if($this->is_enum()) |
|
| 60 | - $this->enum_values = explode('\',\'',$m[1]); |
|
| 61 | - elseif(preg_match('/([\d]+)/', $specs_type, $m) === 1) |
|
| 62 | - $this->length = (int)$m[0]; |
|
| 59 | + if($this->is_enum()) { |
|
| 60 | + $this->enum_values = explode('\',\'',$m[1]); |
|
| 61 | + } elseif(preg_match('/([\d]+)/', $specs_type, $m) === 1) { |
|
| 62 | + $this->length = (int)$m[0]; |
|
| 63 | + } |
|
| 63 | 64 | break; |
| 64 | 65 | } |
| 65 | 66 | } |
| 66 | 67 | |
| 67 | - if(is_null($this->name)) |
|
| 68 | - throw new CruditesException('FIELD_TYPE_UNKNOWN'); |
|
| 68 | + if(is_null($this->name)) { |
|
| 69 | + throw new CruditesException('FIELD_TYPE_UNKNOWN'); |
|
| 70 | + } |
|
| 69 | 71 | } |
| 70 | 72 | |
| 71 | 73 | public function is_text() : bool { return $this->name === self::TYPE_TEXT;} |
@@ -108,23 +110,29 @@ discard block |
||
| 108 | 110 | |
| 109 | 111 | public function validate_value($field_value) |
| 110 | 112 | { |
| 111 | - if($this->is_text()) |
|
| 112 | - return true; |
|
| 113 | + if($this->is_text()) { |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 113 | 116 | |
| 114 | - if($this->is_date_or_time()) |
|
| 115 | - return date_create($field_value) === false ? 'ERR_FIELD_FORMAT' : true; |
|
| 117 | + if($this->is_date_or_time()) { |
|
| 118 | + return date_create($field_value) === false ? 'ERR_FIELD_FORMAT' : true; |
|
| 119 | + } |
|
| 116 | 120 | |
| 117 | - if($this->is_year()) |
|
| 118 | - return preg_match('/^[0-9]{4}$/', $field_value) !== 1 ? 'ERR_FIELD_FORMAT' : true; |
|
| 121 | + if($this->is_year()) { |
|
| 122 | + return preg_match('/^[0-9]{4}$/', $field_value) !== 1 ? 'ERR_FIELD_FORMAT' : true; |
|
| 123 | + } |
|
| 119 | 124 | |
| 120 | - if($this->is_numeric()) |
|
| 121 | - return (!is_numeric($field_value)) ? 'ERR_FIELD_FORMAT' : true; |
|
| 125 | + if($this->is_numeric()) { |
|
| 126 | + return (!is_numeric($field_value)) ? 'ERR_FIELD_FORMAT' : true; |
|
| 127 | + } |
|
| 122 | 128 | |
| 123 | - if($this->is_string()) |
|
| 124 | - return ($this->length() < strlen($field_value)) ? 'ERR_FIELD_TOO_LONG' : true; |
|
| 129 | + if($this->is_string()) { |
|
| 130 | + return ($this->length() < strlen($field_value)) ? 'ERR_FIELD_TOO_LONG' : true; |
|
| 131 | + } |
|
| 125 | 132 | |
| 126 | - if($this->is_enum()) |
|
| 127 | - return !in_array($field_value, $this->enum_values()) ? 'ERR_FIELD_VALUE_RESTRICTED_BY_ENUM' : true; |
|
| 133 | + if($this->is_enum()) { |
|
| 134 | + return !in_array($field_value, $this->enum_values()) ? 'ERR_FIELD_VALUE_RESTRICTED_BY_ENUM' : true; |
|
| 135 | + } |
|
| 128 | 136 | |
| 129 | 137 | return true; |
| 130 | 138 | } |
@@ -166,16 +166,16 @@ |
||
| 166 | 166 | if(count($this->unique_keys_by_name()) === 0 || !is_array($dat_ass)) |
| 167 | 167 | return []; |
| 168 | 168 | |
| 169 | - $keys = array_keys($dat_ass); |
|
| 169 | + $keys = array_keys($dat_ass); |
|
| 170 | 170 | |
| 171 | - foreach($this->unique_keys_by_name() as $constraint_name => $column_names) |
|
| 172 | - { |
|
| 173 | - if(!is_array($column_names)) |
|
| 174 | - $column_names = [$column_names]; |
|
| 171 | + foreach($this->unique_keys_by_name() as $constraint_name => $column_names) |
|
| 172 | + { |
|
| 173 | + if(!is_array($column_names)) |
|
| 174 | + $column_names = [$column_names]; |
|
| 175 | 175 | |
| 176 | - if(empty(array_diff($keys, $column_names))) |
|
| 176 | + if(empty(array_diff($keys, $column_names))) |
|
| 177 | 177 | return $dat_ass; |
| 178 | - } |
|
| 178 | + } |
|
| 179 | 179 | return []; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | namespace HexMakina\Crudites\Table; |
| 4 | 4 | |
| 5 | 5 | use HexMakina\Crudites\{CruditesException}; |
| 6 | -use HexMakina\Crudites\Interfaces\{ConnectionInterface,TableDescriptionInterface,TableColumnInterface}; |
|
| 6 | +use HexMakina\Crudites\Interfaces\{ConnectionInterface, TableDescriptionInterface, TableColumnInterface}; |
|
| 7 | 7 | |
| 8 | 8 | class Description implements TableDescriptionInterface |
| 9 | 9 | { |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | { |
| 43 | 43 | $this->columns[$column->name()] = $column; |
| 44 | 44 | |
| 45 | - if($column->is_primary()) |
|
| 45 | + if ($column->is_primary()) |
|
| 46 | 46 | { |
| 47 | 47 | $this->add_primary_key($column); |
| 48 | - if($column->is_auto_incremented()) |
|
| 48 | + if ($column->is_auto_incremented()) |
|
| 49 | 49 | $this->auto_incremented_primary_key($column); |
| 50 | 50 | } |
| 51 | 51 | } |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | public function add_unique_key($constraint_name, $columns) |
| 59 | 59 | { |
| 60 | - if(!isset($this->unique_keys[$constraint_name])) |
|
| 60 | + if (!isset($this->unique_keys[$constraint_name])) |
|
| 61 | 61 | $this->unique_keys[$constraint_name] = $columns; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | public function add_foreign_key(TableColumnInterface $column) |
| 65 | 65 | { |
| 66 | 66 | $this->foreign_keys_by_table[$column->foreign_table_name()] = $this->foreign_keys_by_table[$column->foreign_table_name()] ?? []; |
| 67 | - $this->foreign_keys_by_table[$column->foreign_table_name()] []= $column; |
|
| 67 | + $this->foreign_keys_by_table[$column->foreign_table_name()] [] = $column; |
|
| 68 | 68 | |
| 69 | 69 | $this->foreign_keys_by_name[$column->name()] = $column; |
| 70 | 70 | } |
@@ -101,18 +101,18 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // TableDescriptionInterface implementation |
| 104 | - public function primary_keys($with_values=null) : array |
|
| 104 | + public function primary_keys($with_values = null) : array |
|
| 105 | 105 | { |
| 106 | - if(is_null($with_values)) |
|
| 106 | + if (is_null($with_values)) |
|
| 107 | 107 | return $this->primary_keys; |
| 108 | 108 | |
| 109 | - if(!is_array($with_values) && count($this->primary_keys) === 1) |
|
| 109 | + if (!is_array($with_values) && count($this->primary_keys) === 1) |
|
| 110 | 110 | $with_values = [current($this->primary_keys)->name() => $with_values]; |
| 111 | 111 | |
| 112 | 112 | $valid_dat_ass = []; |
| 113 | - foreach($this->primary_keys as $pk_name => $pk_field) |
|
| 113 | + foreach ($this->primary_keys as $pk_name => $pk_field) |
|
| 114 | 114 | { |
| 115 | - if(!isset($with_values[$pk_name]) && !$pk_field->is_nullable()) |
|
| 115 | + if (!isset($with_values[$pk_name]) && !$pk_field->is_nullable()) |
|
| 116 | 116 | return []; |
| 117 | 117 | |
| 118 | 118 | $valid_dat_ass[$pk_name] = $with_values[$pk_name]; |
@@ -122,13 +122,13 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | public function match_uniqueness($dat_ass) : array |
| 124 | 124 | { |
| 125 | - if(!is_array($dat_ass)) // aipk value |
|
| 125 | + if (!is_array($dat_ass)) // aipk value |
|
| 126 | 126 | return $this->primary_keys_match($dat_ass); |
| 127 | 127 | |
| 128 | - if(!empty($ret = $this->primary_keys_match($dat_ass))) |
|
| 128 | + if (!empty($ret = $this->primary_keys_match($dat_ass))) |
|
| 129 | 129 | return $ret; |
| 130 | 130 | |
| 131 | - if(!empty($ret = $this->unique_keys_match($dat_ass))) |
|
| 131 | + if (!empty($ret = $this->unique_keys_match($dat_ass))) |
|
| 132 | 132 | return $ret; |
| 133 | 133 | |
| 134 | 134 | return []; |
@@ -142,17 +142,17 @@ discard block |
||
| 142 | 142 | |
| 143 | 143 | public function primary_keys_match($dat_ass) : array |
| 144 | 144 | { |
| 145 | - if(count($this->primary_keys()) === 0) |
|
| 145 | + if (count($this->primary_keys()) === 0) |
|
| 146 | 146 | throw new CruditesException('NO_PRIMARY_KEYS_DEFINED'); |
| 147 | 147 | |
| 148 | - if(!is_array($dat_ass) && count($this->primary_keys()) === 1) |
|
| 148 | + if (!is_array($dat_ass) && count($this->primary_keys()) === 1) |
|
| 149 | 149 | $dat_ass = [current($this->primary_keys())->name() => $dat_ass]; |
| 150 | 150 | |
| 151 | 151 | $valid_dat_ass = []; |
| 152 | - foreach($this->primary_keys as $pk_name => $pk_field) |
|
| 152 | + foreach ($this->primary_keys as $pk_name => $pk_field) |
|
| 153 | 153 | { |
| 154 | 154 | // empty ensures non existing keys, null and empty values |
| 155 | - if(empty($dat_ass[$pk_name]) && !$pk_field->is_nullable()) |
|
| 155 | + if (empty($dat_ass[$pk_name]) && !$pk_field->is_nullable()) |
|
| 156 | 156 | return []; |
| 157 | 157 | |
| 158 | 158 | $valid_dat_ass[$pk_name] = $dat_ass[$pk_name] ?? null; |
@@ -163,17 +163,17 @@ discard block |
||
| 163 | 163 | |
| 164 | 164 | public function unique_keys_match($dat_ass) : array |
| 165 | 165 | { |
| 166 | - if(count($this->unique_keys_by_name()) === 0 || !is_array($dat_ass)) |
|
| 166 | + if (count($this->unique_keys_by_name()) === 0 || !is_array($dat_ass)) |
|
| 167 | 167 | return []; |
| 168 | 168 | |
| 169 | 169 | $keys = array_keys($dat_ass); |
| 170 | 170 | |
| 171 | - foreach($this->unique_keys_by_name() as $constraint_name => $column_names) |
|
| 171 | + foreach ($this->unique_keys_by_name() as $constraint_name => $column_names) |
|
| 172 | 172 | { |
| 173 | - if(!is_array($column_names)) |
|
| 173 | + if (!is_array($column_names)) |
|
| 174 | 174 | $column_names = [$column_names]; |
| 175 | 175 | |
| 176 | - if(empty(array_diff($keys, $column_names))) |
|
| 176 | + if (empty(array_diff($keys, $column_names))) |
|
| 177 | 177 | return $dat_ass; |
| 178 | 178 | } |
| 179 | 179 | return []; |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | { |
| 196 | 196 | $bonding_column_candidates = $this->foreign_keys_by_table()[$other_table->name()] ?? []; |
| 197 | 197 | |
| 198 | - if(count($bonding_column_candidates) === 1) |
|
| 198 | + if (count($bonding_column_candidates) === 1) |
|
| 199 | 199 | return current($bonding_column_candidates); |
| 200 | 200 | |
| 201 | 201 | return null; |
@@ -45,8 +45,9 @@ discard block |
||
| 45 | 45 | if($column->is_primary()) |
| 46 | 46 | { |
| 47 | 47 | $this->add_primary_key($column); |
| 48 | - if($column->is_auto_incremented()) |
|
| 49 | - $this->auto_incremented_primary_key($column); |
|
| 48 | + if($column->is_auto_incremented()) { |
|
| 49 | + $this->auto_incremented_primary_key($column); |
|
| 50 | + } |
|
| 50 | 51 | } |
| 51 | 52 | } |
| 52 | 53 | |
@@ -57,8 +58,9 @@ discard block |
||
| 57 | 58 | |
| 58 | 59 | public function add_unique_key($constraint_name, $columns) |
| 59 | 60 | { |
| 60 | - if(!isset($this->unique_keys[$constraint_name])) |
|
| 61 | - $this->unique_keys[$constraint_name] = $columns; |
|
| 61 | + if(!isset($this->unique_keys[$constraint_name])) { |
|
| 62 | + $this->unique_keys[$constraint_name] = $columns; |
|
| 63 | + } |
|
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | public function add_foreign_key(TableColumnInterface $column) |
@@ -103,17 +105,20 @@ discard block |
||
| 103 | 105 | // TableDescriptionInterface implementation |
| 104 | 106 | public function primary_keys($with_values=null) : array |
| 105 | 107 | { |
| 106 | - if(is_null($with_values)) |
|
| 107 | - return $this->primary_keys; |
|
| 108 | + if(is_null($with_values)) { |
|
| 109 | + return $this->primary_keys; |
|
| 110 | + } |
|
| 108 | 111 | |
| 109 | - if(!is_array($with_values) && count($this->primary_keys) === 1) |
|
| 110 | - $with_values = [current($this->primary_keys)->name() => $with_values]; |
|
| 112 | + if(!is_array($with_values) && count($this->primary_keys) === 1) { |
|
| 113 | + $with_values = [current($this->primary_keys)->name() => $with_values]; |
|
| 114 | + } |
|
| 111 | 115 | |
| 112 | 116 | $valid_dat_ass = []; |
| 113 | 117 | foreach($this->primary_keys as $pk_name => $pk_field) |
| 114 | 118 | { |
| 115 | - if(!isset($with_values[$pk_name]) && !$pk_field->is_nullable()) |
|
| 116 | - return []; |
|
| 119 | + if(!isset($with_values[$pk_name]) && !$pk_field->is_nullable()) { |
|
| 120 | + return []; |
|
| 121 | + } |
|
| 117 | 122 | |
| 118 | 123 | $valid_dat_ass[$pk_name] = $with_values[$pk_name]; |
| 119 | 124 | } |
@@ -122,14 +127,18 @@ discard block |
||
| 122 | 127 | |
| 123 | 128 | public function match_uniqueness($dat_ass) : array |
| 124 | 129 | { |
| 125 | - if(!is_array($dat_ass)) // aipk value |
|
| 130 | + if(!is_array($dat_ass)) { |
|
| 131 | + // aipk value |
|
| 126 | 132 | return $this->primary_keys_match($dat_ass); |
| 133 | + } |
|
| 127 | 134 | |
| 128 | - if(!empty($ret = $this->primary_keys_match($dat_ass))) |
|
| 129 | - return $ret; |
|
| 135 | + if(!empty($ret = $this->primary_keys_match($dat_ass))) { |
|
| 136 | + return $ret; |
|
| 137 | + } |
|
| 130 | 138 | |
| 131 | - if(!empty($ret = $this->unique_keys_match($dat_ass))) |
|
| 132 | - return $ret; |
|
| 139 | + if(!empty($ret = $this->unique_keys_match($dat_ass))) { |
|
| 140 | + return $ret; |
|
| 141 | + } |
|
| 133 | 142 | |
| 134 | 143 | return []; |
| 135 | 144 | } |
@@ -142,18 +151,21 @@ discard block |
||
| 142 | 151 | |
| 143 | 152 | public function primary_keys_match($dat_ass) : array |
| 144 | 153 | { |
| 145 | - if(count($this->primary_keys()) === 0) |
|
| 146 | - throw new CruditesException('NO_PRIMARY_KEYS_DEFINED'); |
|
| 154 | + if(count($this->primary_keys()) === 0) { |
|
| 155 | + throw new CruditesException('NO_PRIMARY_KEYS_DEFINED'); |
|
| 156 | + } |
|
| 147 | 157 | |
| 148 | - if(!is_array($dat_ass) && count($this->primary_keys()) === 1) |
|
| 149 | - $dat_ass = [current($this->primary_keys())->name() => $dat_ass]; |
|
| 158 | + if(!is_array($dat_ass) && count($this->primary_keys()) === 1) { |
|
| 159 | + $dat_ass = [current($this->primary_keys())->name() => $dat_ass]; |
|
| 160 | + } |
|
| 150 | 161 | |
| 151 | 162 | $valid_dat_ass = []; |
| 152 | 163 | foreach($this->primary_keys as $pk_name => $pk_field) |
| 153 | 164 | { |
| 154 | 165 | // empty ensures non existing keys, null and empty values |
| 155 | - if(empty($dat_ass[$pk_name]) && !$pk_field->is_nullable()) |
|
| 156 | - return []; |
|
| 166 | + if(empty($dat_ass[$pk_name]) && !$pk_field->is_nullable()) { |
|
| 167 | + return []; |
|
| 168 | + } |
|
| 157 | 169 | |
| 158 | 170 | $valid_dat_ass[$pk_name] = $dat_ass[$pk_name] ?? null; |
| 159 | 171 | } |
@@ -163,18 +175,21 @@ discard block |
||
| 163 | 175 | |
| 164 | 176 | public function unique_keys_match($dat_ass) : array |
| 165 | 177 | { |
| 166 | - if(count($this->unique_keys_by_name()) === 0 || !is_array($dat_ass)) |
|
| 167 | - return []; |
|
| 178 | + if(count($this->unique_keys_by_name()) === 0 || !is_array($dat_ass)) { |
|
| 179 | + return []; |
|
| 180 | + } |
|
| 168 | 181 | |
| 169 | 182 | $keys = array_keys($dat_ass); |
| 170 | 183 | |
| 171 | 184 | foreach($this->unique_keys_by_name() as $constraint_name => $column_names) |
| 172 | 185 | { |
| 173 | - if(!is_array($column_names)) |
|
| 174 | - $column_names = [$column_names]; |
|
| 186 | + if(!is_array($column_names)) { |
|
| 187 | + $column_names = [$column_names]; |
|
| 188 | + } |
|
| 175 | 189 | |
| 176 | - if(empty(array_diff($keys, $column_names))) |
|
| 177 | - return $dat_ass; |
|
| 190 | + if(empty(array_diff($keys, $column_names))) { |
|
| 191 | + return $dat_ass; |
|
| 192 | + } |
|
| 178 | 193 | } |
| 179 | 194 | return []; |
| 180 | 195 | } |
@@ -195,8 +210,9 @@ discard block |
||
| 195 | 210 | { |
| 196 | 211 | $bonding_column_candidates = $this->foreign_keys_by_table()[$other_table->name()] ?? []; |
| 197 | 212 | |
| 198 | - if(count($bonding_column_candidates) === 1) |
|
| 199 | - return current($bonding_column_candidates); |
|
| 213 | + if(count($bonding_column_candidates) === 1) { |
|
| 214 | + return current($bonding_column_candidates); |
|
| 215 | + } |
|
| 200 | 216 | |
| 201 | 217 | return null; |
| 202 | 218 | } |
@@ -69,10 +69,10 @@ |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | - * @return mixed the default value of a field |
|
| 73 | - * @return int for integer and boolean fields |
|
| 74 | - * @return null where no default is set |
|
| 75 | - */ |
|
| 72 | + * @return mixed the default value of a field |
|
| 73 | + * @return int for integer and boolean fields |
|
| 74 | + * @return null where no default is set |
|
| 75 | + */ |
|
| 76 | 76 | public function default() |
| 77 | 77 | { |
| 78 | 78 | $ret = $this->default_value; |
@@ -48,8 +48,8 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | $dbg = get_object_vars($this); |
| 50 | 50 | |
| 51 | - foreach($dbg as $k => $v) |
|
| 52 | - if(!isset($dbg[$k])) |
|
| 51 | + foreach ($dbg as $k => $v) |
|
| 52 | + if (!isset($dbg[$k])) |
|
| 53 | 53 | unset($dbg[$k]); |
| 54 | 54 | |
| 55 | 55 | return $dbg; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | { |
| 75 | 75 | $ret = $this->default_value; |
| 76 | 76 | |
| 77 | - if(!is_null($this->default_value) && ($this->type()->is_integer() || $this->type()->is_boolean())) |
|
| 77 | + if (!is_null($this->default_value) && ($this->type()->is_integer() || $this->type()->is_boolean())) |
|
| 78 | 78 | $ret = (int)$ret; |
| 79 | 79 | |
| 80 | 80 | return $ret; |
@@ -90,27 +90,27 @@ discard block |
||
| 90 | 90 | $this->extra = $v; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - public function is_primary($setter=null) : bool |
|
| 93 | + public function is_primary($setter = null) : bool |
|
| 94 | 94 | { |
| 95 | - return is_bool($setter)? ($this->primary = $setter) : $this->primary; |
|
| 95 | + return is_bool($setter) ? ($this->primary = $setter) : $this->primary; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - public function is_foreign($setter=null) : bool |
|
| 98 | + public function is_foreign($setter = null) : bool |
|
| 99 | 99 | { |
| 100 | 100 | return is_bool($setter) ? ($this->foreign = $setter) : $this->foreign; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - public function unique_name($setter=null) |
|
| 103 | + public function unique_name($setter = null) |
|
| 104 | 104 | { |
| 105 | 105 | return ($this->unique_name = ($setter ?? $this->unique_name)); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - public function unique_group_name($setter=null) |
|
| 108 | + public function unique_group_name($setter = null) |
|
| 109 | 109 | { |
| 110 | 110 | return ($this->unique_group_name = ($setter ?? $this->unique_group_name)); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - public function setForeignTableName($setter){ $this->foreign_table_name = $setter;} |
|
| 113 | + public function setForeignTableName($setter) { $this->foreign_table_name = $setter; } |
|
| 114 | 114 | public function foreign_table_name() : string |
| 115 | 115 | { |
| 116 | 116 | return $this->foreign_table_name; |
@@ -119,27 +119,27 @@ discard block |
||
| 119 | 119 | public function foreign_table_alias() : string |
| 120 | 120 | { |
| 121 | 121 | $ret = $this->foreign_table_name(); |
| 122 | - if(preg_match('/(.+)_('.$this->foreign_column_name().')$/', $this->name(), $m)) |
|
| 122 | + if (preg_match('/(.+)_('.$this->foreign_column_name().')$/', $this->name(), $m)) |
|
| 123 | 123 | $ret = $m[1]; |
| 124 | 124 | |
| 125 | 125 | return $ret; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - public function setForeignColumnName($setter){ $this->foreign_column_name = $setter;} |
|
| 129 | - public function foreign_column_name() : string{ return $this->foreign_column_name;} |
|
| 128 | + public function setForeignColumnName($setter) { $this->foreign_column_name = $setter; } |
|
| 129 | + public function foreign_column_name() : string{ return $this->foreign_column_name; } |
|
| 130 | 130 | |
| 131 | 131 | |
| 132 | - public function is_index($setter=null) : bool |
|
| 132 | + public function is_index($setter = null) : bool |
|
| 133 | 133 | { |
| 134 | 134 | return is_bool($setter) ? ($this->index = $setter) : $this->index; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - public function is_auto_incremented($setter=null) : bool |
|
| 137 | + public function is_auto_incremented($setter = null) : bool |
|
| 138 | 138 | { |
| 139 | 139 | return is_bool($setter) ? ($this->auto_incremented = $setter) : $this->auto_incremented; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - public function is_nullable($setter=null) : bool |
|
| 142 | + public function is_nullable($setter = null) : bool |
|
| 143 | 143 | { |
| 144 | 144 | return is_bool($setter) ? ($this->nullable = $setter) : $this->nullable; |
| 145 | 145 | } |
@@ -161,9 +161,9 @@ discard block |
||
| 161 | 161 | |
| 162 | 162 | public function import_describe($specs) |
| 163 | 163 | { |
| 164 | - foreach($specs as $k => $v) |
|
| 164 | + foreach ($specs as $k => $v) |
|
| 165 | 165 | { |
| 166 | - switch($k) |
|
| 166 | + switch ($k) |
|
| 167 | 167 | { |
| 168 | 168 | case 'Null': $this->is_nullable($v !== 'NO'); |
| 169 | 169 | break; |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | break; |
| 177 | 177 | |
| 178 | 178 | case 'Extra': |
| 179 | - if($v === 'auto_increment') |
|
| 179 | + if ($v === 'auto_increment') |
|
| 180 | 180 | $this->is_auto_incremented(true); |
| 181 | 181 | else |
| 182 | 182 | $this->setExtra($v); |
@@ -186,19 +186,19 @@ discard block |
||
| 186 | 186 | return $this; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - public function validate_value($field_value=null) |
|
| 189 | + public function validate_value($field_value = null) |
|
| 190 | 190 | { |
| 191 | - if($this->is_auto_incremented()) |
|
| 191 | + if ($this->is_auto_incremented()) |
|
| 192 | 192 | return true; |
| 193 | 193 | |
| 194 | - if($this->type()->is_boolean()) |
|
| 194 | + if ($this->type()->is_boolean()) |
|
| 195 | 195 | return true; |
| 196 | 196 | |
| 197 | - if(is_null($field_value)) |
|
| 197 | + if (is_null($field_value)) |
|
| 198 | 198 | { |
| 199 | - if($this->is_nullable()) |
|
| 199 | + if ($this->is_nullable()) |
|
| 200 | 200 | return true; |
| 201 | - elseif(is_null($this->default())) |
|
| 201 | + elseif (is_null($this->default())) |
|
| 202 | 202 | return 'ERR_FIELD_REQUIRED'; |
| 203 | 203 | } |
| 204 | 204 | |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | |
| 209 | 209 | public function is_hidden() |
| 210 | 210 | { |
| 211 | - switch($this->name()) |
|
| 211 | + switch ($this->name()) |
|
| 212 | 212 | { |
| 213 | 213 | case 'created_by': |
| 214 | 214 | case 'created_on': |
@@ -48,9 +48,10 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | $dbg = get_object_vars($this); |
| 50 | 50 | |
| 51 | - foreach($dbg as $k => $v) |
|
| 52 | - if(!isset($dbg[$k])) |
|
| 51 | + foreach($dbg as $k => $v) { |
|
| 52 | + if(!isset($dbg[$k])) |
|
| 53 | 53 | unset($dbg[$k]); |
| 54 | + } |
|
| 54 | 55 | |
| 55 | 56 | return $dbg; |
| 56 | 57 | } |
@@ -74,8 +75,9 @@ discard block |
||
| 74 | 75 | { |
| 75 | 76 | $ret = $this->default_value; |
| 76 | 77 | |
| 77 | - if(!is_null($this->default_value) && ($this->type()->is_integer() || $this->type()->is_boolean())) |
|
| 78 | - $ret = (int)$ret; |
|
| 78 | + if(!is_null($this->default_value) && ($this->type()->is_integer() || $this->type()->is_boolean())) { |
|
| 79 | + $ret = (int)$ret; |
|
| 80 | + } |
|
| 79 | 81 | |
| 80 | 82 | return $ret; |
| 81 | 83 | } |
@@ -119,8 +121,9 @@ discard block |
||
| 119 | 121 | public function foreign_table_alias() : string |
| 120 | 122 | { |
| 121 | 123 | $ret = $this->foreign_table_name(); |
| 122 | - if(preg_match('/(.+)_('.$this->foreign_column_name().')$/', $this->name(), $m)) |
|
| 123 | - $ret = $m[1]; |
|
| 124 | + if(preg_match('/(.+)_('.$this->foreign_column_name().')$/', $this->name(), $m)) { |
|
| 125 | + $ret = $m[1]; |
|
| 126 | + } |
|
| 124 | 127 | |
| 125 | 128 | return $ret; |
| 126 | 129 | } |
@@ -176,10 +179,11 @@ discard block |
||
| 176 | 179 | break; |
| 177 | 180 | |
| 178 | 181 | case 'Extra': |
| 179 | - if($v === 'auto_increment') |
|
| 180 | - $this->is_auto_incremented(true); |
|
| 181 | - else |
|
| 182 | - $this->setExtra($v); |
|
| 182 | + if($v === 'auto_increment') { |
|
| 183 | + $this->is_auto_incremented(true); |
|
| 184 | + } else { |
|
| 185 | + $this->setExtra($v); |
|
| 186 | + } |
|
| 183 | 187 | break; |
| 184 | 188 | } |
| 185 | 189 | } |
@@ -188,18 +192,21 @@ discard block |
||
| 188 | 192 | |
| 189 | 193 | public function validate_value($field_value=null) |
| 190 | 194 | { |
| 191 | - if($this->is_auto_incremented()) |
|
| 192 | - return true; |
|
| 195 | + if($this->is_auto_incremented()) { |
|
| 196 | + return true; |
|
| 197 | + } |
|
| 193 | 198 | |
| 194 | - if($this->type()->is_boolean()) |
|
| 195 | - return true; |
|
| 199 | + if($this->type()->is_boolean()) { |
|
| 200 | + return true; |
|
| 201 | + } |
|
| 196 | 202 | |
| 197 | 203 | if(is_null($field_value)) |
| 198 | 204 | { |
| 199 | - if($this->is_nullable()) |
|
| 200 | - return true; |
|
| 201 | - elseif(is_null($this->default())) |
|
| 202 | - return 'ERR_FIELD_REQUIRED'; |
|
| 205 | + if($this->is_nullable()) { |
|
| 206 | + return true; |
|
| 207 | + } elseif(is_null($this->default())) { |
|
| 208 | + return 'ERR_FIELD_REQUIRED'; |
|
| 209 | + } |
|
| 203 | 210 | } |
| 204 | 211 | |
| 205 | 212 | // nothing found on the Column level, lets check for Typing error |
@@ -13,10 +13,10 @@ discard block |
||
| 13 | 13 | return empty($match); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - public function get_id($mode=null) |
|
| 16 | + public function get_id($mode = null) |
|
| 17 | 17 | { |
| 18 | 18 | $primary_key = static::table()->auto_incremented_primary_key(); |
| 19 | - if(is_null($primary_key) && count($pks = static::table()->primary_keys())==1) |
|
| 19 | + if (is_null($primary_key) && count($pks = static::table()->primary_keys()) == 1) |
|
| 20 | 20 | $primary_key = current($pks); |
| 21 | 21 | |
| 22 | 22 | return $mode === 'name' ? $primary_key->name() : $this->get($primary_key->name()); |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | public function get($prop_name) |
| 26 | 26 | { |
| 27 | - if(property_exists($this, $prop_name) === true) |
|
| 27 | + if (property_exists($this, $prop_name) === true) |
|
| 28 | 28 | return $this->$prop_name; |
| 29 | 29 | |
| 30 | 30 | return null; |
@@ -37,11 +37,11 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | public function import($assoc_data) |
| 39 | 39 | { |
| 40 | - if(!is_array($assoc_data)) |
|
| 40 | + if (!is_array($assoc_data)) |
|
| 41 | 41 | throw new \Exception(__FUNCTION__.'(assoc_data) parm is not an array'); |
| 42 | 42 | |
| 43 | 43 | // shove it all up in model, god will sort them out |
| 44 | - foreach($assoc_data as $field => $value) |
|
| 44 | + foreach ($assoc_data as $field => $value) |
|
| 45 | 45 | $this->set($field, $value); |
| 46 | 46 | |
| 47 | 47 | return $this; |
@@ -61,10 +61,10 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | $table_name = $reflect->getConstant('TABLE_NAME'); |
| 63 | 63 | |
| 64 | - if($table_name === false) |
|
| 64 | + if ($table_name === false) |
|
| 65 | 65 | { |
| 66 | 66 | $calling_class = $reflect->getShortName(); |
| 67 | - if(defined($const_name = 'TABLE_'.strtoupper($calling_class))) |
|
| 67 | + if (defined($const_name = 'TABLE_'.strtoupper($calling_class))) |
|
| 68 | 68 | $table_name = constant($const_name); |
| 69 | 69 | else |
| 70 | 70 | $table_name = strtolower($calling_class); |
@@ -74,15 +74,15 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | |
| 77 | - public function to_table_row($operator_id=null) |
|
| 77 | + public function to_table_row($operator_id = null) |
|
| 78 | 78 | { |
| 79 | - if(!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) |
|
| 79 | + if (!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) |
|
| 80 | 80 | $this->set('created_by', $operator_id); |
| 81 | 81 | |
| 82 | 82 | $model_data = get_object_vars($this); |
| 83 | 83 | |
| 84 | 84 | // 1. Produce OR restore a row |
| 85 | - if($this->is_new()) |
|
| 85 | + if ($this->is_new()) |
|
| 86 | 86 | $table_row = static::table()->produce($model_data); |
| 87 | 87 | else |
| 88 | 88 | $table_row = static::table()->restore($model_data); |
@@ -16,16 +16,18 @@ discard block |
||
| 16 | 16 | public function get_id($mode=null) |
| 17 | 17 | { |
| 18 | 18 | $primary_key = static::table()->auto_incremented_primary_key(); |
| 19 | - if(is_null($primary_key) && count($pks = static::table()->primary_keys())==1) |
|
| 20 | - $primary_key = current($pks); |
|
| 19 | + if(is_null($primary_key) && count($pks = static::table()->primary_keys())==1) { |
|
| 20 | + $primary_key = current($pks); |
|
| 21 | + } |
|
| 21 | 22 | |
| 22 | 23 | return $mode === 'name' ? $primary_key->name() : $this->get($primary_key->name()); |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | public function get($prop_name) |
| 26 | 27 | { |
| 27 | - if(property_exists($this, $prop_name) === true) |
|
| 28 | - return $this->$prop_name; |
|
| 28 | + if(property_exists($this, $prop_name) === true) { |
|
| 29 | + return $this->$prop_name; |
|
| 30 | + } |
|
| 29 | 31 | |
| 30 | 32 | return null; |
| 31 | 33 | } |
@@ -37,12 +39,14 @@ discard block |
||
| 37 | 39 | |
| 38 | 40 | public function import($assoc_data) |
| 39 | 41 | { |
| 40 | - if(!is_array($assoc_data)) |
|
| 41 | - throw new \Exception(__FUNCTION__.'(assoc_data) parm is not an array'); |
|
| 42 | + if(!is_array($assoc_data)) { |
|
| 43 | + throw new \Exception(__FUNCTION__.'(assoc_data) parm is not an array'); |
|
| 44 | + } |
|
| 42 | 45 | |
| 43 | 46 | // shove it all up in model, god will sort them out |
| 44 | - foreach($assoc_data as $field => $value) |
|
| 45 | - $this->set($field, $value); |
|
| 47 | + foreach($assoc_data as $field => $value) { |
|
| 48 | + $this->set($field, $value); |
|
| 49 | + } |
|
| 46 | 50 | |
| 47 | 51 | return $this; |
| 48 | 52 | } |
@@ -64,10 +68,11 @@ discard block |
||
| 64 | 68 | if($table_name === false) |
| 65 | 69 | { |
| 66 | 70 | $calling_class = $reflect->getShortName(); |
| 67 | - if(defined($const_name = 'TABLE_'.strtoupper($calling_class))) |
|
| 68 | - $table_name = constant($const_name); |
|
| 69 | - else |
|
| 70 | - $table_name = strtolower($calling_class); |
|
| 71 | + if(defined($const_name = 'TABLE_'.strtoupper($calling_class))) { |
|
| 72 | + $table_name = constant($const_name); |
|
| 73 | + } else { |
|
| 74 | + $table_name = strtolower($calling_class); |
|
| 75 | + } |
|
| 71 | 76 | } |
| 72 | 77 | |
| 73 | 78 | return $table_name; |
@@ -76,16 +81,18 @@ discard block |
||
| 76 | 81 | |
| 77 | 82 | public function to_table_row($operator_id=null) |
| 78 | 83 | { |
| 79 | - if(!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) |
|
| 80 | - $this->set('created_by', $operator_id); |
|
| 84 | + if(!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) { |
|
| 85 | + $this->set('created_by', $operator_id); |
|
| 86 | + } |
|
| 81 | 87 | |
| 82 | 88 | $model_data = get_object_vars($this); |
| 83 | 89 | |
| 84 | 90 | // 1. Produce OR restore a row |
| 85 | - if($this->is_new()) |
|
| 86 | - $table_row = static::table()->produce($model_data); |
|
| 87 | - else |
|
| 88 | - $table_row = static::table()->restore($model_data); |
|
| 91 | + if($this->is_new()) { |
|
| 92 | + $table_row = static::table()->produce($model_data); |
|
| 93 | + } else { |
|
| 94 | + $table_row = static::table()->restore($model_data); |
|
| 95 | + } |
|
| 89 | 96 | |
| 90 | 97 | // 2. Apply alterations from form_model data |
| 91 | 98 | $table_row->alter($model_data); |
@@ -2,10 +2,10 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HexMakina\Crudites; |
| 4 | 4 | |
| 5 | -use HexMakina\Crudites\Queries\{Select,Describe}; |
|
| 6 | -use HexMakina\Crudites\Table\{Manipulation,Column}; |
|
| 5 | +use HexMakina\Crudites\Queries\{Select, Describe}; |
|
| 6 | +use HexMakina\Crudites\Table\{Manipulation, Column}; |
|
| 7 | 7 | |
| 8 | -use HexMakina\Crudites\Interfaces\{ConnectionInterface,DatabaseInterface,TableManipulationInterface}; |
|
| 8 | +use HexMakina\Crudites\Interfaces\{ConnectionInterface, DatabaseInterface, TableManipulationInterface}; |
|
| 9 | 9 | |
| 10 | 10 | class Database implements DatabaseInterface |
| 11 | 11 | { |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public function introspect(ConnectionInterface $information_schema = null) |
| 44 | 44 | { |
| 45 | - if(!is_null($information_schema)) |
|
| 45 | + if (!is_null($information_schema)) |
|
| 46 | 46 | $this->introspection_connection = $information_schema; |
| 47 | 47 | |
| 48 | - if(is_null($this->introspectionConnection())) |
|
| 48 | + if (is_null($this->introspectionConnection())) |
|
| 49 | 49 | return null; |
| 50 | 50 | |
| 51 | 51 | $statement = sprintf('SELECT TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION, COLUMN_NAME, POSITION_IN_UNIQUE_CONSTRAINT, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = "%s" ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION', $this->name()); |
@@ -54,17 +54,17 @@ discard block |
||
| 54 | 54 | $q->statement($statement); |
| 55 | 55 | $res = $q->ret_ass(); |
| 56 | 56 | |
| 57 | - foreach($res as $key_usage) |
|
| 57 | + foreach ($res as $key_usage) |
|
| 58 | 58 | { |
| 59 | 59 | $table_name = $key_usage['TABLE_NAME']; |
| 60 | 60 | |
| 61 | - if(isset($key_usage['REFERENCED_TABLE_NAME'])) // FOREIGN KEYS |
|
| 61 | + if (isset($key_usage['REFERENCED_TABLE_NAME'])) // FOREIGN KEYS |
|
| 62 | 62 | { |
| 63 | 63 | $this->fk_by_table[$table_name] = $this->fk_by_table[$table_name] ?? []; |
| 64 | 64 | $this->fk_by_table[$table_name][$key_usage['COLUMN_NAME']] = [$key_usage['REFERENCED_TABLE_NAME'], $key_usage['REFERENCED_COLUMN_NAME']]; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if(!isset($key_usage['POSITION_IN_UNIQUE_CONSTRAINT'])) // PRIMARY & UNIQUES |
|
| 67 | + if (!isset($key_usage['POSITION_IN_UNIQUE_CONSTRAINT'])) // PRIMARY & UNIQUES |
|
| 68 | 68 | { |
| 69 | 69 | $constraint_name = $key_usage['CONSTRAINT_NAME']; |
| 70 | 70 | $column_name = $key_usage['COLUMN_NAME']; |
@@ -75,10 +75,10 @@ discard block |
||
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - foreach($this->unique_by_table as $table_name => $uniques) |
|
| 79 | - foreach($uniques as $constraint_name => $columns) |
|
| 78 | + foreach ($this->unique_by_table as $table_name => $uniques) |
|
| 79 | + foreach ($uniques as $constraint_name => $columns) |
|
| 80 | 80 | { |
| 81 | - foreach($columns as $column_name) |
|
| 81 | + foreach ($columns as $column_name) |
|
| 82 | 82 | $this->unique_by_table[$table_name][$column_name] = [0 => $constraint_name] + $columns; |
| 83 | 83 | unset($this->unique_by_table[$table_name][$constraint_name]); |
| 84 | 84 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | public function inspect($table_name) : TableManipulationInterface |
| 88 | 88 | { |
| 89 | 89 | |
| 90 | - if(isset($this->table_cache[$table_name])) |
|
| 90 | + if (isset($this->table_cache[$table_name])) |
|
| 91 | 91 | return $this->table_cache[$table_name]; |
| 92 | 92 | |
| 93 | 93 | |
@@ -96,21 +96,21 @@ discard block |
||
| 96 | 96 | $description = $describe->ret(); |
| 97 | 97 | |
| 98 | 98 | // TODO test this when all is back to normal 2021.03.09 |
| 99 | - if($description === false) |
|
| 99 | + if ($description === false) |
|
| 100 | 100 | throw new \PDOException("Unable to describe $table_name"); |
| 101 | 101 | |
| 102 | 102 | $table = new Manipulation($table_name, $this->contentConnection()); |
| 103 | 103 | |
| 104 | - foreach($description as $column_name => $specs) |
|
| 104 | + foreach ($description as $column_name => $specs) |
|
| 105 | 105 | { |
| 106 | 106 | $column = new Column($table, $column_name, $specs); |
| 107 | 107 | |
| 108 | 108 | // handling usage constraints |
| 109 | - if(isset($this->unique_by_table[$table_name][$column_name])) |
|
| 109 | + if (isset($this->unique_by_table[$table_name][$column_name])) |
|
| 110 | 110 | { |
| 111 | 111 | $unique_name = $this->unique_by_table[$table_name][$column_name][0]; |
| 112 | 112 | |
| 113 | - switch(count($this->unique_by_table[$table_name][$column_name])) |
|
| 113 | + switch (count($this->unique_by_table[$table_name][$column_name])) |
|
| 114 | 114 | { |
| 115 | 115 | case 2: // constraint name + column |
| 116 | 116 | $column->unique_name($unique_name); |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | } |
| 128 | 128 | // handling usage foreign keys |
| 129 | - if(($reference = $this->getForeignKey($table_name, $column_name)) !== false) |
|
| 129 | + if (($reference = $this->getForeignKey($table_name, $column_name)) !== false) |
|
| 130 | 130 | { |
| 131 | 131 | $column->is_foreign(true); |
| 132 | 132 | $column->setForeignTableName($reference[0]); |
@@ -42,11 +42,13 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public function introspect(ConnectionInterface $information_schema = null) |
| 44 | 44 | { |
| 45 | - if(!is_null($information_schema)) |
|
| 46 | - $this->introspection_connection = $information_schema; |
|
| 45 | + if(!is_null($information_schema)) { |
|
| 46 | + $this->introspection_connection = $information_schema; |
|
| 47 | + } |
|
| 47 | 48 | |
| 48 | - if(is_null($this->introspectionConnection())) |
|
| 49 | - return null; |
|
| 49 | + if(is_null($this->introspectionConnection())) { |
|
| 50 | + return null; |
|
| 51 | + } |
|
| 50 | 52 | |
| 51 | 53 | $statement = sprintf('SELECT TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION, COLUMN_NAME, POSITION_IN_UNIQUE_CONSTRAINT, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = "%s" ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION', $this->name()); |
| 52 | 54 | $q = new Select(); |
@@ -58,15 +60,19 @@ discard block |
||
| 58 | 60 | { |
| 59 | 61 | $table_name = $key_usage['TABLE_NAME']; |
| 60 | 62 | |
| 61 | - if(isset($key_usage['REFERENCED_TABLE_NAME'])) // FOREIGN KEYS |
|
| 63 | + if(isset($key_usage['REFERENCED_TABLE_NAME'])) { |
|
| 64 | + // FOREIGN KEYS |
|
| 62 | 65 | { |
| 63 | 66 | $this->fk_by_table[$table_name] = $this->fk_by_table[$table_name] ?? []; |
| 67 | + } |
|
| 64 | 68 | $this->fk_by_table[$table_name][$key_usage['COLUMN_NAME']] = [$key_usage['REFERENCED_TABLE_NAME'], $key_usage['REFERENCED_COLUMN_NAME']]; |
| 65 | 69 | } |
| 66 | 70 | |
| 67 | - if(!isset($key_usage['POSITION_IN_UNIQUE_CONSTRAINT'])) // PRIMARY & UNIQUES |
|
| 71 | + if(!isset($key_usage['POSITION_IN_UNIQUE_CONSTRAINT'])) { |
|
| 72 | + // PRIMARY & UNIQUES |
|
| 68 | 73 | { |
| 69 | 74 | $constraint_name = $key_usage['CONSTRAINT_NAME']; |
| 75 | + } |
|
| 70 | 76 | $column_name = $key_usage['COLUMN_NAME']; |
| 71 | 77 | |
| 72 | 78 | $this->unique_by_table[$table_name] = $this->unique_by_table[$table_name] ?? []; |
@@ -75,11 +81,12 @@ discard block |
||
| 75 | 81 | } |
| 76 | 82 | } |
| 77 | 83 | |
| 78 | - foreach($this->unique_by_table as $table_name => $uniques) |
|
| 79 | - foreach($uniques as $constraint_name => $columns) |
|
| 84 | + foreach($this->unique_by_table as $table_name => $uniques) { |
|
| 85 | + foreach($uniques as $constraint_name => $columns) |
|
| 80 | 86 | { |
| 81 | 87 | foreach($columns as $column_name) |
| 82 | 88 | $this->unique_by_table[$table_name][$column_name] = [0 => $constraint_name] + $columns; |
| 89 | + } |
|
| 83 | 90 | unset($this->unique_by_table[$table_name][$constraint_name]); |
| 84 | 91 | } |
| 85 | 92 | } |
@@ -87,8 +94,9 @@ discard block |
||
| 87 | 94 | public function inspect($table_name) : TableManipulationInterface |
| 88 | 95 | { |
| 89 | 96 | |
| 90 | - if(isset($this->table_cache[$table_name])) |
|
| 91 | - return $this->table_cache[$table_name]; |
|
| 97 | + if(isset($this->table_cache[$table_name])) { |
|
| 98 | + return $this->table_cache[$table_name]; |
|
| 99 | + } |
|
| 92 | 100 | |
| 93 | 101 | |
| 94 | 102 | $describe = (new Describe($table_name)); |
@@ -96,8 +104,9 @@ discard block |
||
| 96 | 104 | $description = $describe->ret(); |
| 97 | 105 | |
| 98 | 106 | // TODO test this when all is back to normal 2021.03.09 |
| 99 | - if($description === false) |
|
| 100 | - throw new \PDOException("Unable to describe $table_name"); |
|
| 107 | + if($description === false) { |
|
| 108 | + throw new \PDOException("Unable to describe $table_name"); |
|
| 109 | + } |
|
| 101 | 110 | |
| 102 | 111 | $table = new Manipulation($table_name, $this->contentConnection()); |
| 103 | 112 | |