-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Laravel 11 casts method
closes #1877
- Loading branch information
Showing
5 changed files
with
170 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace ModelPropertiesL11; | ||
|
||
use Illuminate\Database\Eloquent\Casts\AsStringable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Stringable; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function test(ModelWithCasts $modelWithCasts): void | ||
{ | ||
assertType('bool', $modelWithCasts->integer); | ||
assertType(Stringable::class, $modelWithCasts->string); | ||
} | ||
|
||
class ModelWithCasts extends Model | ||
{ | ||
protected $casts = [ | ||
'integer' => 'int', | ||
]; | ||
|
||
/** | ||
* @return array{integer: 'bool', string: 'Illuminate\\Database\\Eloquent\\Casts\\AsStringable:argument'} | ||
*/ | ||
public function casts(): array | ||
{ | ||
$argument = 'argument'; | ||
|
||
return [ | ||
'integer' => 'bool', // overrides the cast from the property | ||
'string' => AsStringable::class.':'.$argument, | ||
]; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...s/application/database/migrations/2020_01_30_000000_create_model_with_casts_table.php.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Migrations; | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
|
||
class CreateModelWithCastsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('model_with_casts', static function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
|
||
// Testing property casts | ||
$table->integer('int'); | ||
$table->integer('integer'); | ||
$table->float('real'); | ||
$table->float('float'); | ||
$table->double('double'); | ||
$table->decimal('decimal'); | ||
$table->string('string'); | ||
$table->boolean('bool'); | ||
$table->boolean('boolean'); | ||
$table->json('object'); | ||
$table->json('array'); | ||
$table->json('json'); | ||
$table->json('collection'); | ||
$table->json('nullable_collection')->nullable(); | ||
$table->date('date'); | ||
$table->dateTime('datetime'); | ||
$table->date('immutable_date'); | ||
$table->dateTime('immutable_datetime'); | ||
$table->timestamp('timestamp'); | ||
|
||
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
} |
66db15f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@canvural Could L11 cast support be improved not to need the array shape?
66db15f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szepeviktor No it cannot. Not easily at least.
66db15f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My implementation would not have needed the duplicate array shape---and it was simply/easy.
66db15f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@calebdw I explained in detail why we could not go with your implementation.