Skip to content

Commit 1a0a5bb

Browse files
committed
Adopt model casts method
1 parent f4071e3 commit 1a0a5bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+404
-275
lines changed

src/Generators/ModelGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function buildProperties(Model $model): string
215215

216216
$columns = $this->castableColumns($model->columns());
217217
if (!empty($columns)) {
218-
$properties[] = str_replace('[]', $this->pretty_print_array($columns), $this->filesystem->stub('model.casts.stub'));
218+
$properties[] = str_replace('[]', $this->pretty_print_array($columns, indent: 8), $this->filesystem->stub('model.casts.stub'));
219219
}
220220

221221
return trim(implode(PHP_EOL, array_filter($properties, fn ($property) => !empty(trim($property)))));
@@ -237,11 +237,11 @@ protected function fillableColumns(array $columns): array
237237
);
238238
}
239239

240-
private function pretty_print_array(array $data, bool $assoc = true): string
240+
private function pretty_print_array(array $data, bool $assoc = true, int $indent = 4): string
241241
{
242242
$output = var_export($data, true);
243-
$output = preg_replace('/^\s+/m', ' ', $output);
244-
$output = preg_replace(['/^array\s\(/', '/\)$/'], ['[', ' ]'], $output);
243+
$output = preg_replace('/^\s+/m', str_repeat(' ', $indent + 4), $output);
244+
$output = preg_replace(['/^array\s\(/', '/\)$/'], ['[', str_repeat(' ', $indent) . ']'], $output);
245245

246246
if (!$assoc) {
247247
$output = preg_replace('/^(\s+)[^=]+=>\s+/m', '$1', $output);

stubs/model.casts.stub

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
2-
* The attributes that should be cast to native types.
2+
* Get the attributes that should be cast.
33
*
4-
* @var array
4+
* @return array<string, string>
55
*/
6-
protected $casts = [];
6+
protected function casts(): array
7+
{
8+
return [];
9+
}

tests/fixtures/models/alias-relationships.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ class Salesman extends Model
2323
];
2424

2525
/**
26-
* The attributes that should be cast to native types.
26+
* Get the attributes that should be cast.
2727
*
28-
* @var array
28+
* @return array<string, string>
2929
*/
30-
protected $casts = [
31-
'id' => 'integer',
32-
'belongs_alias_id' => 'integer',
33-
];
30+
protected function casts(): array
31+
{
32+
return [
33+
'id' => 'integer',
34+
'belongs_alias_id' => 'integer',
35+
];
36+
}
3437

3538
public function lead(): HasOne
3639
{

tests/fixtures/models/all-column-types.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,34 @@ class AllType extends Model
7272
];
7373

7474
/**
75-
* The attributes that should be cast to native types.
75+
* Get the attributes that should be cast.
7676
*
77-
* @var array
77+
* @return array<string, string>
7878
*/
79-
protected $casts = [
80-
'bigInteger' => 'integer',
81-
'boolean' => 'boolean',
82-
'date' => 'date',
83-
'dateTime' => 'datetime',
84-
'dateTimeTz' => 'datetime',
85-
'decimal' => 'decimal',
86-
'double' => 'double',
87-
'float' => 'float',
88-
'json' => 'array',
89-
'mediumInteger' => 'integer',
90-
'nullableTimestamps' => 'timestamp',
91-
'smallInteger' => 'integer',
92-
'timestamp' => 'timestamp',
93-
'timestampTz' => 'timestamp',
94-
'tinyInteger' => 'integer',
95-
'unsignedBigInteger' => 'integer',
96-
'unsignedDecimal' => 'decimal',
97-
'unsignedInteger' => 'integer',
98-
'unsignedMediumInteger' => 'integer',
99-
'unsignedSmallInteger' => 'integer',
100-
'unsignedTinyInteger' => 'integer',
101-
];
79+
protected function casts(): array
80+
{
81+
return [
82+
'bigInteger' => 'integer',
83+
'boolean' => 'boolean',
84+
'date' => 'date',
85+
'dateTime' => 'datetime',
86+
'dateTimeTz' => 'datetime',
87+
'decimal' => 'decimal',
88+
'double' => 'double',
89+
'float' => 'float',
90+
'json' => 'array',
91+
'mediumInteger' => 'integer',
92+
'nullableTimestamps' => 'timestamp',
93+
'smallInteger' => 'integer',
94+
'timestamp' => 'timestamp',
95+
'timestampTz' => 'timestamp',
96+
'tinyInteger' => 'integer',
97+
'unsignedBigInteger' => 'integer',
98+
'unsignedDecimal' => 'decimal',
99+
'unsignedInteger' => 'integer',
100+
'unsignedMediumInteger' => 'integer',
101+
'unsignedSmallInteger' => 'integer',
102+
'unsignedTinyInteger' => 'integer',
103+
];
104+
}
102105
}

