comparison src/Replacer/TableReplacer.php @ 21:f0b0d014e448 main-dev

Cleaning up code based on AI overlord review
author Luka Sitas <sitas.luka.97@gmail.com>
date Wed, 26 Feb 2025 19:45:08 -0500
parents 19b7a8de0019
children 827efbf4d73c
comparison
equal deleted inserted replaced
20:81a76472ba21 21:f0b0d014e448
2 2
3 namespace Wizard\MagicForger\Replacer; 3 namespace Wizard\MagicForger\Replacer;
4 4
5 trait TableReplacer 5 trait TableReplacer
6 { 6 {
7 protected $columns; 7 protected ?array $columns = null;
8 8
9 protected function get_columns() 9 /**
10 * Retrieve columns for the current table.
11 *
12 * @return array
13 */
14 protected function get_columns(): array
10 { 15 {
11 if (is_null($this->columns)) { 16 if (is_null($this->columns)) {
12 $this->columns = $this->getCurrentTable()->getColumns(); 17 $this->columns = $this->getCurrentTable()->getColumns();
13 } 18 }
14 19
15 return $this->columns; 20 return $this->columns;
16 } 21 }
17 22
18 protected function get_attributes() 23 /**
19 { 24 * Get a string representation of values for creation.
20 } 25 *
21 26 * @return string
27 */
22 protected function getValuesForCreation(): string 28 protected function getValuesForCreation(): string
23 { 29 {
24 $insert = ''; 30 $insert = '';
25 foreach ($this->get_columns() as $column) { 31 foreach ($this->get_columns() as $column) {
26 $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; 32 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column->getName(), $column->getName()) . "\n";
27 } 33 }
28 34
29 return $insert; 35 return $insert;
30 } 36 }
31 37
38 /**
39 * Get a string representation of table attributes.
40 *
41 * @return string
42 */
32 protected function getAttributes(): string 43 protected function getAttributes(): string
33 { 44 {
34 $insert = ''; 45 $insert = '';
35 foreach ($this->get_columns() as $column) { 46 foreach ($this->get_columns() as $column) {
36 $insert .= "'".$column->getName()."' => '',\n"; 47 $insert .= sprintf("'%s' => '',", $column->getName()) . "\n";
37 } 48 }
38 49
39 return $insert; 50 return $insert;
40 } 51 }
41 52
53 /**
54 * Get formatted validation rules for table columns.
55 *
56 * @return string
57 */
42 protected function getValuesForValidation(): string 58 protected function getValuesForValidation(): string
43 { 59 {
44 $insert = ''; 60 $insert = '';
45 foreach ($this->get_columns() as $column) { 61 foreach ($this->get_columns() as $column) {
46 $insert .= "'".$column->getName()."' => 'nullable',\n"; 62 $insert .= sprintf("'%s' => 'nullable',", $column->getName()) . "\n";
47 } 63 }
48 64
49 return $insert; 65 return $insert;
50 } 66 }
51 67
68 /**
69 * Apply insertions in the target template.
70 *
71 * @param string $target
72 * @return string
73 */
52 public function apply_inserts(string $target): string 74 public function apply_inserts(string $target): string
53 { 75 {
54 $inserts = $this->get_all_keywords($target); 76 $inserts = $this->get_all_keywords($target);
55 $available_replacements = $this->get_available_inserts(); 77 $available_insertions = $this->get_available_inserts();
56 78
57 $target = str_replace( 79 return str_replace(
58 array_keys($available_replacements), 80 array_keys($available_insertions),
59 $available_replacements, 81 $available_insertions,
60 $target 82 $target
61 ); 83 );
62
63 return $target;
64 } 84 }
65 85
66 public function get_available_inserts() 86 /**
87 * Get available insertion points for the template.
88 *
89 * @return array
90 */
91 public function get_available_inserts(): array
67 { 92 {
68 $table_name = $this->getTableInput(); 93 return [
69 $replacements = [ 94 '// {{ valuesForCreation }}' => $this->getValuesForCreation(),
70 '// {{ valuesForCreation }}' => self::getValuesForCreation(), 95 '# {{ attributeInsertPoint }}' => $this->getAttributes(),
71 '# {{ atributeInsertPoint }}' => self::getAttributes(), 96 '// {{ valuesForValidation }}' => $this->getValuesForValidation(),
72 '// {{ valuesForValidation }}' => self::getValuesForValidation(), 97 ];
73 ];
74
75 foreach ($replacements as $key => &$replacement) {
76 $replacement = $replacement."\n".$key;
77 }
78
79 return $replacements;
80 } 98 }
81 } 99 }