Mercurial > packages > magicforger
annotate 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 |
| 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 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
32 public function apply_inserts(string $target): string |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
33 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
34 $inserts = $this->get_all_keywords($target); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
35 $available_replacements = $this->get_available_inserts(); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
36 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
37 $target = str_replace( |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
38 array_keys($available_replacements), |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
39 $available_replacements, |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
40 $target |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
41 ); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
42 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
43 return $target; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
44 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
45 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
46 public function get_available_inserts() |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
47 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
48 $table_name = $this->getTableInput(); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
49 $replacements = [ |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
50 '// {{ valuesForCreation }}' => self::getValuesForCreation(), |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
51 ]; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
52 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
53 foreach ($replacements as $key => &$replacement) { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
54 $replacement = $replacement."\n".$key; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
55 } |
|
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 return $replacements; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
58 } |
| 5 | 59 } |
