Mercurial > packages > magicforger
annotate src/Replacer/Replacer.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 | 1a717c7b211f |
| 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 |
| 5 use Illuminate\Support\Str; | |
| 6 | |
| 7 trait Replacer | |
| 8 { | |
| 9 /** | |
| 10 * Prefix and Suffix for controller. | |
| 11 * Usage is up to the user. | |
| 12 */ | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
13 protected string $controller_prefix = ''; |
| 5 | 14 |
| 15 /** | |
| 16 * Prefix and Suffix for controller. | |
| 17 * Usage is up to the user. | |
| 18 */ | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
19 protected string $controller_suffix = 'Controller'; |
| 5 | 20 |
| 21 /** | |
| 22 * Finds all places in a string that could be replaced. | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
23 * Returns an array of all potential replacements as they appear in the target. |
| 5 | 24 */ |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
25 public function get_all_keywords(string $target): array |
| 5 | 26 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
27 // find all matches to our expected syntax |
| 5 | 28 $matches = []; |
| 29 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); | |
| 30 // sort the array and return unique values | |
| 31 sort($matches[0]); | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
32 |
| 5 | 33 return array_values(array_unique($matches[0])); |
| 34 } | |
| 35 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
36 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
37 * Apply replacements to the target string. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
38 */ |
| 5 | 39 public function apply_replacements(string $target): string |
| 40 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
41 $inserts = $this->get_all_keywords($target); |
| 5 | 42 $available_replacements = $this->get_available_replacements(); |
| 43 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
44 return str_replace( |
| 5 | 45 array_keys($available_replacements), |
| 46 $available_replacements, | |
| 47 $target | |
| 48 ); | |
| 49 } | |
| 50 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
51 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
52 * Get available replacements for string replacements. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
53 */ |
| 14 | 54 public function get_available_replacements(): array |
| 5 | 55 { |
| 56 $table_name = $this->getTableInput(); | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
57 |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
58 return [ |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
59 '{{ class }}' => $this->getClassName($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
60 '{{ controllerName }}' => $this->controller_name($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
61 '{{ model }}' => $this->model_name($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
62 '{{ modelVariable }}' => $this->model_variable($table_name), |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
63 '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
64 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
65 '{{ requestUses }}' => $this->getRequestUses($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
66 '{{ rootNamespace }}' => $this->getRootNamespace(), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
67 '{{ storeRequest }}' => $this->store_request_name($table_name), |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
68 '{{ tableName }}' => $table_name, |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
69 '{{ updateRequest }}' => $this->update_request_name($table_name), |
| 5 | 70 ]; |
| 71 } | |
| 72 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
73 // Model and Controller Naming |
| 5 | 74 |
| 75 /** | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
76 * Generate model name in Studly case. |
| 5 | 77 */ |
| 78 public function model_name(string $name): string | |
| 79 { | |
| 80 return Str::singular(Str::studly($name)); | |
| 81 } | |
| 82 | |
| 83 /** | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
84 * Generate singular model variable name. |
| 5 | 85 */ |
| 86 public function model_variable(string $name): string | |
| 87 { | |
| 11 | 88 return Str::singular($name); |
| 5 | 89 } |
| 90 | |
| 91 /** | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
92 * Generate controller name using prefix/suffix and studly case. |
| 5 | 93 */ |
| 94 public function controller_name(string $name): string | |
| 95 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
96 return $this->controller_prefix. |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
97 $this->model_name($name). |
| 5 | 98 $this->controller_suffix; |
| 99 } | |
| 100 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
102 * Generate the store request name. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
103 */ |
| 5 | 104 public function store_request_name(string $name): string |
| 105 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
106 return 'Store'.$this->model_name($name).'Request'; |
| 5 | 107 } |
| 108 | |
|
21
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 * Generate the update request name. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
111 */ |
| 5 | 112 public function update_request_name(string $name): string |
| 113 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
114 return 'Update'.$this->model_name($name).'Request'; |
| 5 | 115 } |
| 116 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
117 // Namespace Methods |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
118 // These methods handle the formation of various namespaces used within the replacements. |
| 5 | 119 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
120 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
121 * Get the root namespace for the application. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
122 */ |
| 14 | 123 public function getRootNamespace(): string |
| 5 | 124 { |
| 125 return $this->laravel->getNamespace(); | |
| 126 } | |
| 127 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
128 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
129 * Get the model namespace. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
130 */ |
| 14 | 131 public function getModelNamespace(string $name = ''): string |
| 5 | 132 { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
133 return $this->getRootNamespace().'Models'; |
| 5 | 134 } |
| 135 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
136 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
137 * Get the fully-qualified namespaced model class. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
138 */ |
| 14 | 139 public function getNamespacedModel(string $name = ''): string |
| 5 | 140 { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
141 return $this->getModelNamespace().'\\'.$this->model_name($name); |
| 5 | 142 } |
| 143 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
144 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
145 * Get the controller namespace. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
146 */ |
| 14 | 147 public function getControllerNamespace(string $name = ''): string |
| 5 | 148 { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
149 return $this->getRootNamespace().'Http\\Controllers'; |
| 5 | 150 } |
| 151 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
152 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
153 * Get the request namespace. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
154 */ |
| 14 | 155 public function getRequestNamespace(string $name): string |
| 5 | 156 { |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
157 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); |
| 5 | 158 } |
| 159 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
160 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
161 * Get the store request namespace. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
162 */ |
| 14 | 163 public function getStoreRequestNamespace(string $name): string |
| 5 | 164 { |
| 165 return $this->getRequestNamespace($name); | |
| 166 } | |
| 167 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
168 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
169 * Get the update request namespace. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
170 */ |
| 14 | 171 public function getUpdateRequestNamespace(string $name): string |
| 5 | 172 { |
| 173 return $this->getRequestNamespace($name); | |
| 174 } | |
| 175 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
176 /** |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
177 * Get the request uses string for replacement. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
178 */ |
| 14 | 179 public function getRequestUses(string $name): string |
| 5 | 180 { |
| 181 return implode("\n", [ | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
182 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
21
diff
changeset
|
183 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', |
| 5 | 184 ]); |
| 185 } | |
| 186 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
187 // Text Manipulation |
| 5 | 188 |
| 189 /** | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
190 * Convert a string to a human-readable format. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
191 * Assumes camel case input. |
| 5 | 192 */ |
| 193 public function human_readable(string $name): string | |
| 194 { | |
| 195 return Str::title(Str::replace('_', ' ', $name)); | |
| 196 } | |
| 197 | |
| 198 /** | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
199 * Convert a string to a lowercase human-readable format. |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
200 * Assumes camel case input. |
| 5 | 201 */ |
| 202 public function human_readable_lc(string $name): string | |
| 203 { | |
| 204 return Str::lower($this->human_readable($name)); | |
| 205 } | |
| 206 } |
