Mercurial > packages > magicforger
annotate src/Replacer/TableReplacer.php @ 11:3426c7e91c24 main-dev
Modifying generaters and replacers
| author | luka |
|---|---|
| date | Wed, 24 Apr 2024 19:53:42 -0400 |
| parents | 769a17898cc0 |
| children | c969ed13c570 |
| rev | line source |
|---|---|
| 5 | 1 <?php |
| 2 | |
| 3 namespace Wizzard\MagicForger\Replacer; | |
| 4 | |
| 6 | 5 trait TableReplacer |
| 5 | 6 { |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
7 protected $columns; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
8 |
| 5 | 9 protected function get_columns() |
| 10 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
11 if (is_null($this->columns)) { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
12 $this->columns = $this->getCurrentTable()->getColumns(); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
13 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
14 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
15 return $this->columns; |
| 5 | 16 } |
| 17 | |
| 18 protected function get_attributes() | |
| 19 { | |
| 20 } | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
21 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
22 protected function getValuesForCreation() |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
23 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
24 $insert = ''; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
25 foreach ($this->get_columns() as $column) { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
26 $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
27 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
28 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
29 return $insert; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
30 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
31 |
| 11 | 32 protected function getAttributes() |
| 33 { | |
| 34 $insert = ''; | |
| 35 foreach ($this->get_columns() as $column) { | |
| 36 $insert .= "'".$column->getName()."' => '',\n"; | |
| 37 } | |
| 38 | |
| 39 return $insert; | |
| 40 } | |
| 41 | |
| 42 protected function getValuesForValidation() | |
| 43 { | |
| 44 $insert = ''; | |
| 45 foreach ($this->get_columns() as $column) { | |
| 46 $insert .= "'".$column->getName()."' => 'nullable',\n"; | |
| 47 } | |
| 48 | |
| 49 return $insert; | |
| 50 } | |
| 51 | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
52 public function apply_inserts(string $target): string |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
53 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
54 $inserts = $this->get_all_keywords($target); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
55 $available_replacements = $this->get_available_inserts(); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
56 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
57 $target = str_replace( |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
58 array_keys($available_replacements), |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
59 $available_replacements, |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
60 $target |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
61 ); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
62 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
63 return $target; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
64 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
65 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
66 public function get_available_inserts() |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
67 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
68 $table_name = $this->getTableInput(); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
69 $replacements = [ |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
70 '// {{ valuesForCreation }}' => self::getValuesForCreation(), |
| 11 | 71 '# {{ atributeInsertPoint }}' => self::getAttributes(), |
| 72 '// {{ valuesForValidation }}' => self::getValuesForValidation(), | |
| 73 ]; | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
74 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
75 foreach ($replacements as $key => &$replacement) { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
76 $replacement = $replacement."\n".$key; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
77 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
78 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
79 return $replacements; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
80 } |
| 5 | 81 } |
