Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

src/Form/Type/Filter/ChoiceType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Form\Type\Filter;
15
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as FormChoiceType;
18
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\Form\FormTypeInterface;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
/**
26
 * @author Thomas Rabaix <[email protected]>
27
 */
28
class ChoiceType extends AbstractType
29
{
30
    public const TYPE_CONTAINS = 1;
31
32
    public const TYPE_NOT_CONTAINS = 2;
33
34
    public const TYPE_EQUAL = 3;
35
36
    /**
37
     * NEXT_MAJOR: remove this property.
38
     *
39
     * @deprecated since 3.5, to be removed with 4.0
40
     *
41
     * @var TranslatorInterface
42
     */
43
    protected $translator;
44
45
    public function __construct(TranslatorInterface $translator)
46
    {
47
        $this->translator = $translator;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\AdminBundle\Form\...ChoiceType::$translator has been deprecated with message: since 3.5, to be removed with 4.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
48
    }
49
50
    /**
51
     * NEXT_MAJOR: Remove when dropping Symfony <2.8 support.
52
     *
53
     * {@inheritdoc}
54
     */
55
    public function getName()
56
    {
57
        return $this->getBlockPrefix();
58
    }
59
60
    public function getBlockPrefix()
61
    {
62
        return 'sonata_type_filter_choice';
63
    }
64
65
    public function buildForm(FormBuilderInterface $builder, array $options): void
66
    {
67
        $choices = [
68
            'label_type_contains' => self::TYPE_CONTAINS,
69
            'label_type_not_contains' => self::TYPE_NOT_CONTAINS,
70
            'label_type_equals' => self::TYPE_EQUAL,
71
        ];
72
        $operatorChoices = [];
73
74
        // NEXT_MAJOR: Remove first check (when requirement of Symfony is >= 2.8)
75
        if ('hidden' !== $options['operator_type'] && HiddenType::class !== $options['operator_type']) {
76
            $operatorChoices['choice_translation_domain'] = 'SonataAdminBundle';
77
78
            // NEXT_MAJOR: Remove (when requirement of Symfony is >= 3.0)
79
            if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
80
                $operatorChoices['choices_as_values'] = true;
81
            }
82
83
            $operatorChoices['choices'] = $choices;
84
        }
85
86
        $builder
87
            ->add('type', $options['operator_type'], array_merge(['required' => false], $options['operator_options'], $operatorChoices))
88
            ->add('value', $options['field_type'], array_merge(['required' => false], $options['field_options']))
89
        ;
90
    }
91
92
    /**
93
     * NEXT_MAJOR: Remove method, when bumping requirements to SF 2.7+.
94
     *
95
     * {@inheritdoc}
96
     */
97
    public function setDefaultOptions(OptionsResolverInterface $resolver): void
98
    {
99
        $this->configureOptions($resolver);
100
    }
101
102
    public function configureOptions(OptionsResolver $resolver): void
103
    {
104
        $resolver->setDefaults([
105
            'field_type' => FormChoiceType::class,
106
            'field_options' => [],
107
            'operator_type' => FormChoiceType::class,
108
            'operator_options' => [],
109
        ]);
110
    }
111
}
112