Mercurial > packages > magicforger
comparison src/Replacer/TableReplacer.php @ 23:827efbf4d73c main-dev
Huge changes to the relationships for models and more complex
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Fri, 11 Apr 2025 20:50:20 -0400 |
| parents | f0b0d014e448 |
| children | 8dd668020310 |
comparison
equal
deleted
inserted
replaced
| 22:ee8ef14e158d | 23:827efbf4d73c |
|---|---|
| 4 | 4 |
| 5 trait TableReplacer | 5 trait TableReplacer |
| 6 { | 6 { |
| 7 protected ?array $columns = null; | 7 protected ?array $columns = null; |
| 8 | 8 |
| 9 protected array $columns_to_ignore = [ | |
| 10 'id', | |
| 11 'created_at', | |
| 12 'updated_at', | |
| 13 'created_by', | |
| 14 'updated_by', | |
| 15 'deleted_at', | |
| 16 ]; | |
| 17 | |
| 9 /** | 18 /** |
| 10 * Retrieve columns for the current table. | 19 * Retrieve columns for the current table. |
| 11 * | |
| 12 * @return array | |
| 13 */ | 20 */ |
| 14 protected function get_columns(): array | 21 protected function get_columns(): array |
| 15 { | 22 { |
| 16 if (is_null($this->columns)) { | 23 if (is_null($this->columns)) { |
| 17 $this->columns = $this->getCurrentTable()->getColumns(); | 24 $this->columns = $this->getTableColumns($this->getCurrentTable()); |
| 18 } | 25 } |
| 19 | 26 |
| 20 return $this->columns; | 27 return $this->columns; |
| 21 } | 28 } |
| 22 | 29 |
| 23 /** | 30 /** |
| 24 * Get a string representation of values for creation. | 31 * Get a string representation of values for creation. |
| 25 * | |
| 26 * @return string | |
| 27 */ | 32 */ |
| 28 protected function getValuesForCreation(): string | 33 protected function getValuesForCreation(): string |
| 29 { | 34 { |
| 30 $insert = ''; | 35 $insert = ''; |
| 31 foreach ($this->get_columns() as $column) { | 36 foreach ($this->get_columns() as $column) { |
| 32 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column->getName(), $column->getName()) . "\n"; | 37 $column_name = $column['name']; |
| 38 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column_name, $column_name)."\n"; | |
| 33 } | 39 } |
| 34 | 40 |
| 35 return $insert; | 41 return $insert; |
| 36 } | 42 } |
| 37 | 43 |
| 38 /** | 44 /** |
| 39 * Get a string representation of table attributes. | 45 * Get a string representation of table attributes. |
| 40 * | |
| 41 * @return string | |
| 42 */ | 46 */ |
| 43 protected function getAttributes(): string | 47 protected function getAttributes(): string |
| 44 { | 48 { |
| 45 $insert = ''; | 49 $insert = ''; |
| 46 foreach ($this->get_columns() as $column) { | 50 foreach ($this->get_columns() as $column) { |
| 47 $insert .= sprintf("'%s' => '',", $column->getName()) . "\n"; | 51 if (in_array($column['name'], $this->columns_to_ignore)) { |
| 52 continue; | |
| 53 } | |
| 54 $insert .= sprintf("'%s' => '',", $column['name'])."\n"; | |
| 55 } | |
| 56 | |
| 57 return $insert; | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Get a string representation of table fillable columns. | |
| 62 */ | |
| 63 protected function getFillable(): string | |
| 64 { | |
| 65 $insert = ''; | |
| 66 foreach ($this->get_columns() as $column) { | |
| 67 if (in_array($column['name'], $this->columns_to_ignore)) { | |
| 68 continue; | |
| 69 } | |
| 70 $insert .= sprintf("'%s',", $column['name'])."\n"; | |
| 48 } | 71 } |
| 49 | 72 |
| 50 return $insert; | 73 return $insert; |
| 51 } | 74 } |
| 52 | 75 |
| 53 /** | 76 /** |
| 54 * Get formatted validation rules for table columns. | 77 * Get formatted validation rules for table columns. |
| 55 * | |
| 56 * @return string | |
| 57 */ | 78 */ |
| 58 protected function getValuesForValidation(): string | 79 protected function getValuesForValidation(): string |
| 59 { | 80 { |
| 60 $insert = ''; | 81 $insert = ''; |
| 61 foreach ($this->get_columns() as $column) { | 82 foreach ($this->get_columns() as $column) { |
| 62 $insert .= sprintf("'%s' => 'nullable',", $column->getName()) . "\n"; | 83 if (in_array($column['name'], $this->columns_to_ignore)) { |
| 84 continue; | |
| 85 } | |
| 86 $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; | |
| 63 } | 87 } |
| 64 | 88 |
| 65 return $insert; | 89 return $insert; |
| 66 } | 90 } |
| 67 | 91 |
| 68 /** | 92 /** |
| 69 * Apply insertions in the target template. | 93 * Apply insertions in the target template. |
| 70 * | |
| 71 * @param string $target | |
| 72 * @return string | |
| 73 */ | 94 */ |
| 74 public function apply_inserts(string $target): string | 95 public function apply_inserts(string $target): string |
| 75 { | 96 { |
| 76 $inserts = $this->get_all_keywords($target); | 97 $inserts = $this->get_all_keywords($target); |
| 77 $available_insertions = $this->get_available_inserts(); | 98 $available_insertions = $this->get_available_inserts(); |
| 83 ); | 104 ); |
| 84 } | 105 } |
| 85 | 106 |
| 86 /** | 107 /** |
| 87 * Get available insertion points for the template. | 108 * Get available insertion points for the template. |
| 88 * | |
| 89 * @return array | |
| 90 */ | 109 */ |
| 91 public function get_available_inserts(): array | 110 public function get_available_inserts(): array |
| 92 { | 111 { |
| 93 return [ | 112 return [ |
| 94 '// {{ valuesForCreation }}' => $this->getValuesForCreation(), | 113 '// {{ valuesForCreation }}' => $this->getValuesForCreation(), |
| 95 '# {{ attributeInsertPoint }}' => $this->getAttributes(), | 114 '# {{ attributeInsertPoint }}' => $this->getAttributes(), |
| 115 '# {{ fillableInsertPoint }}' => $this->getFillable(), | |
| 96 '// {{ valuesForValidation }}' => $this->getValuesForValidation(), | 116 '// {{ valuesForValidation }}' => $this->getValuesForValidation(), |
| 97 ]; | 117 ]; |
| 98 } | 118 } |
| 99 } | 119 } |
