Skip to content

Commit 0061d0e

Browse files
Cast date columns (#341)
1 parent 545d1a2 commit 0061d0e

File tree

4 files changed

+126
-6
lines changed

4 files changed

+126
-6
lines changed

src/Generators/ModelGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ function (Column $column) {
254254
return $column->name();
255255
},
256256
array_filter($columns, function (Column $column) {
257-
return stripos($column->dataType(), 'datetime') !== false
257+
return $column->dataType() === 'date'
258+
|| stripos($column->dataType(), 'datetime') !== false
258259
|| stripos($column->dataType(), 'timestamp') !== false;
259260
})
260261
);

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function output_generates_models($definition, $path, $model)
5656
->with('model.fillable.stub')
5757
->andReturn(file_get_contents('stubs/model.fillable.stub'));
5858

59-
if (in_array($definition, ['drafts/nested-components.yaml','drafts/resource-statements.yaml'])) {
59+
if (in_array($definition, ['drafts/nested-components.yaml', 'drafts/resource-statements.yaml'])) {
6060
$this->files->expects('stub')
6161
->with('model.hidden.stub')
6262
->andReturn(file_get_contents('stubs/model.hidden.stub'));
@@ -66,7 +66,7 @@ public function output_generates_models($definition, $path, $model)
6666
->with('model.casts.stub')
6767
->andReturn(file_get_contents('stubs/model.casts.stub'));
6868

69-
if ($definition === 'drafts/readme-example.yaml') {
69+
if (in_array($definition, ['drafts/readme-example.yaml', 'drafts/all-column-types.yaml'])) {
7070
$this->files->expects('stub')
7171
->with('model.dates.stub')
7272
->andReturn(file_get_contents('stubs/model.dates.stub'));
@@ -109,6 +109,9 @@ public function output_works_for_pascal_case_definition()
109109
->with('model.casts.stub')
110110
->andReturn(file_get_contents('stubs/model.casts.stub'))
111111
->twice();
112+
$this->files->expects('stub')
113+
->with('model.dates.stub')
114+
->andReturn(file_get_contents('stubs/model.dates.stub'));
112115
$this->files->expects('stub')
113116
->with('model.method.stub')
114117
->andReturn(file_get_contents('stubs/model.method.stub'))
@@ -255,15 +258,12 @@ public function output_respects_configuration()
255258
$this->files->expects('stub')
256259
->with('model.class.stub')
257260
->andReturn(file_get_contents('stubs/model.class.stub'));
258-
259261
$this->files->expects('stub')
260262
->with('model.fillable.stub')
261263
->andReturn(file_get_contents('stubs/model.fillable.stub'));
262-
263264
$this->files->expects('stub')
264265
->with('model.casts.stub')
265266
->andReturn(file_get_contents('stubs/model.casts.stub'));
266-
267267
$this->files->expects('stub')
268268
->with('model.method.stub')
269269
->andReturn(file_get_contents('stubs/model.method.stub'));
@@ -450,6 +450,7 @@ public function modelTreeDataProvider()
450450
['drafts/unconventional.yaml', 'app/Team.php', 'models/unconventional.php'],
451451
['drafts/nested-components.yaml', 'app/Admin/User.php', 'models/nested-components.php'],
452452
['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements.php'],
453+
['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types.php'],
453454
];
454455
}
455456

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class AllType extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'bigInteger',
16+
'binary',
17+
'boolean',
18+
'char',
19+
'date',
20+
'dateTime',
21+
'dateTimeTz',
22+
'decimal',
23+
'double',
24+
'enum',
25+
'float',
26+
'geometry',
27+
'geometryCollection',
28+
'integer',
29+
'ipAddress',
30+
'json',
31+
'jsonb',
32+
'lineString',
33+
'longText',
34+
'macAddress',
35+
'mediumInteger',
36+
'mediumText',
37+
'morphs',
38+
'uuidMorphs',
39+
'multiLineString',
40+
'multiPoint',
41+
'multiPolygon',
42+
'nullableMorphs',
43+
'nullableUuidMorphs',
44+
'nullableTimestamps',
45+
'point',
46+
'polygon',
47+
'rememberToken',
48+
'set',
49+
'smallInteger',
50+
'string',
51+
'text',
52+
'time',
53+
'timeTz',
54+
'timestamp',
55+
'timestampTz',
56+
'tinyInteger',
57+
'unsignedBigInteger',
58+
'unsignedDecimal',
59+
'unsignedInteger',
60+
'unsignedMediumInteger',
61+
'unsignedSmallInteger',
62+
'unsignedTinyInteger',
63+
'uuid',
64+
'year',
65+
];
66+
67+
/**
68+
* The attributes that should be cast to native types.
69+
*
70+
* @var array
71+
*/
72+
protected $casts = [
73+
'bigInteger' => 'integer',
74+
'boolean' => 'boolean',
75+
'decimal' => 'decimal',
76+
'double' => 'double',
77+
'float' => 'float',
78+
'json' => 'array',
79+
'mediumInteger' => 'integer',
80+
'smallInteger' => 'integer',
81+
'tinyInteger' => 'integer',
82+
'unsignedBigInteger' => 'integer',
83+
'unsignedDecimal' => 'decimal',
84+
'unsignedInteger' => 'integer',
85+
'unsignedMediumInteger' => 'integer',
86+
'unsignedSmallInteger' => 'integer',
87+
'unsignedTinyInteger' => 'integer',
88+
];
89+
90+
/**
91+
* The attributes that should be mutated to dates.
92+
*
93+
* @var array
94+
*/
95+
protected $dates = [
96+
'date',
97+
'dateTime',
98+
'dateTimeTz',
99+
'nullableTimestamps',
100+
'timestamp',
101+
'timestampTz',
102+
];
103+
104+
105+
public function uuid()
106+
{
107+
return $this->belongsTo(\App\Uuid::class);
108+
}
109+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class Certificate extends Model
3030
'certificate_type_id' => 'integer',
3131
];
3232

33+
/**
34+
* The attributes that should be mutated to dates.
35+
*
36+
* @var array
37+
*/
38+
protected $dates = [
39+
'expiry_date',
40+
];
41+
3342

3443
public function certificateType()
3544
{

0 commit comments

Comments
 (0)