comparison 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
comparison
equal deleted inserted replaced
6:b46922d4a301 7:769a17898cc0
1 <?php 1 <?php
2 2
3 namespace Wizzard\MagicForger\Replacer; 3 namespace Wizzard\MagicForger\Replacer;
4 4
5 use Illuminate\Support\Str;
6
7 trait TableReplacer 5 trait TableReplacer
8 { 6 {
7 protected $columns;
8
9 protected function get_columns() 9 protected function get_columns()
10 { 10 {
11 return $this->getTables()->getColumns(); 11 if (is_null($this->columns)) {
12 $this->columns = $this->getCurrentTable()->getColumns();
13 }
14
15 return $this->columns;
12 } 16 }
13 17
14 protected function get_attributes() 18 protected function get_attributes()
15 { 19 {
16 } 20 }
21
22 protected function getValuesForCreation()
23 {
24 $insert = '';
25 foreach ($this->get_columns() as $column) {
26 $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n";
27 }
28
29 return $insert;
30 }
31
32 public function apply_inserts(string $target): string
33 {
34 $inserts = $this->get_all_keywords($target);
35 $available_replacements = $this->get_available_inserts();
36
37 $target = str_replace(
38 array_keys($available_replacements),
39 $available_replacements,
40 $target
41 );
42
43 return $target;
44 }
45
46 public function get_available_inserts()
47 {
48 $table_name = $this->getTableInput();
49 $replacements = [
50 '// {{ valuesForCreation }}' => self::getValuesForCreation(),
51 ];
52
53 foreach ($replacements as $key => &$replacement) {
54 $replacement = $replacement."\n".$key;
55 }
56
57 return $replacements;
58 }
17 } 59 }