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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
1 <?php
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
2
19
19b7a8de0019 updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents: 14
diff changeset
3 namespace Wizard\MagicForger\Replacer;
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
4
6
b46922d4a301 Update for psr compliance
luka
parents: 5
diff changeset
5 trait TableReplacer
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
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
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
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
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
28 }
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
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
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
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
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
47 protected function getAttributes(): string
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
48 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
49 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
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
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
71 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
72
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
73 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
74 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
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
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
79 protected function getValuesForValidation(): string
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
80 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
81 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
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
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
87 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
88
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
89 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
90 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
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
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
119 }