Skip to content

Commit 6a459bb

Browse files
committed
feat: enhance ValueEnum and Values with generic type support
1 parent 3f4fd31 commit 6a459bb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Descriptors/Values.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ protected function date(null|string|Closure $attribute = null): ValueDate
7070
}
7171

7272
/**
73+
* @template U
7374
* @param null|string|Closure(T):(array<mixed>|null) $attribute
7475
*
75-
* @return ValueArray<T, mixed>
76+
* @return ValueArray<T, U>
7677
*/
7778
protected function array(null|string|Closure $attribute = null): ValueArray
7879
{
@@ -90,9 +91,10 @@ protected function mixed(null|string|Closure $attribute = null): ValueMixed
9091
}
9192

9293
/**
94+
* @template U
9395
* @param null|string|Closure(T):mixed $attribute
9496
*
95-
* @return ValueEnum<T>
97+
* @return ValueEnum<T, U>
9698
*/
9799
protected function enum(null|string|Closure $attribute = null): ValueEnum
98100
{

src/Descriptors/Values/ValueEnum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,28 @@
88

99
/**
1010
* @template T
11+
* @template U extends UnitEnum|BackedEnum
1112
* @extends Value<T>
1213
*/
1314
class ValueEnum extends Value
1415
{
16+
/**
17+
* @var class-string<U>|null
18+
*/
19+
protected null|string $type = null;
20+
21+
/**
22+
* Define the type of elements in the array
23+
*
24+
* @param class-string<U> $type
25+
* @return $this<T, U>
26+
*/
27+
public function of(string $type): static
28+
{
29+
$this->type = $type;
30+
return $this;
31+
}
32+
1533
protected function value(mixed $of, Request $request): mixed
1634
{
1735
if ($of instanceof BackedEnum) return $of->value;

0 commit comments

Comments
 (0)