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

+4-4
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

+6-3
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

+9-6
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

+28-25
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

+10-7
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

+8-5
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

+8-5
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

+8-5
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

+9-6
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

+10-7
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
{

tests/fixtures/models/custom-pivot-table-name.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ class User extends Model
2929
];
3030

3131
/**
32-
* The attributes that should be cast to native types.
32+
* Get the attributes that should be cast.
3333
*
34-
* @var array
34+
* @return array<string, string>
3535
*/
36-
protected $casts = [
37-
'id' => 'integer',
38-
];
36+
protected function casts(): array
37+
{
38+
return [
39+
'id' => 'integer',
40+
];
41+
}
3942

4043
public function accounts(): BelongsToMany
4144
{

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ class Team 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 users(): BelongsToMany
3235
{

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ class User 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 teams(): BelongsToMany
3235
{

tests/fixtures/models/disable-auto-columns-phpdoc.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ class State extends Model
3333
];
3434

3535
/**
36-
* The attributes that should be cast to native types.
36+
* Get the attributes that should be cast.
3737
*
38-
* @var array
38+
* @return array<string, string>
3939
*/
40-
protected $casts = [
41-
'country_id' => 'integer',
42-
];
40+
protected function casts(): array
41+
{
42+
return [
43+
'country_id' => 'integer',
44+
];
45+
}
4346
}

tests/fixtures/models/disable-auto-columns.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ class State extends Model
2828
];
2929

3030
/**
31-
* The attributes that should be cast to native types.
31+
* Get the attributes that should be cast.
3232
*
33-
* @var array
33+
* @return array<string, string>
3434
*/
35-
protected $casts = [
36-
'country_id' => 'integer',
37-
];
35+
protected function casts(): array
36+
{
37+
return [
38+
'country_id' => 'integer',
39+
];
40+
}
3841
}

tests/fixtures/models/foreign-key-shorthand-phpdoc.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ class Comment extends Model
3030
];
3131

3232
/**
33-
* The attributes that should be cast to native types.
33+
* Get the attributes that should be cast.
3434
*
35-
* @var array
35+
* @return array<string, string>
3636
*/
37-
protected $casts = [
38-
'id' => 'integer',
39-
'post_id' => 'integer',
40-
'author_id' => 'integer',
41-
'ccid' => 'integer',
42-
];
37+
protected function casts(): array
38+
{
39+
return [
40+
'id' => 'integer',
41+
'post_id' => 'integer',
42+
'author_id' => 'integer',
43+
'ccid' => 'integer',
44+
];
45+
}
4346

4447
public function post(): BelongsTo
4548
{

0 commit comments

Comments
 (0)