Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.56% |
20 / 36 |
|
40.00% |
4 / 10 |
CRAP | |
0.00% |
0 / 1 |
SetterMethod | |
55.56% |
20 / 36 |
|
40.00% |
4 / 10 |
55.12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMethod | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
allowsNull | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isRequired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
appliesToContext | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
9.16 | |||
markValueAsMissing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setValue | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
5.02 | |||
getFieldPriority | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
getTypehint | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Core\Metadata\Fields; |
3 | |
4 | use Apie\Core\Attributes\ColumnPriority; |
5 | use Apie\Core\Context\ApieContext; |
6 | use Apie\Core\Metadata\Concerns\UseContextKey; |
7 | use Apie\Core\Metadata\SetterInterface; |
8 | use ReflectionMethod; |
9 | use ReflectionType; |
10 | |
11 | final class SetterMethod implements FieldInterface, SetterInterface |
12 | { |
13 | use UseContextKey; |
14 | |
15 | public function __construct(private readonly ReflectionMethod $method) |
16 | { |
17 | } |
18 | |
19 | public function getMethod(): ReflectionMethod |
20 | { |
21 | return $this->method; |
22 | } |
23 | |
24 | public function allowsNull(): bool |
25 | { |
26 | $parameters = $this->method->getParameters(); |
27 | $parameter = array_pop($parameters); |
28 | $type = $parameter->getType(); |
29 | return $type === null || $type->allowsNull(); |
30 | } |
31 | |
32 | public function isRequired(): bool |
33 | { |
34 | return false; |
35 | } |
36 | |
37 | public function isField(): bool |
38 | { |
39 | return true; |
40 | } |
41 | |
42 | public function appliesToContext(ApieContext $apieContext): bool |
43 | { |
44 | if (!$apieContext->appliesToContext($this->method)) { |
45 | return false; |
46 | } |
47 | $parameters = $this->method->getParameters(); |
48 | // last argument is value set, so we skip that one. |
49 | array_pop($parameters); |
50 | foreach ($parameters as $parameter) { |
51 | $contextKey = $this->getContextKey($apieContext, $parameter); |
52 | if ($contextKey === null || ($parameter->isDefaultValueAvailable() && !$apieContext->hasContext($contextKey))) { |
53 | return false; |
54 | } |
55 | } |
56 | return true; |
57 | } |
58 | |
59 | public function markValueAsMissing(): void |
60 | { |
61 | } |
62 | |
63 | public function setValue(object $object, mixed $value, ApieContext $apieContext): void |
64 | { |
65 | $parameters = $this->method->getParameters(); |
66 | // last argument is value set, so we skip that one. |
67 | array_pop($parameters); |
68 | $arguments = []; |
69 | foreach ($parameters as $parameter) { |
70 | $contextKey = $this->getContextKey($apieContext, $parameter); |
71 | if ($contextKey === null || !$apieContext->hasContext($contextKey)) { |
72 | $arguments[] = $parameter->getDefaultValue(); |
73 | } else { |
74 | $arguments[] = $apieContext->getContext($contextKey); |
75 | } |
76 | } |
77 | $arguments[] = $value; |
78 | |
79 | $this->method->invokeArgs($object, $arguments); |
80 | } |
81 | |
82 | public function getFieldPriority(): ?int |
83 | { |
84 | $attributes = $this->method->getAttributes(ColumnPriority::class); |
85 | if (empty($attributes)) { |
86 | return null; |
87 | } |
88 | |
89 | $attribute = reset($attributes); |
90 | return $attribute->newInstance()->priority; |
91 | } |
92 | |
93 | public function getTypehint(): ?ReflectionType |
94 | { |
95 | $parameters = $this->method->getParameters(); |
96 | // last argument is value set, so also the typehint |
97 | $parameter = array_pop($parameters); |
98 | return $parameter->getType(); |
99 | } |
100 | } |