Completed
Push — master ( 7377be...7ef1c9 )
by Eric
09:09
created

ToolbarManager::setToolbar()   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 2
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 Ivory\CKEditorBundle\Model;
13
14
use Ivory\CKEditorBundle\Exception\ToolbarManagerException;
15
16
/**
17
 * {@inheritdoc}
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21
class ToolbarManager implements ToolbarManagerInterface
22
{
23
    /** @var array */
24
    private $items = array(
25
        'basic.about'           => array('About'),
26
        'basic.basic_styles'    => array('Bold', 'Italic'),
27
        'basic.links'           => array('Link', 'Unlink'),
28
        'basic.paragraph'       => array('NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'),
29
        'standard.about'        => array('Styles', 'Format', 'About'),
30
        'standard.basic_styles' => array('Bold', 'Italic', 'Strike', '-', 'RemoveFormat'),
31
        'standard.clipboard'    => array('Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'),
32
        'standard.document'     => array('Source'),
33
        'standard.editing'      => array('Scayt'),
34
        'standard.links'        => array('Link', 'Unlink', 'Anchor'),
35
        'standard.insert'       => array('Image', 'Table', 'HorizontalRule', 'SpecialChar'),
36
        'standard.paragraph'    => array('NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote'),
37
        'standard.tools'        => array('Maximize'),
38
        'full.about'            => array('About'),
39
        'full.basic_styles'     => array('Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'),
40
        'full.clipboard'        => array('Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'),
41
        'full.colors'           => array('TextColor','BGColor'),
42
        'full.document'         => array('Source', '-', 'NewPage', 'Preview', 'Print', '-', 'Templates'),
43
        'full.editing'          => array('Find', 'Replace', '-', 'SelectAll', '-', 'Scayt'),
44
        'full.forms'            => array('Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'SelectField', 'Button', 'ImageButton', 'HiddenField'),
45
        'full.insert'           => array('Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar', 'Smiley', 'PageBreak', 'Iframe'),
46
        'full.links'            => array('Link', 'Unlink', 'Anchor'),
47
        'full.paragraph'        => array('NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl'),
48
        'full.styles'           => array('Styles', 'Format', 'Font', 'FontSize', 'TextColor', 'BGColor'),
49
        'full.tools'            => array('Maximize', 'ShowBlocks'),
50
    );
51
52
    /** @var array */
53
    private $toolbars = array(
54
        'basic' => array(
55
            '@basic.basic_styles',
56
            '@basic.paragraph',
57
            '@basic.links',
58
            '@basic.about',
59
        ),
60
        'standard' => array(
61
            '@standard.clipboard',
62
            '@standard.editing',
63
            '@standard.links',
64
            '@standard.insert',
65
            '@standard.tools',
66
            '@standard.document',
67
            '/',
68
            '@standard.basic_styles',
69
            '@standard.paragraph',
70
            '@standard.about',
71
        ),
72
        'full' => array(
73
            '@full.document',
74
            '@full.clipboard',
75
            '@full.editing',
76
            '@full.forms',
77
            '/',
78
            '@full.basic_styles',
79
            '@full.paragraph',
80
            '@full.links',
81
            '@full.insert',
82
            '/',
83
            '@full.styles',
84
            '@full.colors',
85
            '@full.tools',
86
            '@full.about',
87
        ),
88
    );
89
90
    /**
91
     * Creates a toolbar manager.
92
     *
93
     * @param array $items    The CKEditor items.
94
     * @param array $toolbars The CKEditor toolbars.
95
     */
96
    public function __construct(array $items = array(), array $toolbars = array())
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 = array();
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