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

HelpTypeExtension::configureOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 1
nop 1
dl 0
loc 19
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\Extension;
13
14
use Symfony\Component\Form\AbstractTypeExtension;
15
use Symfony\Component\Form\Extension\Core\Type\FormType;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\Form\FormView;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
final class HelpTypeExtension extends AbstractTypeExtension
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getExtendedType()
27
    {
28
        return FormType::class;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function buildView(FormView $view, FormInterface $form, array $options): void
35
    {
36
        $helpTranslationDomain = $options['help_translation_domain'];
37
        if ($view->parent && null === $helpTranslationDomain) {
38
            $helpTranslationDomain = $view->vars['translation_domain'];
39
        }
40
41
        $view->vars = array_replace($view->vars, [
42
            'help'                    => $options['help'],
43
            'help_translation_domain' => $helpTranslationDomain,
44
        ]);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function configureOptions(OptionsResolver $resolver): void
51
    {
52
        $helpTranslationDomainNormalizer = function (Options $options, $helpTranslationDomain) {
53
            if (true === $helpTranslationDomain) {
54
                return $options['translation_domain'];
55
            }
56
57
            return $helpTranslationDomain;
58
        };
59
60
        $resolver->setDefaults([
61
            'help'                    => null,
62
            'help_translation_domain' => true,
63
        ]);
64
65
        $resolver->setNormalizer('help_translation_domain', $helpTranslationDomainNormalizer);
66
67
        $resolver->setAllowedTypes('help', ['null', 'string']);
68
        $resolver->setAllowedTypes('help_translation_domain', ['null', 'bool', 'string']);
69
    }
70
}
71