annotate src/Replacer/TableReplacer.php @ 21:f0b0d014e448 main-dev

Cleaning up code based on AI overlord review
author Luka Sitas <sitas.luka.97@gmail.com>
date Wed, 26 Feb 2025 19:45:08 -0500
parents 19b7a8de0019
children 827efbf4d73c
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
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
9 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
10 * 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
11 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
12 * @return array
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
13 */
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
14 protected function get_columns(): array
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
15 {
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
16 if (is_null($this->columns)) {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
17 $this->columns = $this->getCurrentTable()->getColumns();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
18 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
19
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
20 return $this->columns;
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
21 }
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
22
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
23 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
24 * 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
25 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
26 * @return string
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
27 */
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
28 protected function getValuesForCreation(): string
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
29 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
30 $insert = '';
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
31 foreach ($this->get_columns() as $column) {
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
32 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column->getName(), $column->getName()) . "\n";
7
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
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
35 return $insert;
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
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
38 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
39 * 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
40 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
41 * @return string
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
42 */
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
43 protected function getAttributes(): string
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
44 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
45 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
46 foreach ($this->get_columns() as $column) {
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
47 $insert .= sprintf("'%s' => '',", $column->getName()) . "\n";
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
48 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
49
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
50 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
51 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
52
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
53 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
54 * 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
55 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
56 * @return string
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
57 */
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
58 protected function getValuesForValidation(): string
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
59 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
60 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
61 foreach ($this->get_columns() as $column) {
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
62 $insert .= sprintf("'%s' => 'nullable',", $column->getName()) . "\n";
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
63 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
64
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
65 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
66 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
67
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
68 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
69 * 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
70 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
71 * @param string $target
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
72 * @return string
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
73 */
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
74 public function apply_inserts(string $target): string
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
75 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
76 $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
77 $available_insertions = $this->get_available_inserts();
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
78
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
79 return str_replace(
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
80 array_keys($available_insertions),
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
81 $available_insertions,
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
82 $target
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
83 );
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
84 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
85
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
86 /**
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
87 * 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
88 *
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
89 * @return array
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
90 */
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
91 public function get_available_inserts(): array
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
92 {
21
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
93 return [
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
94 '// {{ valuesForCreation }}' => $this->getValuesForCreation(),
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
95 '# {{ attributeInsertPoint }}' => $this->getAttributes(),
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
96 '// {{ valuesForValidation }}' => $this->getValuesForValidation(),
f0b0d014e448 Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
97 ];
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
98 }
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
99 }