DateTimePickerType::buildView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Web\Infrastructure\Form\Symfony\Type;
16
17
use Acme\PhpExtension\DateTime\MomentFormatConverter;
18
use Locale;
19
use Symfony\Component\Form\AbstractType;
20
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\Form\FormView;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
/**
26
 * Defines the custom form field type used to manipulate datetime values across
27
 * Bootstrap Date\Time Picker javascript plugin.
28
 *
29
 * See https://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
30
 *
31
 * @author Yonel Ceruto <[email protected]>
32
 * @author Herberto Graca <[email protected]>
33
 */
34
class DateTimePickerType extends AbstractType
35
{
36
    /**
37
     * @var MomentFormatConverter
38
     */
39
    private $formatConverter;
40
41
    public function __construct(MomentFormatConverter $converter)
42
    {
43
        $this->formatConverter = $converter;
44
    }
45
46
    public function buildView(FormView $view, FormInterface $form, array $options): void
47
    {
48
        $view->vars['attr']['data-date-format'] = $this->formatConverter->convert($options['format']);
49
        $view->vars['attr']['data-date-locale'] = mb_strtolower(str_replace('_', '-', Locale::getDefault()));
50
    }
51
52
    public function configureOptions(OptionsResolver $resolver): void
53
    {
54
        $resolver->setDefaults([
55
            'widget' => 'single_text',
56
        ]);
57
    }
58
59
    public function getParent(): string
60
    {
61
        return DateTimeType::class;
62
    }
63
}
64