comparison src/Replacer/TableReplacer.php @ 39:b5c6ebd33547 ls_dev_2025_09

Improving the factories, tests, and requests
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 25 Sep 2025 23:16:13 -0400
parents 8dd668020310
children 2cf26b593f4a
comparison
equal deleted inserted replaced
38:c062f013fd19 39:b5c6ebd33547
49 $insert = ''; 49 $insert = '';
50 foreach ($this->get_columns() as $column) { 50 foreach ($this->get_columns() as $column) {
51 if (in_array($column['name'], $this->columns_to_ignore)) { 51 if (in_array($column['name'], $this->columns_to_ignore)) {
52 continue; 52 continue;
53 } 53 }
54 $type = $column['type_name']; 54 $type = $column['type_name'];
55 //date 55 // date
56 if(in_array($type, ['date'])) { 56 if (in_array($type, ['date'])) {
57 $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n"; 57 $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n";
58 } 58 }
59 //time 59 // time
60 if(in_array($type, ['timestamp'])) { 60 if (in_array($type, ['timestamp'])) {
61 $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n"; 61 $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n";
62 } 62 }
63 } 63 }
64 64
65 return $insert; 65 return $insert;
66 } 66 }
67 67
102 */ 102 */
103 protected function getValuesForValidation(): string 103 protected function getValuesForValidation(): string
104 { 104 {
105 $insert = ''; 105 $insert = '';
106 foreach ($this->get_columns() as $column) { 106 foreach ($this->get_columns() as $column) {
107 if (in_array($column['name'], $this->columns_to_ignore)) { 107 // Don't do validation on some of the basic columns
108 continue; 108 if (in_array($column['name'], $this->columns_to_ignore)) {
109 } 109 continue;
110 $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; 110 }
111
112 $options = [];
113
114 // Add default value checks if needed
115 if (is_null($column['default']) && ! $column['nullable']) {
116 $options[] = "'required'";
117 }
118 else {
119 $options[] = "'nullable'";
120 }
121
122 // Determine the validations based on column type
123 $type = strtolower($column['type_name']);
124 $size = (preg_match('/\((\d+)\)/', $column['type'], $matches)) ? $matches[1] : null;
125
126 switch ($type) {
127 case 'varchar':
128 case 'char':
129 $options[] = "'string'";
130
131 // Extract length if defined
132 if (! is_null($size)) {
133 $options[] = "'max:$size'";
134 }
135 break;
136
137 case 'text':
138 $options[] = "'string'";
139 break;
140
141 case 'int':
142 case 'tinyint':
143 case 'smallint':
144 case 'mediumint':
145 case 'bigint':
146 $options[] = "'integer'";
147 if (strpos($column['type'], 'unsigned') !== false) {
148 $options[] = "'min:0'";
149 }
150 if (! is_null($size)) {
151 $options[] = "'max_digits:".$size."'";
152 }
153 break;
154
155 case 'decimal':
156 case 'double':
157 case 'float':
158 $options[] = "'numeric'";
159 // Extract precision and scale if defined
160 if (preg_match('/\((\d+),(\d+)\)/', $column['type'], $matches)) {
161 $precision = $matches[1];
162 $scale = isset($matches[2]) ? ','.$matches[2] : '';
163
164 $options[] = "'decimal:$precision$scale'";
165 }
166 break;
167
168 case 'boolean':
169 $options[] = "'boolean'";
170 break;
171
172 case 'date':
173 case 'datetime':
174 case 'timestamp':
175 $options[] = "'date'";
176 break;
177
178 // Add other types as needed
179 }
180
181 $insert .= sprintf("'%s' => [%s],\n", $column['name'], implode(',', $options));
111 } 182 }
112 183
113 return $insert; 184 return $insert;
114 } 185 }
115 186