Mercurial > packages > magicforger
diff src/Replacer/TableReplacer.php @ 7:769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
| author | luka |
|---|---|
| date | Wed, 18 Oct 2023 21:04:11 -0400 |
| parents | b46922d4a301 |
| children | 3426c7e91c24 |
line wrap: on
line diff
--- a/src/Replacer/TableReplacer.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Replacer/TableReplacer.php Wed Oct 18 21:04:11 2023 -0400 @@ -2,16 +2,58 @@ namespace Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; - trait TableReplacer { + protected $columns; + protected function get_columns() { - return $this->getTables()->getColumns(); + if (is_null($this->columns)) { + $this->columns = $this->getCurrentTable()->getColumns(); + } + + return $this->columns; } protected function get_attributes() { } + + protected function getValuesForCreation() + { + $insert = ''; + foreach ($this->get_columns() as $column) { + $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; + } + + return $insert; + } + + public function apply_inserts(string $target): string + { + $inserts = $this->get_all_keywords($target); + $available_replacements = $this->get_available_inserts(); + + $target = str_replace( + array_keys($available_replacements), + $available_replacements, + $target + ); + + return $target; + } + + public function get_available_inserts() + { + $table_name = $this->getTableInput(); + $replacements = [ + '// {{ valuesForCreation }}' => self::getValuesForCreation(), + ]; + + foreach ($replacements as $key => &$replacement) { + $replacement = $replacement."\n".$key; + } + + return $replacements; + } }
