Mercurial > packages > magicforger
annotate src/Replacer/TableReplacer.php @ 31:8dd668020310 codex
started converting too bootstrap, also better support for casts
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Tue, 19 Aug 2025 20:34:53 -0400 |
| parents | 827efbf4d73c |
| children | b5c6ebd33547 |
| 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 */ |
|
31
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
47 protected function getCasts(): string |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
48 { |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
49 $insert = ''; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
50 foreach ($this->get_columns() as $column) { |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
51 if (in_array($column['name'], $this->columns_to_ignore)) { |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
52 continue; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
53 } |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
54 $type = $column['type_name']; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
55 //date |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
56 if(in_array($type, ['date'])) { |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
57 $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n"; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
58 } |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
59 //time |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
60 if(in_array($type, ['timestamp'])) { |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
61 $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n"; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
62 } |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
63 } |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
64 |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
65 return $insert; |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
66 } |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
67 |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
68 /** |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
69 * Get a string representation of table attributes. |
|
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
70 */ |
| 14 | 71 protected function getAttributes(): string |
| 11 | 72 { |
| 73 $insert = ''; | |
| 74 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
|
75 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
|
76 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
77 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
78 $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
|
79 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
80 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
81 return $insert; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
82 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
83 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
84 /** |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
85 * 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
|
86 */ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
87 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
|
88 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
89 $insert = ''; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
90 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
|
91 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
|
92 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
93 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
94 $insert .= sprintf("'%s',", $column['name'])."\n"; |
| 11 | 95 } |
| 96 | |
| 97 return $insert; | |
| 98 } | |
| 99 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
100 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 * 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
|
102 */ |
| 14 | 103 protected function getValuesForValidation(): string |
| 11 | 104 { |
| 105 $insert = ''; | |
| 106 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
|
107 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
|
108 continue; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
109 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
110 $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; |
| 11 | 111 } |
| 112 | |
| 113 return $insert; | |
| 114 } | |
| 115 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
116 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
117 * 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
|
118 */ |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
119 public function apply_inserts(string $target): string |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
120 { |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
121 $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
|
122 $available_insertions = $this->get_available_inserts(); |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
123 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
124 return str_replace( |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
125 array_keys($available_insertions), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
126 $available_insertions, |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
127 $target |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
128 ); |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
129 } |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
130 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
131 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
132 * 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
|
133 */ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
134 public function get_available_inserts(): array |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
135 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
136 return [ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
137 '// {{ valuesForCreation }}' => $this->getValuesForCreation(), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
138 '# {{ attributeInsertPoint }}' => $this->getAttributes(), |
|
31
8dd668020310
started converting too bootstrap, also better support for casts
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
139 '# {{ castInsertPoint }}' => $this->getCasts(), |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
140 '# {{ fillableInsertPoint }}' => $this->getFillable(), |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
141 '// {{ valuesForValidation }}' => $this->getValuesForValidation(), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
142 ]; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
6
diff
changeset
|
143 } |
| 5 | 144 } |
