Completed
Push — master ( 1212b8...011c4b )
by Paweł
29:44 queued 19:42
created

SelectAttributeType::getBlockPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\AttributeBundle\Form\Type\AttributeType;
13
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\CallbackTransformer;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
/**
22
 * @author Laurent Paganin-Gioanni <[email protected]>
23
 */
24
final class SelectAttributeType extends AbstractType
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getParent()
30
    {
31
        return ChoiceType::class;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function buildForm(FormBuilderInterface $builder, array $options)
38
    {
39
        if (is_array($options['configuration'])
40
            && isset($options['configuration']['multiple'])
41
            && !$options['configuration']['multiple']) {
42
            $builder->addModelTransformer(new CallbackTransformer(
43
                function($array) {
44
                    if (count($array) > 0) {
45
                        return $array[0];
46
                    }
47
48
                    return null;
49
                },
50
                function($string) {
51
                    if (!is_null($string)) {
52
                        return [$string];
53
                    }
54
55
                    return [];
56
                }
57
            ));
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function configureOptions(OptionsResolver $resolver)
65
    {
66
        $resolver
67
            ->setRequired('configuration')
68
            ->setDefault('placeholder', 'sylius.form.attribute_type_configuration.select.choose')
69
            ->setNormalizer('choices', function(Options $options){
70
                if (is_array($options['configuration'])
71
                    && isset($options['configuration']['choices'])
72
                    && is_array($options['configuration']['choices'])) {
73
                    $choices = array_flip($options['configuration']['choices']);
74
                    ksort($choices);
75
76
                    return $choices;
77
                }
78
79
                return [];
80
            })
81
            ->setNormalizer('multiple', function(Options $options){
82
                if (is_array($options['configuration']) && isset($options['configuration']['multiple'])) {
83
                    return $options['configuration']['multiple'];
84
                }
85
86
                return false;
87
            })
88
        ;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getBlockPrefix()
95
    {
96
        return 'sylius_attribute_type_select';
97
    }
98
}
99