comparison 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
comparison
equal deleted inserted replaced
22:ee8ef14e158d 23:827efbf4d73c
7 trait Replacer 7 trait Replacer
8 { 8 {
9 /** 9 /**
10 * Prefix and Suffix for controller. 10 * Prefix and Suffix for controller.
11 * Usage is up to the user. 11 * Usage is up to the user.
12 *
13 * @var string
14 */ 12 */
15 protected string $controller_prefix = ''; 13 protected string $controller_prefix = '';
16 14
17 /** 15 /**
18 * Prefix and Suffix for controller. 16 * Prefix and Suffix for controller.
19 * Usage is up to the user. 17 * Usage is up to the user.
20 *
21 * @var string
22 */ 18 */
23 protected string $controller_suffix = 'Controller'; 19 protected string $controller_suffix = 'Controller';
24 20
25 /** 21 /**
26 * Finds all places in a string that could be replaced. 22 * Finds all places in a string that could be replaced.
27 * Returns an array of all potential replacements as they appear in the target. 23 * Returns an array of all potential replacements as they appear in the target.
28 *
29 * @param string $target
30 * @return array
31 */ 24 */
32 public function get_all_keywords(string $target): array 25 public function get_all_keywords(string $target): array
33 { 26 {
34 // find all matches to our expected syntax 27 // find all matches to our expected syntax
35 $matches = []; 28 $matches = [];
40 return array_values(array_unique($matches[0])); 33 return array_values(array_unique($matches[0]));
41 } 34 }
42 35
43 /** 36 /**
44 * Apply replacements to the target string. 37 * Apply replacements to the target string.
45 *
46 * @param string $target
47 * @return string
48 */ 38 */
49 public function apply_replacements(string $target): string 39 public function apply_replacements(string $target): string
50 { 40 {
51 $inserts = $this->get_all_keywords($target); 41 $inserts = $this->get_all_keywords($target);
52 $available_replacements = $this->get_available_replacements(); 42 $available_replacements = $this->get_available_replacements();
58 ); 48 );
59 } 49 }
60 50
61 /** 51 /**
62 * Get available replacements for string replacements. 52 * Get available replacements for string replacements.
63 *
64 * @return array
65 */ 53 */
66 public function get_available_replacements(): array 54 public function get_available_replacements(): array
67 { 55 {
68 $table_name = $this->getTableInput(); 56 $table_name = $this->getTableInput();
69 57
70 return [ 58 return [
71 '{{ class }}' => $this->getClassName($table_name), 59 '{{ class }}' => $this->getClassName($table_name),
72 '{{ controllerName }}' => $this->controller_name($table_name), 60 '{{ controllerName }}' => $this->controller_name($table_name),
73 '{{ model }}' => $this->model_name($table_name), 61 '{{ model }}' => $this->model_name($table_name),
74 '{{ modelVariable }}' => $this->model_variable($table_name), 62 '{{ modelVariable }}' => $this->model_variable($table_name),
75 '{{ namespace }}' => $this->{'get' . $this->type . 'Namespace'}($table_name), 63 '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name),
76 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), 64 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name),
77 '{{ requestUses }}' => $this->getRequestUses($table_name), 65 '{{ requestUses }}' => $this->getRequestUses($table_name),
78 '{{ rootNamespace }}' => $this->getRootNamespace(), 66 '{{ rootNamespace }}' => $this->getRootNamespace(),
79 '{{ storeRequest }}' => $this->store_request_name($table_name), 67 '{{ storeRequest }}' => $this->store_request_name($table_name),
80 '{{ tableName }}' => $table_name, 68 '{{ tableName }}' => $table_name,
84 72
85 // Model and Controller Naming 73 // Model and Controller Naming
86 74
87 /** 75 /**
88 * Generate model name in Studly case. 76 * Generate model name in Studly case.
89 *
90 * @param string $name
91 * @return string
92 */ 77 */
93 public function model_name(string $name): string 78 public function model_name(string $name): string
94 { 79 {
95 return Str::singular(Str::studly($name)); 80 return Str::singular(Str::studly($name));
96 } 81 }
97 82
98 /** 83 /**
99 * Generate singular model variable name. 84 * Generate singular model variable name.
100 *
101 * @param string $name
102 * @return string
103 */ 85 */
104 public function model_variable(string $name): string 86 public function model_variable(string $name): string
105 { 87 {
106 return Str::singular($name); 88 return Str::singular($name);
107 } 89 }
108 90
109 /** 91 /**
110 * Generate controller name using prefix/suffix and studly case. 92 * Generate controller name using prefix/suffix and studly case.
111 *
112 * @param string $name
113 * @return string
114 */ 93 */
115 public function controller_name(string $name): string 94 public function controller_name(string $name): string
116 { 95 {
117 return $this->controller_prefix . 96 return $this->controller_prefix.
118 $this->model_name($name) . 97 $this->model_name($name).
119 $this->controller_suffix; 98 $this->controller_suffix;
120 } 99 }
121 100
122 /** 101 /**
123 * Generate the store request name. 102 * Generate the store request name.
124 *
125 * @param string $name
126 * @return string
127 */ 103 */
128 public function store_request_name(string $name): string 104 public function store_request_name(string $name): string
129 { 105 {
130 return 'Store' . $this->model_name($name) . 'Request'; 106 return 'Store'.$this->model_name($name).'Request';
131 } 107 }
132 108
133 /** 109 /**
134 * Generate the update request name. 110 * Generate the update request name.
135 *
136 * @param string $name
137 * @return string
138 */ 111 */
139 public function update_request_name(string $name): string 112 public function update_request_name(string $name): string
140 { 113 {
141 return 'Update' . $this->model_name($name) . 'Request'; 114 return 'Update'.$this->model_name($name).'Request';
142 } 115 }
143 116
144 // Namespace Methods 117 // Namespace Methods
145 // These methods handle the formation of various namespaces used within the replacements. 118 // These methods handle the formation of various namespaces used within the replacements.
146 119
147 /** 120 /**
148 * Get the root namespace for the application. 121 * Get the root namespace for the application.
149 *
150 * @return string
151 */ 122 */
152 public function getRootNamespace(): string 123 public function getRootNamespace(): string
153 { 124 {
154 return $this->laravel->getNamespace(); 125 return $this->laravel->getNamespace();
155 } 126 }
156 127
157 /** 128 /**
158 * Get the model namespace. 129 * Get the model namespace.
159 *
160 * @param string $name
161 * @return string
162 */ 130 */
163 public function getModelNamespace(string $name = ''): string 131 public function getModelNamespace(string $name = ''): string
164 { 132 {
165 return $this->getRootNamespace() . 'Models'; 133 return $this->getRootNamespace().'Models';
166 } 134 }
167 135
168 /** 136 /**
169 * Get the fully-qualified namespaced model class. 137 * Get the fully-qualified namespaced model class.
170 *
171 * @param string $name
172 * @return string
173 */ 138 */
174 public function getNamespacedModel(string $name = ''): string 139 public function getNamespacedModel(string $name = ''): string
175 { 140 {
176 return $this->getModelNamespace() . '\\' . $this->model_name($name); 141 return $this->getModelNamespace().'\\'.$this->model_name($name);
177 } 142 }
178 143
179 /** 144 /**
180 * Get the controller namespace. 145 * Get the controller namespace.
181 *
182 * @param string $name
183 * @return string
184 */ 146 */
185 public function getControllerNamespace(string $name = ''): string 147 public function getControllerNamespace(string $name = ''): string
186 { 148 {
187 return $this->getRootNamespace() . 'Http\\Controllers'; 149 return $this->getRootNamespace().'Http\\Controllers';
188 } 150 }
189 151
190 /** 152 /**
191 * Get the request namespace. 153 * Get the request namespace.
192 *
193 * @param string $name
194 * @return string
195 */ 154 */
196 public function getRequestNamespace(string $name): string 155 public function getRequestNamespace(string $name): string
197 { 156 {
198 return $this->getRootNamespace() . 'Http\\Requests\\' . $this->model_name($name); 157 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name);
199 } 158 }
200 159
201 /** 160 /**
202 * Get the store request namespace. 161 * Get the store request namespace.
203 *
204 * @param string $name
205 * @return string
206 */ 162 */
207 public function getStoreRequestNamespace(string $name): string 163 public function getStoreRequestNamespace(string $name): string
208 { 164 {
209 return $this->getRequestNamespace($name); 165 return $this->getRequestNamespace($name);
210 } 166 }
211 167
212 /** 168 /**
213 * Get the update request namespace. 169 * Get the update request namespace.
214 *
215 * @param string $name
216 * @return string
217 */ 170 */
218 public function getUpdateRequestNamespace(string $name): string 171 public function getUpdateRequestNamespace(string $name): string
219 { 172 {
220 return $this->getRequestNamespace($name); 173 return $this->getRequestNamespace($name);
221 } 174 }
222 175
223 /** 176 /**
224 * Get the request uses string for replacement. 177 * Get the request uses string for replacement.
225 *
226 * @param string $name
227 * @return string
228 */ 178 */
229 public function getRequestUses(string $name): string 179 public function getRequestUses(string $name): string
230 { 180 {
231 return implode("\n", [ 181 return implode("\n", [
232 'use ' . $this->getRequestNamespace($name) . '\\' . $this->store_request_name($name) . ';', 182 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';',
233 'use ' . $this->getRequestNamespace($name) . '\\' . $this->update_request_name($name) . ';', 183 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';',
234 ]); 184 ]);
235 } 185 }
236 186
237 // Text Manipulation 187 // Text Manipulation
238 188
239 /** 189 /**
240 * Convert a string to a human-readable format. 190 * Convert a string to a human-readable format.
241 * Assumes camel case input. 191 * Assumes camel case input.
242 *
243 * @param string $name
244 * @return string
245 */ 192 */
246 public function human_readable(string $name): string 193 public function human_readable(string $name): string
247 { 194 {
248 return Str::title(Str::replace('_', ' ', $name)); 195 return Str::title(Str::replace('_', ' ', $name));
249 } 196 }
250 197
251 /** 198 /**
252 * Convert a string to a lowercase human-readable format. 199 * Convert a string to a lowercase human-readable format.
253 * Assumes camel case input. 200 * Assumes camel case input.
254 *
255 * @param string $name
256 * @return string
257 */ 201 */
258 public function human_readable_lc(string $name): string 202 public function human_readable_lc(string $name): string
259 { 203 {
260 return Str::lower($this->human_readable($name)); 204 return Str::lower($this->human_readable($name));
261 } 205 }