Mercurial > packages > magicforger
annotate src/Replacer/TableReplacer.php @ 23:827efbf4d73c main-dev
Huge changes to the relationships for models and more complex
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Fri, 11 Apr 2025 20:50:20 -0400 |
| parents | f0b0d014e448 |
| children | 8dd668020310 |
| rev | line source |
|---|---|
| 5 | 1 <?php |
| 2 | |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
14
diff
changeset
|
3 namespace Wizard\MagicForger\Replacer; |
| 5 | 4 |
| 6 | 5 trait TableReplacer |
| 5 | 6 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
7 protected ?array $columns = null; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
8 |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
9 protected array $columns_to_ignore = [ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
10 'id', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
11 'created_at', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
12 'updated_at', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
13 'created_by', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
14 'updated_by', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
15 'deleted_at', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
16 ]; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
17 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
18 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
19 * Retrieve columns for the current table. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
20 */ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
21 protected function get_columns(): array |
| 5 | 22 { |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
23 if (is_null($this->columns)) { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
24 $this->columns = $this->getTableColumns($this->getCurrentTable()); |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
25 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
26 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
27 return $this->columns; |
| 5 | 28 } |
| 29 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
30 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
31 * Get a string representation of values for creation. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
32 */ |
| 14 | 33 protected function getValuesForCreation(): string |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
34 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
35 $insert = ''; |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
36 foreach ($this->get_columns() as $column) { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
37 $column_name = $column['name']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
38 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column_name, $column_name)."\n"; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
39 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
40 |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
41 return $insert; |
|
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 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
44 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
45 * Get a string representation of table attributes. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
46 */ |
| 14 | 47 protected function getAttributes(): string |
| 11 | 48 { |
| 49 $insert = ''; | |
| 50 foreach ($this->get_columns() as $column) { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
51 if (in_array($column['name'], $this->columns_to_ignore)) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
52 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
53 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
54 $insert .= sprintf("'%s' => '',", $column['name'])."\n"; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
55 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
56 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
57 return $insert; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
58 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
59 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
60 /** |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
61 * Get a string representation of table fillable columns. |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
62 */ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
63 protected function getFillable(): string |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
64 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
65 $insert = ''; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
66 foreach ($this->get_columns() as $column) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
67 if (in_array($column['name'], $this->columns_to_ignore)) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
68 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
69 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
70 $insert .= sprintf("'%s',", $column['name'])."\n"; |
| 11 | 71 } |
| 72 | |
| 73 return $insert; | |
| 74 } | |
| 75 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
76 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
77 * Get formatted validation rules for table columns. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
78 */ |
| 14 | 79 protected function getValuesForValidation(): string |
| 11 | 80 { |
| 81 $insert = ''; | |
| 82 foreach ($this->get_columns() as $column) { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
83 if (in_array($column['name'], $this->columns_to_ignore)) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
84 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
85 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
86 $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; |
| 11 | 87 } |
| 88 | |
| 89 return $insert; | |
| 90 } | |
| 91 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
92 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
93 * Apply insertions in the target template. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
94 */ |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
95 public function apply_inserts(string $target): string |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
96 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
97 $inserts = $this->get_all_keywords($target); |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
98 $available_insertions = $this->get_available_inserts(); |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
99 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
100 return str_replace( |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 array_keys($available_insertions), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
102 $available_insertions, |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
103 $target |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
104 ); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
105 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
106 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
107 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
108 * Get available insertion points for the template. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
109 */ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
110 public function get_available_inserts(): array |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
111 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
112 return [ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
113 '// {{ valuesForCreation }}' => $this->getValuesForCreation(), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
114 '# {{ attributeInsertPoint }}' => $this->getAttributes(), |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
115 '# {{ fillableInsertPoint }}' => $this->getFillable(), |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
116 '// {{ valuesForValidation }}' => $this->getValuesForValidation(), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
117 ]; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
118 } |
| 5 | 119 } |
