Skip to content

Commit 06a5549

Browse files
Update the annotations on the eloquent casts
1 parent 1cc1ee0 commit 06a5549

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Support/EloquentCasts/DataCollectionEloquentCast.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212
use Spatie\LaravelData\Exceptions\CannotCastData;
1313
use Spatie\LaravelData\Support\DataConfig;
1414

15+
/**
16+
* @template TData of (BaseData&TransformableData)
17+
* @template TDataCollection of DataCollection
18+
*
19+
* @implements CastsAttributes<TDataCollection<TData>|null,TDataCollection<TData>|array|null>
20+
*/
1521
class DataCollectionEloquentCast implements CastsAttributes
1622
{
1723
protected DataConfig $dataConfig;
1824

25+
/**
26+
* @param class-string<TData> $dataClass
27+
* @param class-string<TDataCollection> $dataCollectionClass
28+
* @param array<string> $arguments
29+
*/
1930
public function __construct(
2031
protected string $dataClass,
2132
protected string $dataCollectionClass = DataCollection::class,
@@ -105,6 +116,10 @@ public function set($model, string $key, $value, array $attributes): ?string
105116
return $dataCollection;
106117
}
107118

119+
/**
120+
* @param TDataCollection<TData>|null $firstValue
121+
* @param TDataCollection<TData>|null $secondValue
122+
*/
108123
public function compare($model, string $key, $firstValue, $secondValue): bool
109124
{
110125
return $this->get($model, $key, $firstValue, [])?->toArray() === $this->get($model, $key, $secondValue, [])?->toArray();

src/Support/EloquentCasts/DataEloquentCast.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99
use Spatie\LaravelData\Exceptions\CannotCastData;
1010
use Spatie\LaravelData\Support\DataConfig;
1111

12+
/**
13+
* @template TData of (BaseData&TransformableData)
14+
*
15+
* @implements CastsAttributes<TData|null,TData|array|null>
16+
*/
1217
class DataEloquentCast implements CastsAttributes
1318
{
1419
protected DataConfig $dataConfig;
1520

21+
/**
22+
* @param class-string<TData> $dataClass $dataClass
23+
* @param string[] $arguments
24+
*/
1625
public function __construct(
17-
/** @var class-string<BaseData&TransformableData> $dataClass */
1826
protected string $dataClass,
19-
/** @var string[] $arguments */
2027
protected array $arguments = []
2128
) {
2229
$this->dataConfig = app(DataConfig::class);
@@ -39,7 +46,7 @@ public function get($model, string $key, $value, array $attributes): BaseData|Tr
3946
$payload = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
4047

4148
if ($this->isAbstractClassCast()) {
42-
/** @var class-string<BaseData&TransformableData> $dataClass */
49+
/** @var class-string<TData> $dataClass */
4350
$dataClass = $this->dataConfig->morphMap->getMorphedDataClass($payload['type']) ?? $payload['type'];
4451

4552
return $dataClass::from($payload['data']);
@@ -82,6 +89,10 @@ public function set($model, string $key, $value, array $attributes): ?string
8289
return $value;
8390
}
8491

92+
/**
93+
* @param TData|null $firstValue
94+
* @param TData|null $secondValue
95+
*/
8596
public function compare($model, string $key, $firstValue, $secondValue): bool
8697
{
8798
return $this->get($model, $key, $firstValue, [])?->toArray() === $this->get($model, $key, $secondValue, [])?->toArray();

0 commit comments

Comments
 (0)