Skip to content

Commit a2396ae

Browse files
committed
feat: struct value type
1 parent 7459b7d commit a2396ae

File tree

20 files changed

+206
-34
lines changed

20 files changed

+206
-34
lines changed

src/Descriptors/Describer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ abstract public function retriever(): null|string|Closure;
151151
*
152152
* @return int|string
153153
*/
154-
public static function retrieveName(mixed $value, int|string $key): int|string
154+
public static function retrieveName(mixed $value, int|string $key, null|string $prefix = null): int|string
155155
{
156156
if (is_int($key) && $value instanceof self && is_string($retriever = $value->retriever())) {
157-
return $retriever;
157+
return $prefix ? $prefix . '.' . $retriever : $retriever;
158158
}
159159

160-
return $key;
160+
return $prefix ? $prefix . '.' . $key : $key;
161161
}
162162
}

src/Descriptors/Values.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace Ark4ne\JsonApi\Descriptors;
44

5-
use Ark4ne\JsonApi\Descriptors\Relations\RelationMany;
6-
use Ark4ne\JsonApi\Descriptors\Relations\RelationOne;
7-
use Ark4ne\JsonApi\Descriptors\Values\{ValueArray,
5+
use Ark4ne\JsonApi\Descriptors\Values\{
6+
ValueArray,
87
ValueBool,
98
ValueDate,
109
ValueEnum,
1110
ValueFloat,
1211
ValueInteger,
1312
ValueMixed,
14-
ValueString};
13+
ValueString,
14+
ValueStruct
15+
};
1516
use Closure;
1617

1718
/**
@@ -98,4 +99,14 @@ protected function enum(null|string|Closure $attribute = null): ValueEnum
9899
{
99100
return new ValueEnum($attribute);
100101
}
102+
103+
/**
104+
* @param Closure(T):iterable<string, \Closure|mixed>|iterable<array-key, \Ark4ne\JsonApi\Descriptors\Values\Value> $attribute
105+
*
106+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueStruct<T>
107+
*/
108+
protected function struct(Closure $attribute = null): ValueStruct
109+
{
110+
return new ValueStruct($attribute);
111+
}
101112
}

src/Descriptors/Values/Value.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function resolveFor(Request $request, mixed $model, string $field): mixed
108108

109109
return $value === null && $this->nullable
110110
? null
111-
: $this->value($value);
111+
: $this->value($value, $request);
112112
}
113113

114114
protected function check(Request $request, mixed $model, string $attribute): bool
@@ -120,5 +120,5 @@ protected function check(Request $request, mixed $model, string $attribute): boo
120120
return parent::check($request, $model, $attribute);
121121
}
122122

123-
abstract protected function value(mixed $of): mixed;
123+
abstract protected function value(mixed $of, Request $request): mixed;
124124
}

src/Descriptors/Values/ValueArray.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Support\Collection;
67

78
/**
@@ -12,10 +13,10 @@ class ValueArray extends Value
1213
{
1314
/**
1415
* @param mixed $of
15-
*
16+
* @param Request $request
1617
* @return array<array-key, mixed>
1718
*/
18-
public function value(mixed $of): array
19+
public function value(mixed $of, Request $request): array
1920
{
2021
return (new Collection($of))->toArray();
2122
}

src/Descriptors/Values/ValueBool.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Illuminate\Http\Request;
6+
57
/**
68
* @template T
79
* @extends Value<T>
810
*/
911
class ValueBool extends Value
1012
{
11-
public function value(mixed $of): bool
13+
public function value(mixed $of, Request $request): bool
1214
{
1315
return (bool)$of;
1416
}

src/Descriptors/Values/ValueDate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Closure;
77
use DateTime;
88
use DateTimeInterface;
9+
use Illuminate\Http\Request;
910

1011
/**
1112
* @template T
@@ -31,7 +32,7 @@ public function format(string $format): static
3132
/**
3233
* @throws \Exception
3334
*/
34-
public function value(mixed $of): string
35+
public function value(mixed $of, Request $request): string
3536
{
3637
if ($of === null) {
3738
return (new DateTime("@0"))->format($this->format);

src/Descriptors/Values/ValueEnum.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

55
use BackedEnum;
6+
use Illuminate\Http\Request;
67
use UnitEnum;
78

89
/**
@@ -11,7 +12,7 @@
1112
*/
1213
class ValueEnum extends Value
1314
{
14-
protected function value(mixed $of): mixed
15+
protected function value(mixed $of, Request $request): mixed
1516
{
1617
if ($of instanceof BackedEnum) return $of->value;
1718
if ($of instanceof UnitEnum) return $of->name;

src/Descriptors/Values/ValueFloat.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ark4ne\JsonApi\Support\Config;
66
use Closure;
7+
use Illuminate\Http\Request;
78

89
/**
910
* @template T
@@ -26,7 +27,7 @@ public function precision(int $precision): static
2627
return $this;
2728
}
2829

29-
public function value(mixed $of): float
30+
public function value(mixed $of, Request $request): float
3031
{
3132
if (isset($this->precision)) {
3233
$precision = 10 ** $this->precision;

src/Descriptors/Values/ValueInteger.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Illuminate\Http\Request;
6+
57
/**
68
* @template T
79
* @extends Value<T>
810
*/
911
class ValueInteger extends Value
1012
{
11-
public function value(mixed $of): int
13+
public function value(mixed $of, Request $request): int
1214
{
1315
return (int)$of;
1416
}

src/Descriptors/Values/ValueMixed.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Illuminate\Http\Request;
6+
57
/**
68
* @template T
79
* @extends Value<T>
810
*/
911
class ValueMixed extends Value
1012
{
11-
public function value(mixed $of): mixed
13+
public function value(mixed $of, Request $request): mixed
1214
{
1315
return $of;
1416
}

0 commit comments

Comments
 (0)