-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractChoiceInput.php
More file actions
88 lines (72 loc) · 2.28 KB
/
AbstractChoiceInput.php
File metadata and controls
88 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\DesignSystemTwig\Twig\Components;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
use Symfony\UX\TwigComponent\Attribute\PreMount;
abstract class AbstractChoiceInput
{
public string $name;
public bool $checked = false;
public bool $disabled = false;
public bool $error = false;
#[ExposeInTemplate('class')]
public string $inputClass = '';
public bool $required = false;
public string $size = 'medium';
public ?string $value = null;
/**
* @param array<string, mixed> $props
*
* @return array<string, mixed>
*/
#[PreMount]
public function validate(array $props): array
{
$resolver = new OptionsResolver();
$resolver->setIgnoreUndefined();
$resolver
->define('name')
->required()
->allowedTypes('string')
->allowedValues(static function (string $value): bool {
return trim($value) !== '';
});
$resolver
->define('checked')
->allowedTypes('bool')
->default(false);
$resolver
->define('disabled')
->allowedTypes('bool')
->default(false);
$resolver
->define('error')
->allowedTypes('bool')
->default(false);
$resolver
->define('inputClass')
->allowedTypes('string')
->default('');
$resolver
->define('required')
->allowedTypes('bool')
->default(false);
$resolver
->define('size')
->allowedValues('small', 'medium')
->default('medium');
$resolver
->define('value')
->allowedTypes('null', 'string')
->default(null);
$this->configurePropsResolver($resolver);
return $resolver->resolve($props) + $props;
}
abstract protected function configurePropsResolver(OptionsResolver $resolver): void;
abstract public function getType(): string;
}