Passed
Push — master ( 5c1865...50d94c )
by Christian
02:07
created

TimePickerType::finishView()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 8
nop 3
dl 0
loc 17
rs 9.9332
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
        $format    = 'HH';
28
29
        if ($options['with_minutes']) {
30
            $format .= ':mm';
31
            $options['dp_use_minutes'] = true;
32
        }
33
34
        if ($options['with_seconds']) {
35
            $format .= ':ss';
36
            $options['dp_use_seconds'] = true;
37
        }
38
39
        $view->vars['moment_format']         = $format;
40
        $view->vars['datepicker_use_button'] = empty($options['datepicker_use_button']) ? false : true;
41
        $view->vars['dp_options']            = self::createDpOptions($options);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function configureOptions(OptionsResolver $resolver): void
48
    {
49
        $resolver
50
            ->setDefaults([
51
                'input'                 => 'datetime',
52
                'widget'                => 'single_text',
53
                'datepicker_use_button' => true,
54
                'dp_pick_date'          => false,
55
                'dp_minute_stepping'    => 1,
56
            ])
57
            ->setAllowedTypes('input', 'string')
58
            ->setAllowedTypes('widget', 'string')
59
        ;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getParent()
66
    {
67
        return TimeType::class;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getBlockPrefix()
74
    {
75
        return 'core23_type_time_picker';
76
    }
77
78
    /**
79
     * @param array $options
80
     *
81
     * @return array
82
     */
83
    private static function createDpOptions(array $options): array
84
    {
85
        $dpOptions = [];
86
87
        foreach ($options as $key => $value) {
88
            if (false !== strpos($key, 'dp_')) {
89
                // We remove 'dp_' and camelize the options names
90
                $dpKey = substr($key, 3);
91
                $dpKey = preg_replace_callback(
92
                    '/_([a-z])/',
93
                    function ($c) {
94
                        return strtoupper($c[1]);
95
                    },
96
                    $dpKey
97
                );
98
99
                $dpOptions[$dpKey] = $value;
100
            }
101
        }
102
103
        return $dpOptions;
104
    }
105
}
106