Skip to content

Commit 09293cb

Browse files
committed
feat: rename Valuable to Describer
1 parent e006a33 commit 09293cb

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @template T as \Illuminate\Database\Eloquent\Model
1212
*/
13-
abstract class Valuable
13+
abstract class Describer
1414
{
1515
/**
1616
* @var array<Closure(Request, T, string): bool>
@@ -46,7 +46,7 @@ public function whenNotNull(): static
4646
Request $request,
4747
Model $model,
4848
string $attribute
49-
): bool => null !== $this->valueForModel($model, $attribute));
49+
): bool => null !== $this->retrieveValue($model, $attribute));
5050
}
5151

5252
/**
@@ -60,7 +60,23 @@ public function whenFilled(): static
6060
Request $request,
6161
Model $model,
6262
string $attribute
63-
): bool => filled($this->valueForModel($model, $attribute)));
63+
): bool => filled($this->retrieveValue($model, $attribute)));
64+
}
65+
66+
/**
67+
* @param \Illuminate\Http\Request $request
68+
* @param T $model
69+
* @param string $field
70+
*
71+
* @return mixed
72+
*/
73+
public function valueFor(Request $request, Model $model, string $field): mixed
74+
{
75+
if (!$this->check($request, $model, $field)) {
76+
return new MissingValue();
77+
}
78+
79+
return $this->resolveFor($request, $model, $field);
6480
}
6581

6682
/**
@@ -72,7 +88,7 @@ public function whenFilled(): static
7288
*
7389
* @return bool
7490
*/
75-
public function check(Request $request, Model $model, string $attribute): bool
91+
protected function check(Request $request, Model $model, string $attribute): bool
7692
{
7793
foreach ($this->rules as $rule) {
7894
if (!$rule($request, $model, $attribute)) {
@@ -83,23 +99,7 @@ public function check(Request $request, Model $model, string $attribute): bool
8399
return true;
84100
}
85101

86-
/**
87-
* @param \Illuminate\Http\Request $request
88-
* @param T $model
89-
* @param string $field
90-
*
91-
* @return mixed
92-
*/
93-
public function valueFor(Request $request, Model $model, string $field): mixed
94-
{
95-
if (!$this->check($request, $model, $field)) {
96-
return new MissingValue();
97-
}
98-
99-
return $this->resolveFor($request, $model, $field);
100-
}
101-
102-
private function valueForModel(Model $model, string $attribute): mixed
102+
private function retrieveValue(Model $model, string $attribute): mixed
103103
{
104104
$retriever = $this->retriever();
105105
if ($retriever === null) {

src/Descriptors/Relations/Relation.php

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

33
namespace Ark4ne\JsonApi\Descriptors\Relations;
44

5-
use Ark4ne\JsonApi\Descriptors\Valuable;
5+
use Ark4ne\JsonApi\Descriptors\Describer;
66
use Ark4ne\JsonApi\Resources\Relationship;
77
use Closure;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Http\Request;
10+
use Illuminate\Http\Resources\MissingValue;
1011

1112
/**
1213
* @template T as \Illuminate\Database\Eloquent\Model
13-
* @extends Valuable<T>
14+
* @extends Describer<T>
1415
*/
15-
abstract class Relation extends Valuable
16+
abstract class Relation extends Describer
1617
{
1718
protected ?Closure $links = null;
1819
protected ?Closure $meta = null;
@@ -87,13 +88,15 @@ public function whenPivotLoaded(string $table, string $accessor = null): static
8788

8889
public function resolveFor(Request $request, Model $model, string $field): Relationship
8990
{
90-
if ($this->relation instanceof Closure) {
91-
$value = fn() => ($this->relation)($model, $field);
91+
$retriever = $this->retriever();
92+
93+
if ($retriever instanceof Closure) {
94+
$value = static fn() => $retriever($model, $field);
9295
} else {
93-
$value = fn() => $model->getRelationValue($this->relation ?? $field);
96+
$value = static fn() => $model->getRelationValue($retriever ?? $field);
9497
}
9598

96-
$relation = $this->value($value);
99+
$relation = $this->value(fn() => $this->check($request, $model, $field) ? $value() : new MissingValue());
97100

98101
if ($this->whenIncluded) {
99102
$relation->whenIncluded();
@@ -102,5 +105,17 @@ public function resolveFor(Request $request, Model $model, string $field): Relat
102105
return $relation;
103106
}
104107

108+
/**
109+
* @param \Illuminate\Http\Request $request
110+
* @param T $model
111+
* @param string $field
112+
*
113+
* @return mixed
114+
*/
115+
public function valueFor(Request $request, Model $model, string $field): mixed
116+
{
117+
return $this->resolveFor($request, $model, $field);
118+
}
119+
105120
abstract protected function value(Closure $value): Relationship;
106121
}

src/Descriptors/Resolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ protected function resolveValues(Request $request, ?iterable $values): ?array
2121

2222
return (new Collection($values))
2323
->reduce(function (Collection $fields, $value, int|string $key) use ($request) {
24-
if (is_int($key) && ($value instanceof Valuable) && is_string($value->retriever())) {
24+
if (is_int($key) && ($value instanceof Describer) && is_string($value->retriever())) {
2525
$key = $value->retriever();
2626
}
2727

2828
$fields[$key] = value(
29-
$value instanceof Valuable
29+
$value instanceof Describer
3030
? $value->valueFor($request, $this->resource, $key)
3131
: $value
3232
);

src/Descriptors/Values/Value.php

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

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5-
use Ark4ne\JsonApi\Descriptors\Valuable;
5+
use Ark4ne\JsonApi\Descriptors\Describer;
66
use Ark4ne\JsonApi\Support\Fields;
77
use Closure;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Http\Request;
1010

1111
/**
1212
* @template T as \Illuminate\Database\Eloquent\Model
13-
* @extends Valuable<T>
13+
* @extends Describer<T>
1414
*/
15-
abstract class Value extends Valuable
15+
abstract class Value extends Describer
1616
{
1717
protected bool $nullable = true;
1818

0 commit comments

Comments
 (0)