Completed
Pull Request — 2.1 (#289)
by Piotr
02:46
created

GenericFormElement::initForm()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Admin\CRUD;
11
12
use FSi\Bundle\AdminBundle\Admin\AbstractElement;
13
use FSi\Bundle\AdminBundle\Exception\RuntimeException;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
abstract class GenericFormElement extends AbstractElement implements FormElement
19
{
20
    /**
21
     * @var \Symfony\Component\Form\FormFactoryInterface
22
     */
23
    protected $formFactory;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getRoute()
29
    {
30
        return 'fsi_admin_form';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function setDefaultOptions(OptionsResolver $resolver)
37
    {
38
        $resolver->setDefaults([
39
            'template_form' => null,
40
            'allow_add' => true
41
        ]);
42
43
        $resolver->setAllowedTypes('template_form', ['null', 'string']);
44
        $resolver->setAllowedTypes('allow_add', 'bool');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function configureOptions(OptionsResolver $resolver)
51
    {
52
        $this->setDefaultOptions($resolver);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setFormFactory(FormFactoryInterface $factory)
59
    {
60
        $this->formFactory = $factory;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function createForm($data = null)
67
    {
68
        $form = $this->initForm($this->formFactory, $data);
69
70
        if (!is_object($form) || !$form instanceof FormInterface) {
71
            throw new RuntimeException('initForm should return instanceof Symfony\\Component\\Form\\FormInterface');
72
        }
73
74
        return $form;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getSuccessRoute()
81
    {
82
        return $this->getRoute();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getSuccessRouteParameters()
89
    {
90
        return $this->getRouteParameters();
91
    }
92
93
    /**
94
     * Initialize create Form. This form will be used in createAction in FormController.
95
     *
96
     * @param \Symfony\Component\Form\FormFactoryInterface $factory
97
     * @param mixed $data
98
     * @return \Symfony\Component\Form\FormInterface
99
     */
100
    abstract protected function initForm(FormFactoryInterface $factory, $data = null);
101
}
102