Skip to content

Commit f4b4ffc

Browse files
miken32d3v2a
authored andcommitted
Improve casts support (barryvdh#1262)
* support for all possible `Model::$casts` types * add testing for casts * update changelog * update changelog (again?)
1 parent 8d33097 commit f4b4ffc

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44

55
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.10.0...master)
66
--------------
7+
### Added
8+
- Add support for cast types `decimal:*`, `encrypted:*`, `immutable_date`, `immutable_datetime`, `custom_datetime`, and `immutable_custom_datetime` [#1262 / miken32](https://github.com/barryvdh/laravel-ide-helper/pull/1262)
9+
710
### Fixed
811
- Fix recursively searching for `HasFactory` and `Macroable` traits [\#1216 / daniel-de-wit](https://github.com/barryvdh/laravel-ide-helper/pull/1216)
912

src/Console/ModelsCommand.php

+17
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,24 @@ public function castPropertiesType($model)
355355
{
356356
$casts = $model->getCasts();
357357
foreach ($casts as $name => $type) {
358+
if (Str::startsWith($type, 'decimal:')) {
359+
$type = 'decimal';
360+
} elseif (Str::startsWith($type, 'custom_datetime:')) {
361+
$type = 'date';
362+
} elseif (Str::startsWith($type, 'immutable_custom_datetime:')) {
363+
$type = 'immutable_date';
364+
} elseif (Str::startsWith($type, 'encrypted:')) {
365+
$type = Str::after($type, ':');
366+
}
358367
switch ($type) {
368+
case 'encrypted':
369+
$realType = 'mixed';
370+
break;
359371
case 'boolean':
360372
case 'bool':
361373
$realType = 'boolean';
362374
break;
375+
case 'decimal':
363376
case 'string':
364377
$realType = 'string';
365378
break;
@@ -384,6 +397,10 @@ public function castPropertiesType($model)
384397
case 'datetime':
385398
$realType = $this->dateClass;
386399
break;
400+
case 'immutable_date':
401+
case 'immutable_datetime':
402+
$realType = '\Carbon\CarbonImmutable';
403+
break;
387404
case 'collection':
388405
$realType = '\Illuminate\Support\Collection';
389406
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\SimpleCasts\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class SimpleCast extends Model
10+
{
11+
protected $casts = [
12+
'cast_to_int' => 'int',
13+
'cast_to_integer' => 'integer',
14+
'cast_to_real' => 'real',
15+
'cast_to_float' => 'float',
16+
'cast_to_double' => 'double',
17+
'cast_to_decimal' => 'decimal:4',
18+
'cast_to_string' => 'string',
19+
'cast_to_bool' => 'bool',
20+
'cast_to_boolean' => 'boolean',
21+
'cast_to_object' => 'object',
22+
'cast_to_array' => 'array',
23+
'cast_to_json' => 'json',
24+
'cast_to_collection' => 'collection',
25+
'cast_to_date' => 'date',
26+
'cast_to_datetime' => 'datetime',
27+
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
28+
'cast_to_immutable_date' => 'immutable_date',
29+
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
30+
'cast_to_immutable_datetime' => 'immutable_datetime',
31+
'cast_to_timestamp' => 'timestamp',
32+
'cast_to_encrypted' => 'encrypted',
33+
'cast_to_encrypted_array' => 'encrypted:array',
34+
'cast_to_encrypted_collection' => 'encrypted:collection',
35+
'cast_to_encrypted_json' => 'encrypted:json',
36+
'cast_to_encrypted_object' => 'encrypted:object',
37+
];
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\SimpleCasts;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
10+
class Test extends AbstractModelsCommand
11+
{
12+
public function test(): void
13+
{
14+
$command = $this->app->make(ModelsCommand::class);
15+
16+
$tester = $this->runCommand($command, [
17+
'--write' => true,
18+
]);
19+
20+
$this->assertSame(0, $tester->getStatusCode());
21+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
22+
$this->assertMatchesMockedSnapshot();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\SimpleCasts\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
/**
10+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\SimpleCasts\Models\SimpleCast
11+
*
12+
* @property integer $cast_to_int
13+
* @property integer $cast_to_integer
14+
* @property float $cast_to_real
15+
* @property float $cast_to_float
16+
* @property float $cast_to_double
17+
* @property string $cast_to_decimal
18+
* @property string $cast_to_string
19+
* @property boolean $cast_to_bool
20+
* @property boolean $cast_to_boolean
21+
* @property object $cast_to_object
22+
* @property array $cast_to_array
23+
* @property array $cast_to_json
24+
* @property \Illuminate\Support\Collection $cast_to_collection
25+
* @property \Illuminate\Support\Carbon $cast_to_date
26+
* @property \Illuminate\Support\Carbon $cast_to_datetime
27+
* @property \Illuminate\Support\Carbon $cast_to_custom_datetime
28+
* @property \Carbon\CarbonImmutable $cast_to_immutable_date
29+
* @property \Carbon\CarbonImmutable $cast_to_immutable_custom_datetime
30+
* @property \Carbon\CarbonImmutable $cast_to_immutable_datetime
31+
* @property integer $cast_to_timestamp
32+
* @property mixed $cast_to_encrypted
33+
* @property array $cast_to_encrypted_array
34+
* @property \Illuminate\Support\Collection $cast_to_encrypted_collection
35+
* @property array $cast_to_encrypted_json
36+
* @property object $cast_to_encrypted_object
37+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast newModelQuery()
38+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast newQuery()
39+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast query()
40+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToArray($value)
41+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToBool($value)
42+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToBoolean($value)
43+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToCollection($value)
44+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToCustomDatetime($value)
45+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToDate($value)
46+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToDatetime($value)
47+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToDecimal($value)
48+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToDouble($value)
49+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToEncrypted($value)
50+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToEncryptedArray($value)
51+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToEncryptedCollection($value)
52+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToEncryptedJson($value)
53+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToEncryptedObject($value)
54+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToFloat($value)
55+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToImmutableCustomDatetime($value)
56+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToImmutableDate($value)
57+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToImmutableDatetime($value)
58+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToInt($value)
59+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToInteger($value)
60+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToJson($value)
61+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToObject($value)
62+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToReal($value)
63+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToString($value)
64+
* @method static \Illuminate\Database\Eloquent\Builder|SimpleCast whereCastToTimestamp($value)
65+
* @mixin \Eloquent
66+
*/
67+
class SimpleCast extends Model
68+
{
69+
protected $casts = [
70+
'cast_to_int' => 'int',
71+
'cast_to_integer' => 'integer',
72+
'cast_to_real' => 'real',
73+
'cast_to_float' => 'float',
74+
'cast_to_double' => 'double',
75+
'cast_to_decimal' => 'decimal:4',
76+
'cast_to_string' => 'string',
77+
'cast_to_bool' => 'bool',
78+
'cast_to_boolean' => 'boolean',
79+
'cast_to_object' => 'object',
80+
'cast_to_array' => 'array',
81+
'cast_to_json' => 'json',
82+
'cast_to_collection' => 'collection',
83+
'cast_to_date' => 'date',
84+
'cast_to_datetime' => 'datetime',
85+
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
86+
'cast_to_immutable_date' => 'immutable_date',
87+
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
88+
'cast_to_immutable_datetime' => 'immutable_datetime',
89+
'cast_to_timestamp' => 'timestamp',
90+
'cast_to_encrypted' => 'encrypted',
91+
'cast_to_encrypted_array' => 'encrypted:array',
92+
'cast_to_encrypted_collection' => 'encrypted:collection',
93+
'cast_to_encrypted_json' => 'encrypted:json',
94+
'cast_to_encrypted_object' => 'encrypted:object',
95+
];
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
class SimpleCastsTable extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::create('simple_casts', static function (Blueprint $table) {
14+
$table->string('cast_to_int');
15+
$table->string('cast_to_integer');
16+
$table->string('cast_to_real');
17+
$table->string('cast_to_float');
18+
$table->string('cast_to_double');
19+
$table->string('cast_to_decimal');
20+
$table->string('cast_to_string');
21+
$table->string('cast_to_bool');
22+
$table->string('cast_to_boolean');
23+
$table->string('cast_to_object');
24+
$table->string('cast_to_array');
25+
$table->string('cast_to_json');
26+
$table->string('cast_to_collection');
27+
$table->string('cast_to_date');
28+
$table->string('cast_to_datetime');
29+
$table->string('cast_to_custom_datetime');
30+
$table->string('cast_to_immutable_date');
31+
$table->string('cast_to_immutable_custom_datetime');
32+
$table->string('cast_to_immutable_datetime');
33+
$table->string('cast_to_timestamp');
34+
$table->string('cast_to_encrypted');
35+
$table->string('cast_to_encrypted_array');
36+
$table->string('cast_to_encrypted_collection');
37+
$table->string('cast_to_encrypted_json');
38+
$table->string('cast_to_encrypted_object');
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)