Skip to content

Commit fd0b646

Browse files
author
Nathan Esayeas
authored
Removes $dates property from models on L8+ (#393)
1 parent eaa8300 commit fd0b646

File tree

6 files changed

+28
-59
lines changed

6 files changed

+28
-59
lines changed

src/Generators/ModelGenerator.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ protected function buildProperties(Model $model)
141141
$properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns), $this->files->stub('model.casts.stub'));
142142
}
143143

144-
$columns = $this->dateColumns($model->columns());
145-
if (! empty($columns)) {
146-
$properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model.dates.stub'));
144+
if (! Blueprint::isLaravel8OrHigher()) {
145+
$columns = $this->dateColumns($model->columns());
146+
if (! empty($columns)) {
147+
$properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model.dates.stub'));
148+
}
147149
}
148150

149151
return trim($properties);
@@ -285,6 +287,20 @@ function (Column $column) {
285287

286288
private function castForColumn(Column $column)
287289
{
290+
if (Blueprint::isLaravel8OrHigher()) {
291+
if ($column->dataType() === 'date') {
292+
return 'date';
293+
}
294+
295+
if (stripos($column->dataType(), 'datetime') !== false) {
296+
return 'datetime';
297+
}
298+
299+
if (stripos($column->dataType(), 'timestamp') !== false) {
300+
return 'timestamp';
301+
}
302+
}
303+
288304
if (stripos($column->dataType(), 'integer') || $column->dataType() === 'id') {
289305
return 'integer';
290306
}

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public function output_generates_models($definition, $path, $model)
6969
->with('model.casts.stub')
7070
->andReturn($this->stub('model.casts.stub'));
7171

72-
if (in_array($definition, ['drafts/readme-example.yaml', 'drafts/all-column-types.yaml'])) {
73-
$this->files->expects('stub')
74-
->with('model.dates.stub')
75-
->andReturn($this->stub('model.dates.stub'));
76-
}
77-
7872
$this->files->shouldReceive('stub')
7973
->with('model.method.stub')
8074
->andReturn($this->stub('model.method.stub'));
@@ -214,9 +208,6 @@ public function output_works_for_pascal_case_definition()
214208
->with('model.casts.stub')
215209
->andReturn($this->stub('model.casts.stub'))
216210
->twice();
217-
$this->files->expects('stub')
218-
->with('model.dates.stub')
219-
->andReturn(file_get_contents('stubs/model.dates.stub'));
220211
$this->files->expects('stub')
221212
->with('model.method.stub')
222213
->andReturn($this->stub('model.method.stub'))
@@ -421,12 +412,6 @@ public function output_generates_phpdoc_for_model($definition, $path, $model)
421412
->with('model.casts.stub')
422413
->andReturn($this->stub('model.casts.stub'));
423414

424-
if ($definition === 'drafts/readme-example.yaml') {
425-
$this->files->expects('stub')
426-
->with('model.dates.stub')
427-
->andReturn($this->stub('model.dates.stub'));
428-
}
429-
430415
$this->files->shouldReceive('stub')
431416
->with('model.method.stub')
432417
->andReturn($this->stub('model.method.stub'));

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@ class AllType extends Model
7575
protected $casts = [
7676
'bigInteger' => 'integer',
7777
'boolean' => 'boolean',
78+
'date' => 'date',
79+
'dateTime' => 'datetime',
80+
'dateTimeTz' => 'datetime',
7881
'decimal' => 'decimal',
7982
'double' => 'double',
8083
'float' => 'float',
8184
'json' => 'array',
8285
'mediumInteger' => 'integer',
86+
'nullableTimestamps' => 'timestamp',
8387
'smallInteger' => 'integer',
88+
'timestamp' => 'timestamp',
89+
'timestampTz' => 'timestamp',
8490
'tinyInteger' => 'integer',
8591
'unsignedBigInteger' => 'integer',
8692
'unsignedDecimal' => 'decimal',
@@ -90,20 +96,6 @@ class AllType extends Model
9096
'unsignedTinyInteger' => 'integer',
9197
];
9298

93-
/**
94-
* The attributes that should be mutated to dates.
95-
*
96-
* @var array
97-
*/
98-
protected $dates = [
99-
'date',
100-
'dateTime',
101-
'dateTimeTz',
102-
'nullableTimestamps',
103-
'timestamp',
104-
'timestampTz',
105-
];
106-
10799

108100
public function uuid()
109101
{

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ class Certificate extends Model
3131
protected $casts = [
3232
'id' => 'integer',
3333
'certificate_type_id' => 'integer',
34-
];
35-
36-
/**
37-
* The attributes that should be mutated to dates.
38-
*
39-
* @var array
40-
*/
41-
protected $dates = [
42-
'expiry_date',
34+
'expiry_date' => 'date',
4335
];
4436

4537

tests/fixtures/models/readme-example-laravel8.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ class Post extends Model
2828
*/
2929
protected $casts = [
3030
'id' => 'integer',
31+
'published_at' => 'timestamp',
3132
'author_id' => 'integer',
3233
];
3334

34-
/**
35-
* The attributes that should be mutated to dates.
36-
*
37-
* @var array
38-
*/
39-
protected $dates = [
40-
'published_at',
41-
];
42-
4335

4436
public function author()
4537
{

tests/fixtures/models/readme-example-phpdoc-laravel8.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,10 @@ class Post extends Model
3737
*/
3838
protected $casts = [
3939
'id' => 'integer',
40+
'published_at' => 'timestamp',
4041
'author_id' => 'integer',
4142
];
4243

43-
/**
44-
* The attributes that should be mutated to dates.
45-
*
46-
* @var array
47-
*/
48-
protected $dates = [
49-
'published_at',
50-
];
51-
5244

5345
/**
5446
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo

0 commit comments

Comments
 (0)