UrlType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Type;
7
8
use Symfony\Component\Form\AbstractType;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use Zicht\Bundle\UrlBundle\Aliasing\Aliasing;
14
use Zicht\Bundle\UrlBundle\Form\DataTransformer\TextTransformer;
15
16
/**
17
 * Type for choosing an URL
18
 */
19
class UrlType extends AbstractType
20
{
21
    /**
22
     * @var Aliasing
23
     */
24
    private $aliasing;
25
26
    /**
27
     * UrlType constructor.
28
     *
29
     * @param Aliasing $aliasing
30
     */
31 4
    public function __construct(Aliasing $aliasing)
32
    {
33 4
        $this->aliasing = $aliasing;
34 4
    }
35
36
    /**
37
     * @{inheritDoc}
38
     */
39 1
    public function getName()
40
    {
41 1
        return 'zicht_url';
42
    }
43
44
    /**
45
     * @{inheritDoc}
46
     */
47 1
    public function configureOptions(OptionsResolver $resolver)
48
    {
49 1
        parent::configureOptions($resolver);
50
51
        $resolver
52 1
            ->setDefaults(
53
                array(
54 1
                    'with_edit_button'      => true,
55
                    'no_transform_public'   => false,
56
                    'no_transform_internal' => false,
57
                    'url_suggest'           => '/admin/url/suggest',
58
                )
59
            );
60 1
    }
61
62
    /**
63
     * @{inheritDoc}
64
     */
65 1
    public function finishView(FormView $view, FormInterface $form, array $options)
66
    {
67 1
        parent::finishView($view, $form, $options);
68 1
        $view->vars['url_suggest'] = $options['url_suggest'];
69 1
        $view->vars['with_edit_button'] = $options['with_edit_button'];
70 1
    }
71
72
    /**
73
     * @{inheritDoc}
74
     */
75 1
    public function getParent()
76
    {
77 1
        return 'text';
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function buildForm(FormBuilderInterface $builder, array $options)
84
    {
85
        $mode = TextTransformer::MODE_TO_INTERNAL|TextTransformer::MODE_TO_PUBLIC;
86
87
        if ($options['no_transform_public']) {
88
            $mode ^= TextTransformer::MODE_TO_PUBLIC;
89
        }
90
91
        if ($options['no_transform_internal']) {
92
            $mode ^= TextTransformer::MODE_TO_INTERNAL;
93
        }
94
95
        if ($mode > 0) {
96
            $builder->addModelTransformer(new TextTransformer($this->aliasing, $mode));
97
        }
98
    }
99
}
100