tests/fixtures/models/certificate-pascal-case-example.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ class Certificate extends Model
2525
];
2626

2727
/**
28-
* The attributes that should be cast to native types.
28+
* Get the attributes that should be cast.
2929
*
30-
* @var array
30+
* @return array<string, string>
3131
*/
32-
protected $casts = [
33-
'id' => 'integer',
34-
'certificate_type_id' => 'integer',
35-
'expiry_date' => 'date',
36-
];
32+
protected function casts(): array
33+
{
34+
return [
35+
'id' => 'integer',
36+
'certificate_type_id' => 'integer',
37+
'expiry_date' => 'date',
38+
];
39+
}
3740

3841
public function certificateType(): BelongsTo
3942
{

tests/fixtures/models/certificate-type-pascal-case-example.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ class CertificateType extends Model
2020
];
2121

2222
/**
23-
* The attributes that should be cast to native types.
23+
* Get the attributes that should be cast.
2424
*
25-
* @var array
25+
* @return array<string, string>
2626
*/
27-
protected $casts = [
28-
'id' => 'integer',
29-
];
27+
protected function casts(): array
28+
{
29+
return [
30+
'id' => 'integer',
31+
];
32+
}
3033

3134
public function certificates(): HasMany
3235
{

tests/fixtures/models/comment-global-namespace.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ class Comment extends Model
1818
protected $fillable = [];
1919

2020
/**
21-
* The attributes that should be cast to native types.
21+
* Get the attributes that should be cast.
2222
*
23-
* @var array
23+
* @return array<string, string>
2424
*/
25-
protected $casts = [
26-
'id' => 'integer',
27-
];
25+
protected function casts(): array
26+
{
27+
return [
28+
'id' => 'integer',
29+
];
30+
}
2831
}

tests/fixtures/models/comment.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ class Comment extends Model
1818
protected $fillable = [];
1919

2020
/**
21-
* The attributes that should be cast to native types.
21+
* Get the attributes that should be cast.
2222
*
23-
* @var array
23+
* @return array<string, string>
2424
*/
25-
protected $casts = [
26-
'id' => 'integer',
27-
];
25+
protected function casts(): array
26+
{
27+
return [
28+
'id' => 'integer',
29+
];
30+
}
2831
}

tests/fixtures/models/custom-models-namespace.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ class Tag extends Model
2222
];
2323

2424
/**
25-
* The attributes that should be cast to native types.
25+
* Get the attributes that should be cast.
2626
*
27-
* @var array
27+
* @return array<string, string>
2828
*/
29-
protected $casts = [
30-
'id' => 'integer',
31-
'active' => 'boolean',
32-
];
29+
protected function casts(): array
30+
{
31+
return [
32+
'id' => 'integer',
33+
'active' => 'boolean',
34+
];
35+
}
3336
}

tests/fixtures/models/custom-pivot-membership.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ class Membership extends Pivot
3535
];
3636

3737
/**
38-
* The attributes that should be cast to native types.
38+
* Get the attributes that should be cast.
3939
*
40-
* @var array
40+
* @return array<string, string>
4141
*/
42-
protected $casts = [
43-
'id' => 'integer',
44-
'user_id' => 'integer',
45-
'team_id' => 'integer',
46-
];
42+
protected function casts(): array
43+
{
44+
return [
45+
'id' => 'integer',
46+
'user_id' => 'integer',
47+
'team_id' => 'integer',
48+
];
49+
}
4750

4851
public function user(): BelongsTo
4952
{

0 commit comments

Comments
 (0)