Mercurial > packages > magicforger
diff src/Replacer/TableReplacer.php @ 34:f65ab84ee47f default
merge with codex
| author | luka |
|---|---|
| date | Wed, 10 Sep 2025 21:00:47 -0400 |
| parents | 8dd668020310 |
| children | b5c6ebd33547 |
line wrap: on
line diff
--- a/src/Replacer/TableReplacer.php Sat Dec 02 10:20:32 2023 -0500 +++ b/src/Replacer/TableReplacer.php Wed Sep 10 21:00:47 2025 -0400 @@ -1,59 +1,144 @@ <?php -namespace Wizzard\MagicForger\Replacer; +namespace Wizard\MagicForger\Replacer; trait TableReplacer { - protected $columns; + protected ?array $columns = null; - protected function get_columns() + protected array $columns_to_ignore = [ + 'id', + 'created_at', + 'updated_at', + 'created_by', + 'updated_by', + 'deleted_at', + ]; + + /** + * Retrieve columns for the current table. + */ + protected function get_columns(): array { if (is_null($this->columns)) { - $this->columns = $this->getCurrentTable()->getColumns(); + $this->columns = $this->getTableColumns($this->getCurrentTable()); } return $this->columns; } - protected function get_attributes() + /** + * Get a string representation of values for creation. + */ + protected function getValuesForCreation(): string { + $insert = ''; + foreach ($this->get_columns() as $column) { + $column_name = $column['name']; + $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column_name, $column_name)."\n"; + } + + return $insert; } - protected function getValuesForCreation() + /** + * Get a string representation of table attributes. + */ + protected function getCasts(): string { $insert = ''; foreach ($this->get_columns() as $column) { - $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; + if (in_array($column['name'], $this->columns_to_ignore)) { + continue; + } + $type = $column['type_name']; + //date + if(in_array($type, ['date'])) { + $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n"; + } + //time + if(in_array($type, ['timestamp'])) { + $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n"; + } + } + + return $insert; + } + + /** + * Get a string representation of table attributes. + */ + protected function getAttributes(): string + { + $insert = ''; + foreach ($this->get_columns() as $column) { + if (in_array($column['name'], $this->columns_to_ignore)) { + continue; + } + $insert .= sprintf("'%s' => '',", $column['name'])."\n"; } return $insert; } + /** + * Get a string representation of table fillable columns. + */ + protected function getFillable(): string + { + $insert = ''; + foreach ($this->get_columns() as $column) { + if (in_array($column['name'], $this->columns_to_ignore)) { + continue; + } + $insert .= sprintf("'%s',", $column['name'])."\n"; + } + + return $insert; + } + + /** + * Get formatted validation rules for table columns. + */ + protected function getValuesForValidation(): string + { + $insert = ''; + foreach ($this->get_columns() as $column) { + if (in_array($column['name'], $this->columns_to_ignore)) { + continue; + } + $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; + } + + return $insert; + } + + /** + * Apply insertions in the target template. + */ public function apply_inserts(string $target): string { $inserts = $this->get_all_keywords($target); - $available_replacements = $this->get_available_inserts(); + $available_insertions = $this->get_available_inserts(); - $target = str_replace( - array_keys($available_replacements), - $available_replacements, + return str_replace( + array_keys($available_insertions), + $available_insertions, $target ); - - return $target; } - public function get_available_inserts() + /** + * Get available insertion points for the template. + */ + public function get_available_inserts(): array { - $table_name = $this->getTableInput(); - $replacements = [ - '// {{ valuesForCreation }}' => self::getValuesForCreation(), + return [ + '// {{ valuesForCreation }}' => $this->getValuesForCreation(), + '# {{ attributeInsertPoint }}' => $this->getAttributes(), + '# {{ castInsertPoint }}' => $this->getCasts(), + '# {{ fillableInsertPoint }}' => $this->getFillable(), + '// {{ valuesForValidation }}' => $this->getValuesForValidation(), ]; - - foreach ($replacements as $key => &$replacement) { - $replacement = $replacement."\n".$key; - } - - return $replacements; } }
