Skip to content

Commit 736376d

Browse files
authored
feat: enhance ValueEnum and Values with generic type support (#35)
* feat: enhance ValueEnum and Values with generic type support * fix: update generic type annotations in Values class methods
1 parent 3f4fd31 commit 736376d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Descriptors/Values.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function mixed(null|string|Closure $attribute = null): ValueMixed
9292
/**
9393
* @param null|string|Closure(T):mixed $attribute
9494
*
95-
* @return ValueEnum<T>
95+
* @return ValueEnum<T, mixed>
9696
*/
9797
protected function enum(null|string|Closure $attribute = null): ValueEnum
9898
{
@@ -110,10 +110,9 @@ protected function struct(Closure $attribute): ValueStruct
110110
}
111111

112112
/**
113-
* @template U
114-
* @param Value<U> $type
113+
* @param Value<T> $type
115114
* @param null|string|Closure(T):(array<mixed>|null) $attribute
116-
* @return ValueArray<T, U>
115+
* @return ValueArray<T, mixed>
117116
*/
118117
protected function arrayOf(Value $type, null|string|Closure $attribute = null): ValueArray
119118
{

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)