Passed
Push — master ( f5fadc...72d313 )
by Christian
02:28
created

TimePickerType::finishView()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 24
nop 3
dl 0
loc 35
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Form\Type;
13
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\Extension\Core\Type\TimeType;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\Form\FormView;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
final class TimePickerType extends AbstractType
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function finishView(FormView $view, FormInterface $form, array $options): void
26
    {
27
        $dpOptions = [];
28
        $format    = 'HH';
29
30
        if ($options['with_minutes']) {
31
            $format .= ':mm';
32
            $options['dp_use_minutes'] = true;
33
        }
34
35
        if ($options['with_seconds']) {
36
            $format .= ':ss';
37
            $options['dp_use_seconds'] = true;
38
        }
39
40
        $view->vars['moment_format'] = $format;
41
42
        foreach ($options as $key => $value) {
43
            if (false !== strpos($key, 'dp_')) {
44
                // We remove 'dp_' and camelize the options names
45
                $dpKey = substr($key, 3);
46
                $dpKey = preg_replace_callback(
47
                    '/_([a-z])/',
48
                    function ($c) {
49
                        return strtoupper($c[1]);
50
                    },
51
                    $dpKey
52
                );
53
54
                $dpOptions[$dpKey] = $value;
55
            }
56
        }
57
58
        $view->vars['datepicker_use_button'] = empty($options['datepicker_use_button']) ? false : true;
59
        $view->vars['dp_options']            = $dpOptions;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function configureOptions(OptionsResolver $resolver): void
66
    {
67
        $resolver
68
            ->setDefaults([
69
                'input'                 => 'datetime',
70
                'widget'                => 'single_text',
71
                'datepicker_use_button' => true,
72
                'dp_pick_date'          => false,
73
                'dp_minute_stepping'    => 1,
74
            ])
75
            ->setAllowedTypes('input', 'string')
76
            ->setAllowedTypes('widget', 'string')
77
        ;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getParent()
84
    {
85
        return TimeType::class;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getBlockPrefix()
92
    {
93
        return 'core23_type_time_picker';
94
    }
95
}
96