Skip to content

Commit c9a43d7

Browse files
committed
fix: descriptor phpdoc
1 parent acb6d27 commit c9a43d7

File tree

15 files changed

+183
-141
lines changed

15 files changed

+183
-141
lines changed

src/Descriptors/Describer.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace Ark4ne\JsonApi\Descriptors;
44

55
use Closure;
6-
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Http\Request;
87
use Illuminate\Http\Resources\MissingValue;
98

109
/**
11-
* @template T as \Illuminate\Database\Eloquent\Model
10+
* @template T
1211
*/
1312
abstract class Describer
1413
{
@@ -28,8 +27,8 @@ public function when(bool|Closure $condition): static
2827
{
2928
$this->rules[] = static fn(
3029
Request $request,
31-
Model $model,
32-
string $attribute
30+
mixed $model,
31+
string $attribute
3332
): bool => value($condition, $request, $model, $attribute);
3433

3534
return $this;
@@ -44,8 +43,8 @@ public function whenNotNull(): static
4443
{
4544
return $this->when(fn(
4645
Request $request,
47-
Model $model,
48-
string $attribute
46+
mixed $model,
47+
string $attribute
4948
): bool => null !== $this->retrieveValue($model, $attribute));
5049
}
5150

@@ -58,19 +57,19 @@ public function whenFilled(): static
5857
{
5958
return $this->when(fn(
6059
Request $request,
61-
Model $model,
62-
string $attribute
60+
mixed $model,
61+
string $attribute
6362
): bool => filled($this->retrieveValue($model, $attribute)));
6463
}
6564

6665
/**
6766
* @param \Illuminate\Http\Request $request
68-
* @param T $model
69-
* @param string $field
67+
* @param T $model
68+
* @param string $field
7069
*
7170
* @return mixed
7271
*/
73-
public function valueFor(Request $request, Model $model, string $field): mixed
72+
public function valueFor(Request $request, mixed $model, string $field): mixed
7473
{
7574
if (!$this->check($request, $model, $field)) {
7675
return new MissingValue();
@@ -82,13 +81,13 @@ public function valueFor(Request $request, Model $model, string $field): mixed
8281
/**
8382
* Checks if the field should be displayed
8483
*
85-
* @param \Illuminate\Http\Request $request
86-
* @param \Illuminate\Database\Eloquent\Model $model
87-
* @param string $attribute
84+
* @param \Illuminate\Http\Request $request
85+
* @param T $model
86+
* @param string $attribute
8887
*
8988
* @return bool
9089
*/
91-
protected function check(Request $request, Model $model, string $attribute): bool
90+
protected function check(Request $request, mixed $model, string $attribute): bool
9291
{
9392
foreach ($this->rules as $rule) {
9493
if (!$rule($request, $model, $attribute)) {
@@ -99,7 +98,7 @@ protected function check(Request $request, Model $model, string $attribute): boo
9998
return true;
10099
}
101100

102-
private function retrieveValue(Model $model, string $attribute): mixed
101+
private function retrieveValue(mixed $model, string $attribute): mixed
103102
{
104103
$retriever = $this->retriever();
105104
if ($retriever === null) {
@@ -113,20 +112,20 @@ private function retrieveValue(Model $model, string $attribute): mixed
113112

114113
/**
115114
* @param \Illuminate\Http\Request $request
116-
* @param T $model
117-
* @param string $field
115+
* @param T $model
116+
* @param string $field
118117
*
119118
* @return mixed
120119
*/
121-
abstract protected function resolveFor(Request $request, Model $model, string $field): mixed;
120+
abstract protected function resolveFor(Request $request, mixed $model, string $field): mixed;
122121

123122
/**
124123
* @return string|Closure|null
125124
*/
126125
abstract public function retriever(): null|string|Closure;
127126

128127
/**
129-
* @param mixed $value
128+
* @param mixed $value
130129
* @param int|string $key
131130
*
132131
* @return int|string

src/Descriptors/Descriptors.php

Lines changed: 3 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,113 +2,11 @@
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\{
8-
ValueArray,
9-
ValueBool,
10-
ValueDate,
11-
ValueFloat,
12-
ValueInteger,
13-
ValueMixed,
14-
ValueString
15-
};
16-
use Closure;
17-
185
/**
19-
* @template T as \Illuminate\Database\Eloquent\Model
6+
* @deprecated
207
*/
218
trait Descriptors
229
{
23-
/**
24-
* @param null|string|Closure(T):mixed $attribute
25-
*
26-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueBool<T>
27-
*/
28-
protected function bool(null|string|Closure $attribute = null): ValueBool
29-
{
30-
return new ValueBool($attribute);
31-
}
32-
33-
/**
34-
* @param null|string|Closure(T):mixed $attribute
35-
*
36-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueInteger<T>
37-
*/
38-
protected function integer(null|string|Closure $attribute = null): ValueInteger
39-
{
40-
return new ValueInteger($attribute);
41-
}
42-
43-
/**
44-
* @param null|string|Closure(T):mixed $attribute
45-
*
46-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueFloat<T>
47-
*/
48-
public function float(null|string|Closure $attribute = null): ValueFloat
49-
{
50-
return new ValueFloat($attribute);
51-
}
52-
53-
/**
54-
* @param null|string|Closure(T):mixed $attribute
55-
*
56-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueString<T>
57-
*/
58-
protected function string(null|string|Closure $attribute = null): ValueString
59-
{
60-
return new ValueString($attribute);
61-
}
62-
63-
/**
64-
* @param null|string|Closure(T):(\DateTimeInterface|string|int|null) $attribute
65-
*
66-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueDate<T>
67-
*/
68-
protected function date(null|string|Closure $attribute = null): ValueDate
69-
{
70-
return new ValueDate($attribute);
71-
}
72-
73-
/**
74-
* @param null|string|Closure(T):(array<mixed>|null) $attribute
75-
*
76-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueArray<T>
77-
*/
78-
protected function array(null|string|Closure $attribute = null): ValueArray
79-
{
80-
return new ValueArray($attribute);
81-
}
82-
83-
/**
84-
* @param null|string|Closure(T):(object|null) $attribute
85-
*
86-
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueMixed<T>
87-
*/
88-
protected function mixed(null|string|Closure $attribute = null): ValueMixed
89-
{
90-
return new ValueMixed($attribute);
91-
}
92-
93-
/**
94-
* @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $for
95-
* @param null|string|Closure(T):mixed $relation
96-
*
97-
* @return \Ark4ne\JsonApi\Descriptors\Relations\RelationOne<T>
98-
*/
99-
protected function one(string $for, null|string|Closure $relation = null): RelationOne
100-
{
101-
return new RelationOne($for, $relation);
102-
}
103-
104-
/**
105-
* @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $for
106-
* @param null|string|Closure(T):mixed $relation
107-
*
108-
* @return \Ark4ne\JsonApi\Descriptors\Relations\RelationMany<T>
109-
*/
110-
protected function many(string $for, null|string|Closure $relation = null): RelationMany
111-
{
112-
return new RelationMany($for, $relation);
113-
}
10+
use Relations;
11+
use Values;
11412
}

src/Descriptors/Relations.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Descriptors;
4+
5+
use Ark4ne\JsonApi\Descriptors\Relations\RelationMany;
6+
use Ark4ne\JsonApi\Descriptors\Relations\RelationOne;
7+
use Ark4ne\JsonApi\Descriptors\Values\{
8+
ValueArray,
9+
ValueBool,
10+
ValueDate,
11+
ValueFloat,
12+
ValueInteger,
13+
ValueMixed,
14+
ValueString
15+
};
16+
use Closure;
17+
18+
/**
19+
* @template T as \Illuminate\Database\Eloquent\Model
20+
*/
21+
trait Relations
22+
{
23+
/**
24+
* @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $for
25+
* @param null|string|Closure(T):mixed $relation
26+
*
27+
* @return \Ark4ne\JsonApi\Descriptors\Relations\RelationOne<T>
28+
*/
29+
protected function one(string $for, null|string|Closure $relation = null): RelationOne
30+
{
31+
return new RelationOne($for, $relation);
32+
}
33+
34+
/**
35+
* @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $for
36+
* @param null|string|Closure(T):mixed $relation
37+
*
38+
* @return \Ark4ne\JsonApi\Descriptors\Relations\RelationMany<T>
39+
*/
40+
protected function many(string $for, null|string|Closure $relation = null): RelationMany
41+
{
42+
return new RelationMany($for, $relation);
43+
}
44+
}

src/Descriptors/Relations/Relation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function whenPivotLoaded(string $table, string $accessor = null): self
106106
);
107107
}
108108

109-
public function resolveFor(Request $request, Model $model, string $field): Relationship
109+
public function resolveFor(Request $request, mixed $model, string $field): Relationship
110110
{
111111
$retriever = $this->retriever();
112112

@@ -132,7 +132,7 @@ public function resolveFor(Request $request, Model $model, string $field): Relat
132132
*
133133
* @return mixed
134134
*/
135-
public function valueFor(Request $request, Model $model, string $field): mixed
135+
public function valueFor(Request $request, mixed $model, string $field): mixed
136136
{
137137
return $this->resolveFor($request, $model, $field);
138138
}

src/Descriptors/Values.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Descriptors;
4+
5+
use Ark4ne\JsonApi\Descriptors\Relations\RelationMany;
6+
use Ark4ne\JsonApi\Descriptors\Relations\RelationOne;
7+
use Ark4ne\JsonApi\Descriptors\Values\{
8+
ValueArray,
9+
ValueBool,
10+
ValueDate,
11+
ValueFloat,
12+
ValueInteger,
13+
ValueMixed,
14+
ValueString
15+
};
16+
use Closure;
17+
18+
/**
19+
* @template T
20+
*/
21+
trait Values
22+
{
23+
/**
24+
* @param null|string|Closure(T):mixed $attribute
25+
*
26+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueBool<T>
27+
*/
28+
protected function bool(null|string|Closure $attribute = null): ValueBool
29+
{
30+
return new ValueBool($attribute);
31+
}
32+
33+
/**
34+
* @param null|string|Closure(T):mixed $attribute
35+
*
36+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueInteger<T>
37+
*/
38+
protected function integer(null|string|Closure $attribute = null): ValueInteger
39+
{
40+
return new ValueInteger($attribute);
41+
}
42+
43+
/**
44+
* @param null|string|Closure(T):mixed $attribute
45+
*
46+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueFloat<T>
47+
*/
48+
public function float(null|string|Closure $attribute = null): ValueFloat
49+
{
50+
return new ValueFloat($attribute);
51+
}
52+
53+
/**
54+
* @param null|string|Closure(T):mixed $attribute
55+
*
56+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueString<T>
57+
*/
58+
protected function string(null|string|Closure $attribute = null): ValueString
59+
{
60+
return new ValueString($attribute);
61+
}
62+
63+
/**
64+
* @param null|string|Closure(T):(\DateTimeInterface|string|int|null) $attribute
65+
*
66+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueDate<T>
67+
*/
68+
protected function date(null|string|Closure $attribute = null): ValueDate
69+
{
70+
return new ValueDate($attribute);
71+
}
72+
73+
/**
74+
* @param null|string|Closure(T):(array<mixed>|null) $attribute
75+
*
76+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueArray<T>
77+
*/
78+
protected function array(null|string|Closure $attribute = null): ValueArray
79+
{
80+
return new ValueArray($attribute);
81+
}
82+
83+
/**
84+
* @param null|string|Closure(T):mixed $attribute
85+
*
86+
* @return \Ark4ne\JsonApi\Descriptors\Values\ValueMixed<T>
87+
*/
88+
protected function mixed(null|string|Closure $attribute = null): ValueMixed
89+
{
90+
return new ValueMixed($attribute);
91+
}
92+
}

0 commit comments

Comments
 (0)