Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.94% |
31 / 33 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DropdownOptionsComponentProvider | |
93.94% |
31 / 33 |
|
66.67% |
2 / 3 |
6.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supports | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| createComponentFor | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\Factories\Concrete; |
| 3 | |
| 4 | use Apie\CmsApiDropdownOption\DropdownOptionProvider\DropdownOptionProviderInterface; |
| 5 | use Apie\Core\Attributes\CmsSingleInput; |
| 6 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 7 | use Apie\Core\BoundedContext\BoundedContextId; |
| 8 | use Apie\Core\ContextConstants; |
| 9 | use Apie\Core\Dto\CmsInputOption; |
| 10 | use Apie\HtmlBuilders\Components\Forms\SingleInput; |
| 11 | use Apie\HtmlBuilders\Configuration\ApplicationConfiguration; |
| 12 | use Apie\HtmlBuilders\FormBuildContext; |
| 13 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 14 | use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface; |
| 15 | use ReflectionClass; |
| 16 | use ReflectionType; |
| 17 | |
| 18 | class DropdownOptionsComponentProvider implements FormComponentProviderInterface |
| 19 | { |
| 20 | public function __construct( |
| 21 | private readonly ApplicationConfiguration $applicationConfiguration |
| 22 | ) { |
| 23 | } |
| 24 | public function supports(ReflectionType $type, FormBuildContext $context): bool |
| 25 | { |
| 26 | $apieContext = $context->getApieContext(); |
| 27 | $formName = $context->getFormName(); |
| 28 | if (!$apieContext->hasContext(DropdownOptionProviderInterface::class) |
| 29 | || !$formName->hasChildFormFieldName()) { |
| 30 | return false; |
| 31 | } |
| 32 | $dropdownOptionProvider = $apieContext->getContext(DropdownOptionProviderInterface::class); |
| 33 | assert($dropdownOptionProvider instanceof DropdownOptionProviderInterface); |
| 34 | return $dropdownOptionProvider->supports( |
| 35 | $apieContext->withContext('property', $context->getFormName()->toValidationErrorKey()) |
| 36 | ); |
| 37 | } |
| 38 | public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface |
| 39 | { |
| 40 | $apieContext = $context->getApieContext(); |
| 41 | $configuration = $this->applicationConfiguration->createConfiguration( |
| 42 | $apieContext, |
| 43 | $apieContext->getContext(BoundedContextHashmap::class), |
| 44 | new BoundedContextId($apieContext->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 45 | ); |
| 46 | if ($apieContext->hasContext(ContextConstants::RESOURCE_NAME)) { |
| 47 | $resource = new ReflectionClass($apieContext->getContext(ContextConstants::RESOURCE_NAME)); |
| 48 | $autocompleteUrl = $configuration->getContextUrl('/' . $resource->getShortName() . '/dropdown-options/' . $context->getFormName()->toValidationErrorKey()); |
| 49 | } else { |
| 50 | $resource = new ReflectionClass($apieContext->getContext(ContextConstants::SERVICE_CLASS)); |
| 51 | $autocompleteUrl = $configuration->getContextUrl('/action/' . $resource->getShortName() . '/' . $apieContext->getContext(ContextConstants::METHOD_NAME) . '/dropdown-options/' . $context->getFormName()->toValidationErrorKey()); |
| 52 | } |
| 53 | return new SingleInput( |
| 54 | $context->getFormName(), |
| 55 | $context->getFilledInValue(), |
| 56 | $context->createTranslationLabel(), |
| 57 | $type->allowsNull(), |
| 58 | $type, |
| 59 | new CmsSingleInput( |
| 60 | ['combobox'], |
| 61 | new CmsInputOption(autocompleteUrl: $autocompleteUrl) |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | } |