Completed
Pull Request — master (#47)
by Maximilian
62:08 queued 57s
created

ToolbarManager::hasToolbar()   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 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\CKEditorBundle\Model;
13
14
use FOS\CKEditorBundle\Exception\ToolbarManagerException;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class ToolbarManager implements ToolbarManagerInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    private $items = [
25
        'basic.about' => ['About'],
26
        'basic.basic_styles' => ['Bold', 'Italic'],
27
        'basic.links' => ['Link', 'Unlink'],
28
        'basic.paragraph' => ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'],
29
        'standard.about' => ['Styles', 'Format', 'About'],
30
        'standard.basic_styles' => ['Bold', 'Italic', 'Strike', '-', 'RemoveFormat'],
31
        'standard.clipboard' => ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
32
        'standard.document' => ['Source'],
33
        'standard.editing' => ['Scayt'],
34
        'standard.links' => ['Link', 'Unlink', 'Anchor'],
35
        'standard.insert' => ['Image', 'Table', 'HorizontalRule', 'SpecialChar'],
36
        'standard.paragraph' => ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote'],
37
        'standard.tools' => ['Maximize'],
38
        'full.about' => ['About'],
39
        'full.basic_styles' => ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'],
40
        'full.clipboard' => ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
41
        'full.colors' => ['TextColor', 'BGColor'],
42
        'full.document' => ['Source', '-', 'NewPage', 'Preview', 'Print', '-', 'Templates'],
43
        'full.editing' => ['Find', 'Replace', '-', 'SelectAll', '-', 'Scayt'],
44
        'full.forms' => ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'SelectField', 'Button', 'ImageButton', 'HiddenField'],
45
        'full.insert' => ['Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar', 'Smiley', 'PageBreak', 'Iframe'],
46
        'full.links' => ['Link', 'Unlink', 'Anchor'],
47
        'full.paragraph' => ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl'],
48
        'full.styles' => ['Styles', 'Format', 'Font', 'FontSize', 'TextColor', 'BGColor'],
49
        'full.tools' => ['Maximize', 'ShowBlocks'],
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private $toolbars = [
56
        'basic' => [
57
            '@basic.basic_styles',
58
            '@basic.paragraph',
59
            '@basic.links',
60
            '@basic.about',
61
        ],
62
        'standard' => [
63
            '@standard.clipboard',
64
            '@standard.editing',
65
            '@standard.links',
66
            '@standard.insert',
67
            '@standard.tools',
68
            '@standard.document',
69
            '/',
70
            '@standard.basic_styles',
71
            '@standard.paragraph',
72
            '@standard.about',
73
        ],
74
        'full' => [
75
            '@full.document',
76
            '@full.clipboard',
77
            '@full.editing',
78
            '@full.forms',
79
            '/',
80
            '@full.basic_styles',
81
            '@full.paragraph',
82
            '@full.links',
83
            '@full.insert',
84
            '/',
85
            '@full.styles',
86
            '@full.colors',
87
            '@full.tools',
88
            '@full.about',
89
        ],
90
    ];
91
92
    /**
93
     * @param array $items
94
     * @param array $toolbars
95
     */
96
    public function __construct(array $items = [], array $toolbars = [])
97
    {
98
        $this->setItems($items);
99
        $this->setToolbars($toolbars);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function hasItems()
106
    {
107
        return !empty($this->items);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getItems()
114
    {
115
        return $this->items;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function setItems(array $items)
122
    {
123
        foreach ($items as $name => $item) {
124
            $this->setItem($name, $item);
125
        }
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function hasItem($name)
132
    {
133
        return isset($this->items[$name]);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getItem($name)
140
    {
141
        if (!$this->hasItem($name)) {
142
            throw ToolbarManagerException::itemDoesNotExist($name);
143
        }
144
145
        return $this->items[$name];
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function setItem($name, array $item)
152
    {
153
        $this->items[$name] = $item;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function hasToolbars()
160
    {
161
        return !empty($this->toolbars);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getToolbars()
168
    {
169
        return $this->toolbars;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function setToolbars(array $toolbars)
176
    {
177
        foreach ($toolbars as $name => $toolbar) {
178
            $this->setToolbar($name, $toolbar);
179
        }
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function hasToolbar($name)
186
    {
187
        return isset($this->toolbars[$name]);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function getToolbar($name)
194
    {
195
        if (!$this->hasToolbar($name)) {
196
            throw ToolbarManagerException::toolbarDoesNotExist($name);
197
        }
198
199
        return $this->toolbars[$name];
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function setToolbar($name, array $toolbar)
206
    {
207
        $this->toolbars[$name] = $toolbar;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function resolveToolbar($name)
214
    {
215
        $toolbars = [];
216
217
        foreach ($this->getToolbar($name) as $name => $item) {
218
            $toolbars[] = is_string($item) && substr($item, 0, 1) === '@'
219
                ? $this->getItem(substr($item, 1))
220
                : $item;
221
        }
222
223
        return $toolbars;
224
    }
225
}
226