Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
32 / 40 |
|
76.92% |
10 / 13 |
CRAP | |
0.00% |
0 / 1 |
| ConstructorParameter | |
80.00% |
32 / 40 |
|
76.92% |
10 / 13 |
40.19 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| markValueAsMissing | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| getMissingValue | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
7.73 | |||
| allowsNull | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasDefaultValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isRequired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| isField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| appliesToContext | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getTypehint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFieldPriority | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| getAttributes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Metadata\Fields; |
| 3 | |
| 4 | use Apie\Core\Attributes\ColumnPriority; |
| 5 | use Apie\Core\Attributes\Context; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\Exceptions\IndexNotFoundException; |
| 8 | use Apie\Core\Metadata\Concerns\UseContextKey; |
| 9 | use Apie\Core\Metadata\SetterInterface; |
| 10 | use Apie\Core\Utils\ConverterUtils; |
| 11 | use ReflectionParameter; |
| 12 | use ReflectionType; |
| 13 | |
| 14 | class ConstructorParameter implements FieldWithPossibleDefaultValue, SetterInterface, FallbackFieldInterface |
| 15 | { |
| 16 | use UseContextKey; |
| 17 | |
| 18 | public function __construct(private readonly ReflectionParameter $parameter) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | public function setValue(object $object, mixed $value, ApieContext $apieContext): void |
| 23 | { |
| 24 | // no-op |
| 25 | } |
| 26 | public function markValueAsMissing(): void |
| 27 | { |
| 28 | if (!$this->hasDefaultValue() && $this->isField()) { |
| 29 | throw new IndexNotFoundException($this->parameter->name); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function getMissingValue(ApieContext $apieContext): mixed |
| 34 | { |
| 35 | if ($this->isField()) { |
| 36 | if (!$this->hasDefaultValue()) { |
| 37 | throw new IndexNotFoundException($this->parameter->name); |
| 38 | } |
| 39 | return $this->getDefaultValue(); |
| 40 | } |
| 41 | $contextKey = $this->getContextKey($apieContext, $this->parameter); |
| 42 | if (!$apieContext->hasContext($contextKey) && $this->hasDefaultValue()) { |
| 43 | return $this->getDefaultValue(); |
| 44 | } |
| 45 | $parameterType = $this->parameter->getType(); |
| 46 | if ($parameterType) { |
| 47 | return ConverterUtils::dynamicCast($apieContext->getContext($contextKey), $parameterType); |
| 48 | } |
| 49 | return $apieContext->getContext($contextKey); |
| 50 | } |
| 51 | |
| 52 | public function allowsNull(): bool |
| 53 | { |
| 54 | $type = $this->parameter->getType(); |
| 55 | return (null === $type || $type->allowsNull()); |
| 56 | } |
| 57 | |
| 58 | public function hasDefaultValue(): bool |
| 59 | { |
| 60 | return $this->parameter->isDefaultValueAvailable(); |
| 61 | } |
| 62 | |
| 63 | public function getDefaultValue(): mixed |
| 64 | { |
| 65 | return $this->parameter->getDefaultValue(); |
| 66 | } |
| 67 | |
| 68 | public function isRequired(): bool |
| 69 | { |
| 70 | return !$this->parameter->isDefaultValueAvailable() && $this->isField(); |
| 71 | } |
| 72 | |
| 73 | public function isField(): bool |
| 74 | { |
| 75 | return !$this->parameter->getAttributes(Context::class); |
| 76 | } |
| 77 | |
| 78 | public function appliesToContext(ApieContext $apieContext): bool |
| 79 | { |
| 80 | if ($this->isField()) { |
| 81 | return true; |
| 82 | } |
| 83 | $contextKey = $this->getContextKey($apieContext, $this->parameter); |
| 84 | return $this->parameter->isDefaultValueAvailable() || $apieContext->hasContext($contextKey); |
| 85 | } |
| 86 | |
| 87 | public function getTypehint(): ?ReflectionType |
| 88 | { |
| 89 | return $this->parameter->getType(); |
| 90 | } |
| 91 | |
| 92 | public function getFieldPriority(): ?int |
| 93 | { |
| 94 | $attributes = $this->parameter->getAttributes(ColumnPriority::class); |
| 95 | if (empty($attributes)) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | $attribute = reset($attributes); |
| 100 | return $attribute->newInstance()->priority; |
| 101 | } |
| 102 | |
| 103 | public function getAttributes(string $attributeClass, bool $classDocBlock = true, bool $propertyDocblock = true, bool $argumentDocBlock = true): array |
| 104 | { |
| 105 | $list = []; |
| 106 | if ($argumentDocBlock || ($propertyDocblock && $this->parameter->isPromoted())) { |
| 107 | foreach ($this->parameter->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 108 | $list[] = $attribute->newInstance(); |
| 109 | } |
| 110 | } |
| 111 | $class = ConverterUtils::toReflectionClass($this->parameter->getType()); |
| 112 | if ($class && $classDocBlock) { |
| 113 | foreach ($class->getAttributes($attributeClass, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 114 | $list[] = $attribute->newInstance(); |
| 115 | } |
| 116 | } |
| 117 | return $list; |
| 118 | } |
| 119 | } |