Completed
Push — master ( 3a5159...5d8648 )
by Marko
14:57
created

CKEditorType::resolveConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
3
/*
4
 * This file is part of the FOSCKEditor Bundle.
5
 *
6
 * (c) 2018 - present  Friends of Symfony
7
 * (c) 2009 - 2017     Eric GELOEN <[email protected]>
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace FOS\CKEditorBundle\Form\Type;
14
15
use FOS\CKEditorBundle\Config\CKEditorConfiguration;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\Form\FormView;
21
use Symfony\Component\OptionsResolver\Options;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class CKEditorType extends AbstractType
28
{
29
    /**
30
     * @var CKEditorConfiguration
31
     */
32
    private $configuration;
33
34
    public function __construct(CKEditorConfiguration $configuration)
35
    {
36
        $this->configuration = $configuration;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function buildForm(FormBuilderInterface $builder, array $options)
43
    {
44
        $builder->setAttribute('enable', $options['enable']);
45
46
        if (!$options['enable']) {
47
            return;
48
        }
49
50
        $builder->setAttribute('async', $options['async']);
51
        $builder->setAttribute('autoload', $options['autoload']);
52
        $builder->setAttribute('auto_inline', $options['auto_inline']);
53
        $builder->setAttribute('inline', $options['inline']);
54
        $builder->setAttribute('jquery', $options['jquery']);
55
        $builder->setAttribute('require_js', $options['require_js']);
56
        $builder->setAttribute('input_sync', $options['input_sync']);
57
        $builder->setAttribute('filebrowsers', $options['filebrowsers']);
58
        $builder->setAttribute('base_path', $options['base_path']);
59
        $builder->setAttribute('js_path', $options['js_path']);
60
        $builder->setAttribute('jquery_path', $options['jquery_path']);
61
        $builder->setAttribute('config', $this->resolveConfig($options));
62
        $builder->setAttribute('config_name', $options['config_name']);
63
        $builder->setAttribute('plugins', array_merge($this->configuration->getPlugins(), $options['plugins']));
64
        $builder->setAttribute('styles', array_merge($this->configuration->getStyles(), $options['styles']));
65
        $builder->setAttribute('templates', array_merge($this->configuration->getTemplates(), $options['templates']));
66
    }
67
68
    private function resolveConfig(array $options): array
69
    {
70
        $config = $options['config'];
71
72
        if (null === $options['config_name']) {
73
            $options['config_name'] = uniqid('fos', true);
74
        } else {
75
            $config = array_merge($this->configuration->getConfig($options['config_name']), $config);
76
        }
77
78
        if (isset($config['toolbar']) && is_string($config['toolbar'])) {
79
            $config['toolbar'] = $this->configuration->getToolbar($config['toolbar']);
80
        }
81
82
        return $config;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function buildView(FormView $view, FormInterface $form, array $options)
89
    {
90
        $config = $form->getConfig();
91
        $view->vars['enable'] = $config->getAttribute('enable');
92
93
        if (!$view->vars['enable']) {
94
            return;
95
        }
96
97
        $view->vars['async'] = $config->getAttribute('async');
98
        $view->vars['autoload'] = $config->getAttribute('autoload');
99
        $view->vars['auto_inline'] = $config->getAttribute('auto_inline');
100
        $view->vars['inline'] = $config->getAttribute('inline');
101
        $view->vars['jquery'] = $config->getAttribute('jquery');
102
        $view->vars['require_js'] = $config->getAttribute('require_js');
103
        $view->vars['input_sync'] = $config->getAttribute('input_sync');
104
        $view->vars['filebrowsers'] = $config->getAttribute('filebrowsers');
105
        $view->vars['base_path'] = $config->getAttribute('base_path');
106
        $view->vars['js_path'] = $config->getAttribute('js_path');
107
        $view->vars['jquery_path'] = $config->getAttribute('jquery_path');
108
        $view->vars['config'] = $config->getAttribute('config');
109
        $view->vars['config_name'] = $config->getAttribute('config_name');
110
        $view->vars['plugins'] = $config->getAttribute('plugins');
111
        $view->vars['styles'] = $config->getAttribute('styles');
112
        $view->vars['templates'] = $config->getAttribute('templates');
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function configureOptions(OptionsResolver $resolver)
119
    {
120
        $resolver
121
            ->setDefaults([
122
                'enable' => $this->configuration->isEnable(),
123
                'async' => $this->configuration->isAsync(),
124
                'autoload' => $this->configuration->isAutoload(),
125
                'auto_inline' => $this->configuration->isAutoInline(),
126
                'inline' => $this->configuration->isInline(),
127
                'jquery' => $this->configuration->isJquery(),
128
                'require_js' => $this->configuration->isRequireJs(),
129
                'input_sync' => $this->configuration->isInputSync(),
130
                'filebrowsers' => $this->configuration->getFilebrowsers(),
131
                'base_path' => $this->configuration->getBasePath(),
132
                'js_path' => $this->configuration->getJsPath(),
133
                'jquery_path' => $this->configuration->getJqueryPath(),
134
                'config_name' => $this->configuration->getDefaultConfig(),
135
                'config' => [],
136
                'plugins' => [],
137
                'styles' => [],
138
                'templates' => [],
139
            ])
140
            ->addAllowedTypes('enable', 'bool')
141
            ->addAllowedTypes('async', 'bool')
142
            ->addAllowedTypes('autoload', 'bool')
143
            ->addAllowedTypes('auto_inline', 'bool')
144
            ->addAllowedTypes('inline', 'bool')
145
            ->addAllowedTypes('jquery', 'bool')
146
            ->addAllowedTypes('require_js', 'bool')
147
            ->addAllowedTypes('input_sync', 'bool')
148
            ->addAllowedTypes('filebrowsers', 'array')
149
            ->addAllowedTypes('config_name', ['string', 'null'])
150
            ->addAllowedTypes('base_path', 'string')
151
            ->addAllowedTypes('js_path', 'string')
152
            ->addAllowedTypes('jquery_path', 'string')
153
            ->addAllowedTypes('config', 'array')
154
            ->addAllowedTypes('plugins', 'array')
155
            ->addAllowedTypes('styles', 'array')
156
            ->addAllowedTypes('templates', 'array')
157
            ->setNormalizer('base_path', function (Options $options, $value) {
158
                if ('/' !== substr($value, -1)) {
159
                    $value .= '/';
160
                }
161
162
                return $value;
163
            });
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getParent()
170
    {
171
        return method_exists(AbstractType::class, 'getBlockPrefix') ? TextareaType::class : 'textarea';
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function getName()
178
    {
179
        return $this->getBlockPrefix();
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getBlockPrefix()
186
    {
187
        return 'ckeditor';
188
    }
189
}
190