Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
9.09% |
2 / 22 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BooleanComponentProvider | |
9.09% |
2 / 22 |
|
50.00% |
1 / 2 |
33.05 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createComponentFor | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\Factories\Concrete; |
| 3 | |
| 4 | use Apie\Core\Attributes\CmsSingleInput; |
| 5 | use Apie\Core\Dto\CmsInputOption; |
| 6 | use Apie\Core\Dto\ValueOption; |
| 7 | use Apie\Core\Enums\ScalarType; |
| 8 | use Apie\Core\Lists\ValueOptionList; |
| 9 | use Apie\Core\Metadata\MetadataFactory; |
| 10 | use Apie\Core\ValueObjects\Utils; |
| 11 | use Apie\HtmlBuilders\Components\Forms\SingleInput; |
| 12 | use Apie\HtmlBuilders\FormBuildContext; |
| 13 | use Apie\HtmlBuilders\Interfaces\ComponentInterface; |
| 14 | use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface; |
| 15 | use ReflectionType; |
| 16 | |
| 17 | /** |
| 18 | * Creates a form field for a boolean. |
| 19 | */ |
| 20 | class BooleanComponentProvider implements FormComponentProviderInterface |
| 21 | { |
| 22 | public function supports(ReflectionType $type, FormBuildContext $context): bool |
| 23 | { |
| 24 | $metadata = MetadataFactory::getCreationMetadata($type, $context->getApieContext()); |
| 25 | return $metadata->toScalarType() === ScalarType::BOOLEAN; |
| 26 | } |
| 27 | |
| 28 | public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface |
| 29 | { |
| 30 | $value = $context->getFilledInValue($type->allowsNull() ? null : false); |
| 31 | if ($value !== null) { |
| 32 | $value = Utils::toBoolean($value); |
| 33 | } |
| 34 | return new SingleInput( |
| 35 | $context->getFormName(), |
| 36 | $value, |
| 37 | $context->createTranslationLabel(), |
| 38 | $type->allowsNull(), |
| 39 | $type, |
| 40 | new CmsSingleInput( |
| 41 | $type->allowsNull() ? ['select'] : ['checkbox', 'select'], |
| 42 | new CmsInputOption( |
| 43 | options: new ValueOptionList([ |
| 44 | new ValueOption('On', true), |
| 45 | new ValueOption('Off', false), |
| 46 | ...($type->allowsNull() ? [new ValueOption('-', null)] : []) |
| 47 | ]) |
| 48 | ) |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | } |