4
4
5
5
namespace GiacomoMasseroni \LaravelModelsGenerator \Writers \Laravel11 ;
6
6
7
+ use GiacomoMasseroni \LaravelModelsGenerator \Entities \Property ;
7
8
use GiacomoMasseroni \LaravelModelsGenerator \Writers \WriterInterface ;
8
9
use Illuminate \Support \Str ;
9
10
@@ -20,8 +21,8 @@ public function imports(): string
20
21
21
22
public function properties (): string
22
23
{
23
- return implode ("\n" , array_map (function (string $ property ) {
24
- return " * @property $ property" ;
24
+ return implode ("\n" , array_map (function (Property $ property ) {
25
+ return ' * @property ' .( $ property -> readOnly ? ' -read ' : '' ). ' ' . $ property-> return . ' ' . $ property -> field ;
25
26
}, $ this ->table ->properties ));
26
27
}
27
28
@@ -171,19 +172,14 @@ private function hasMany(): string
171
172
{
172
173
$ content = '' ;
173
174
foreach ($ this ->table ->hasMany as $ hasMany ) {
174
- if ($ this ->table ->thereIsAnotherHasMany ($ hasMany )) {
175
- $ relationName = Str::camel (Str::plural ($ hasMany ->name )).'As ' .ucfirst (Str::camel (str_replace ($ this ->table ->primaryKey , '' , $ hasMany ->foreignKeyName )));
176
- } else {
177
- $ relationName = Str::camel (Str::plural ($ hasMany ->name ));
178
- }
179
175
$ relatedClassName = ucfirst (Str::camel ($ hasMany ->related ));
180
176
181
177
$ content .= "\n" ."\n" ;
182
178
183
179
$ content .= $ this ->spacer .'/** ' ."\n" ;
184
180
$ content .= $ this ->spacer .' * @return HasMany< ' .$ relatedClassName .', $this> ' ."\n" ;
185
181
$ content .= $ this ->spacer .' */ ' ."\n" ;
186
- $ content .= $ this ->spacer .'public function ' .$ relationName .'(): HasMany ' ."\n" ;
182
+ $ content .= $ this ->spacer .'public function ' .$ hasMany -> name .'(): HasMany ' ."\n" ;
187
183
$ content .= $ this ->spacer .'{ ' ."\n" ;
188
184
$ content .= str_repeat ($ this ->spacer , 2 ).'return $this->hasMany( ' .$ relatedClassName .'::class, \'' .$ hasMany ->foreignKeyName .'\'' .(! empty ($ hasMany ->localKeyName ) ? ', \'' .$ hasMany ->localKeyName .'\'' : '' ).'); ' ."\n" ;
189
185
$ content .= $ this ->spacer .'} ' ;
@@ -196,21 +192,13 @@ private function belongTo(): string
196
192
{
197
193
$ content = '' ;
198
194
foreach ($ this ->table ->belongsTo as $ belongsTo ) {
199
- $ foreignClassName = ucfirst (Str::camel (Str::singular ($ belongsTo ->foreignKey ->getForeignTableName ())));
200
- $ foreignColumnName = $ belongsTo ->foreignKey ->getForeignColumns ()[0 ];
201
- $ localColumnName = $ belongsTo ->foreignKey ->getLocalColumns ()[0 ];
202
- if (str_contains ($ localColumnName , $ foreignColumnName ) && $ localColumnName != $ foreignColumnName ) {
203
- $ relationName = Str::camel (str_replace ($ foreignColumnName , '' , $ localColumnName ));
204
- } else {
205
- $ relationName = Str::camel (Str::singular ($ belongsTo ->foreignKey ->getForeignTableName ()));
206
- }
207
195
$ content .= "\n" ."\n" ;
208
196
$ content .= $ this ->spacer .'/** ' ."\n" ;
209
- $ content .= $ this ->spacer .' * @return BelongsTo< ' .$ foreignClassName .', $this> ' ."\n" ;
197
+ $ content .= $ this ->spacer .' * @return BelongsTo< ' .$ belongsTo -> foreignClassName .', $this> ' ."\n" ;
210
198
$ content .= $ this ->spacer .' */ ' ."\n" ;
211
- $ content .= $ this ->spacer .'public function ' .$ relationName .'(): BelongsTo ' ."\n" ;
199
+ $ content .= $ this ->spacer .'public function ' .$ belongsTo -> name .'(): BelongsTo ' ."\n" ;
212
200
$ content .= $ this ->spacer .'{ ' ."\n" ;
213
- $ content .= str_repeat ($ this ->spacer , 2 ).'return $this->belongsTo( ' .$ foreignClassName .'::class, \'' .$ foreignColumnName .'\'' .($ localColumnName != $ this ->table ->primaryKey ? ', \'' .$ localColumnName .'\'' : '' ).'); ' ."\n" ;
201
+ $ content .= str_repeat ($ this ->spacer , 2 ).'return $this->belongsTo( ' .$ belongsTo -> foreignClassName .'::class, \'' .$ belongsTo -> foreignColumnName .'\'' .($ belongsTo -> localColumnName != $ this ->table ->primaryKey ? ', \'' .$ belongsTo -> localColumnName .'\'' : '' ).'); ' ."\n" ;
214
202
$ content .= $ this ->spacer .'} ' ;
215
203
}
216
204
@@ -222,30 +210,18 @@ private function belongsToMany(): string
222
210
$ content = '' ;
223
211
224
212
foreach ($ this ->table ->belongsToMany as $ belongsToMany ) {
225
- if ($ belongsToMany ->pivot == $ this ->table ->name .'_ ' .$ belongsToMany ->related ||
226
- $ belongsToMany ->pivot == $ belongsToMany ->related .'_ ' .$ this ->table ->name ) {
227
- $ relationName = Str::camel (Str::plural ($ belongsToMany ->related ));
228
- } else {
229
- if (Str::start ($ belongsToMany ->related , $ belongsToMany ->pivot )) {
230
- $ related = str_replace ($ belongsToMany ->pivot .'_ ' , '' , $ belongsToMany ->related );
231
- } else {
232
- $ related = $ belongsToMany ->related ;
233
- }
234
- $ relationName = Str::camel (str_replace ("{$ this ->table ->name }_ " , '' , $ belongsToMany ->pivot ).'_ ' .Str::plural ($ related ));
235
- }
213
+ $ withPivot = count ($ belongsToMany ->pivotAttributes );
236
214
237
- $ foreignClassName = ucfirst (Str::camel (Str::singular ($ belongsToMany ->related )));
238
- //$foreignColumnName = $belongsTo->foreignKey->getForeignColumns()[0];
239
215
$ content .= "\n" ."\n" ;
240
216
$ content .= $ this ->spacer .'/** ' ."\n" ;
241
- $ content .= $ this ->spacer .' * @return BelongsToMany< ' .$ foreignClassName .', $this> ' ."\n" ;
217
+ $ content .= $ this ->spacer .' * @return BelongsToMany< ' .$ belongsToMany -> foreignClassName .', $this> ' ."\n" ;
242
218
$ content .= $ this ->spacer .' */ ' ."\n" ;
243
- $ content .= $ this ->spacer .'public function ' .$ relationName .'(): BelongsToMany ' ."\n" ;
219
+ $ content .= $ this ->spacer .'public function ' .$ belongsToMany -> name .'(): BelongsToMany ' ."\n" ;
244
220
$ content .= $ this ->spacer .'{ ' ."\n" ;
245
- $ content .= str_repeat ($ this ->spacer , 2 ).'return $this->belongsToMany( ' .$ foreignClassName .'::class, \'' .$ belongsToMany ->pivot .'\', \'' .$ belongsToMany ->foreignPivotKey .'\', \'' .$ belongsToMany ->relatedPivotKey .'\') ' ."\n" ;
246
- $ content .= str_repeat ($ this ->spacer , 3 ).(count ($ belongsToMany ->pivotAttributes ) > 0 ? '->withPivot( \'' .implode ('\', \'' , $ belongsToMany ->pivotAttributes ).'\') ' : '' )."\n" ;
247
- $ content .= str_repeat ($ this ->spacer , 3 ).( $ belongsToMany -> timestamps ? '->withTimestamps() ' : '' ). ' ; ' ."\n" ;
248
- $ content .= '} ' ;
221
+ $ content .= str_repeat ($ this ->spacer , 2 ).'return $this->belongsToMany( ' .$ belongsToMany -> foreignClassName .'::class, \'' .$ belongsToMany ->pivot .'\', \'' .$ belongsToMany ->foreignPivotKey .'\', \'' .$ belongsToMany ->relatedPivotKey .'\') ' .(! $ withPivot ? ' ; ' : '' ) ."\n" ;
222
+ $ content .= $ withPivot ? str_repeat ($ this ->spacer , 3 ).(count ($ belongsToMany ->pivotAttributes ) > 0 ? '->withPivot( \'' .implode ('\', \'' , $ belongsToMany ->pivotAttributes ).'\') ' : '' ).(! $ belongsToMany -> timestamps ? ' ; ' : '' ). "\n" : '' ;
223
+ $ content .= $ belongsToMany -> timestamps ? str_repeat ($ this ->spacer , 3 ).'->withTimestamps(); ' ."\n" : '' ;
224
+ $ content .= $ this -> spacer . '} ' ;
249
225
}
250
226
251
227
return $ content ;
0 commit comments