Completed
Push — develop ( 20cffe...ad5ee4 )
by Florian
03:42
created

BootstrapCollectionType::getBlockPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of BraincraftedBootstrapBundle.
4
 * (c) 2012-2013 by Florian Eckerstorfer
5
 */
6
7
namespace Braincrafted\Bundle\BootstrapBundle\Form\Type;
8
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\FormView;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper;
16
17
/**
18
 * BootstrapCollectionType
19
 *
20
 * @package    BraincraftedBootstrapBundle
21
 * @subpackage Form
22
 * @author     Florian Eckerstorfer <[email protected]>
23
 * @copyright  2012-2013 Florian Eckerstorfer
24
 * @license    http://opensource.org/licenses/MIT The MIT License
25
 * @link       http://bootstrap.braincrafted.com Bootstrap for Symfony2
26
 */
27
class BootstrapCollectionType extends AbstractType
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function buildView(FormView $view, FormInterface $form, array $options)
33
    {
34
        $view->vars = array_replace(
35
            $view->vars,
36
            array(
37
                'allow_add'          => $options['allow_add'],
38
                'allow_delete'       => $options['allow_delete'],
39
                'add_button_text'    => $options['add_button_text'],
40
                'delete_button_text' => $options['delete_button_text'],
41
                'sub_widget_col'     => $options['sub_widget_col'],
42
                'button_col'         => $options['button_col'],
43
                'prototype_name'     => $options['prototype_name']
44
            )
45
        );
46
47
        if (false === $view->vars['allow_delete']) {
48
            $view->vars['sub_widget_col'] += $view->vars['button_col'];
49
        }
50
51
        if ($form->getConfig()->hasAttribute('prototype')) {
52
            $view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view);
53
        }
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function configureOptions(OptionsResolver $resolver)
60
    {
61
        $optionsNormalizer = function (Options $options, $value) {
62
            // @codeCoverageIgnoreStart
63
            $value['block_name'] = 'entry';
64
65
            return $value;
66
            // @codeCoverageIgnoreEnd
67
        };
68
69
        $defaults =  array(
70
            'allow_add'          => false,
71
            'allow_delete'       => false,
72
            'prototype'          => true,
73
            'prototype_name'     => '__name__',
74
            'add_button_text'    => 'Add',
75
            'delete_button_text' => 'Delete',
76
            'sub_widget_col'     => 10,
77
            'button_col'         => 2,
78
            'options'            => array(),
79
        );
80
81
82
        // map old class to new one using LegacyFormHelper
83
        $defaults['type'] = LegacyFormHelper::getType('text');
84
85
        $resolver->setDefaults($defaults);
86
87
        $resolver->setNormalizer('options', $optionsNormalizer);
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function getParent()
94
    {
95
        // map old class to new one using LegacyFormHelper
96
        return LegacyFormHelper::getType('collection');
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function getBlockPrefix()
103
    {
104
        return 'bootstrap_collection';
105
    }
106
107
    /**
108
     * Backward compatibility for SF < 3.0
109
     *
110
     * @return null|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
111
     */
112
    public function getName() {
113
        return $this->getBlockPrefix();
114
    }
115
}
116