Passed
Push — master ( 0de292...c7f65a )
by Christian
02:22
created

HelpTypeExtension::getExtendedType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
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
/**
22
 * @deprecated Use the symfony help extension
23
 * @see https://symfony.com/doc/current/form/form_customization.html#form-help-form-view
24
 */
25
final class HelpTypeExtension extends AbstractTypeExtension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function buildView(FormView $view, FormInterface $form, array $options): void
31
    {
32
        $helpTranslationDomain = $options['help_translation_domain'];
33
        if ($view->parent && null === $helpTranslationDomain) {
34
            $helpTranslationDomain = $view->vars['translation_domain'];
35
        }
36
37
        $view->vars = array_replace($view->vars, [
38
            'help'                    => $options['help'],
39
            'help_translation_domain' => $helpTranslationDomain,
40
        ]);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function configureOptions(OptionsResolver $resolver): void
47
    {
48
        $helpTranslationDomainNormalizer = static function (Options $options, $helpTranslationDomain) {
49
            if (null === $helpTranslationDomain && $options->offsetExists('translation_domain')) {
50
                return $options->offsetGet('translation_domain');
51
            }
52
53
            return $helpTranslationDomain;
54
        };
55
56
        $resolver->setDefaults([
57
            'help'                    => null,
58
            'help_translation_domain' => null,
59
        ]);
60
61
        $resolver->setNormalizer('help_translation_domain', $helpTranslationDomainNormalizer);
62
63
        $resolver->setAllowedTypes('help', ['null', 'string']);
64
        $resolver->setAllowedTypes('help_translation_domain', ['null', 'bool', 'string']);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getExtendedType()
71
    {
72
        foreach (static::getExtendedTypes() as $extendedType) {
73
            return $extendedType;
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public static function getExtendedTypes(): iterable
81
    {
82
        return [
83
            FormType::class,
84
        ];
85
    }
86
}
87