Add Data Eloquent casting

This commit is contained in:
philipp lang 2025-03-31 01:49:15 +02:00
parent eb00d928fc
commit 9d5c71adcd
7 changed files with 48 additions and 7 deletions

View File

@ -3,11 +3,11 @@
namespace App\Form\Data;
use App\Fileshare\Data\FileshareResourceData;
use App\Form\Fields\Field;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
#[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)]
@ -16,4 +16,10 @@ class ExportData extends Data
public function __construct(public ?FileshareResourceData $root = null, public ?string $groupBy = null, public ?string $toGroupField = null)
{
}
/** @return DataEloquentCast<self> */
public static function castUsing(array $arguments): DataEloquentCast
{
return new DataEloquentCast(static::class, $arguments);
}
}

View File

@ -8,6 +8,7 @@ use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
class FormConfigData extends Data
{
@ -29,4 +30,10 @@ class FormConfigData extends Data
new FieldCollection([])
);
}
/** @return DataEloquentCast<self> */
public static function castUsing(array $arguments): DataEloquentCast
{
return new DataEloquentCast(static::class, $arguments);
}
}

View File

@ -6,6 +6,7 @@ use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
class Condition extends Data
{
@ -42,4 +43,10 @@ class Condition extends Data
{
return $this->mode === ConditionMode::ALL;
}
/** @return DataEloquentCast<self> */
public static function castUsing(array $arguments): DataEloquentCast
{
return new DataEloquentCast(static::class, $arguments);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Lib\Editor;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;
/** @todo replace blocks with actual block data classes */
class EditorData extends Data implements Editorable
@ -83,4 +84,10 @@ class EditorData extends Data implements Editorable
{
return $this;
}
/** @return DataEloquentCast<self> */
public static function castUsing(array $arguments): DataEloquentCast
{
return new DataEloquentCast(static::class, $arguments);
}
}

12
composer.lock generated
View File

@ -6011,16 +6011,16 @@
},
{
"name": "nette/utils",
"version": "v4.0.5",
"version": "v4.0.6",
"source": {
"type": "git",
"url": "https://github.com/nette/utils.git",
"reference": "736c567e257dbe0fcf6ce81b4d6dbe05c6899f96"
"reference": "ce708655043c7050eb050df361c5e313cf708309"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nette/utils/zipball/736c567e257dbe0fcf6ce81b4d6dbe05c6899f96",
"reference": "736c567e257dbe0fcf6ce81b4d6dbe05c6899f96",
"url": "https://api.github.com/repos/nette/utils/zipball/ce708655043c7050eb050df361c5e313cf708309",
"reference": "ce708655043c7050eb050df361c5e313cf708309",
"shasum": ""
},
"require": {
@ -6091,9 +6091,9 @@
],
"support": {
"issues": "https://github.com/nette/utils/issues",
"source": "https://github.com/nette/utils/tree/v4.0.5"
"source": "https://github.com/nette/utils/tree/v4.0.6"
},
"time": "2024-08-07T15:39:19+00:00"
"time": "2025-03-30T21:06:30+00:00"
},
{
"name": "nikic/php-parser",

View File

@ -7,6 +7,7 @@ parameters:
stubFiles:
- tests/stub/phpstan/TestResponse.stub
- tests/stub/phpstan/Settings.stub
- tests/stub/phpstan/DataEloquentCast.stub
paths:
- app

View File

@ -0,0 +1,13 @@
<?php
namespace Spatie\LaravelData\Support\EloquentCasts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
/**
* @template TDto
* @implements CastsAttributes<TDto, TDto>
*/
class DataEloquentCast implements CastsAttributes
{
}