BBCodeParser::getParsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace PheRum\BBCode;
4
5
use PheRum\BBCode\Traits\ArrayTrait;
6
7
class BBCodeParser
8
{
9
    use ArrayTrait;
10
11
    /**
12
     * @var array
13
     */
14
    public $parsers = [
15
        'bold' => [
16
            'pattern' => '/\[b\](.*?)\[\/b\]/s',
17
            'replace' => '<strong>$1</strong>',
18
            'content' => '$1',
19
        ],
20
21
        'italic' => [
22
            'pattern' => '/\[i\](.*?)\[\/i\]/s',
23
            'replace' => '<em>$1</em>',
24
            'content' => '$1',
25
        ],
26
27
        'underline' => [
28
            'pattern' => '/\[u\](.*?)\[\/u\]/s',
29
            'replace' => '<u>$1</u>',
30
            'content' => '$1',
31
        ],
32
33
        'linethrough' => [
34
            'pattern' => '/\[s\](.*?)\[\/s\]/s',
35
            'replace' => '<strike>$1</strike>',
36
            'content' => '$1',
37
        ],
38
39
        'size' => [
40
            'pattern' => '/\[size\=([1-7])\](.*?)\[\/size\]/s',
41
            'replace' => '<font size="$1">$2</font>',
42
            'content' => '$2',
43
        ],
44
45
        'color' => [
46
            'pattern' => '/\[color\=(#[A-f0-9]{6}|#[A-f0-9]{3})\](.*?)\[\/color\]/s',
47
            'replace' => '<font color="$1">$2</font>',
48
            'content' => '$2',
49
        ],
50
51
        'center' => [
52
            'pattern' => '/\[center\](.*?)\[\/center\]/s',
53
            'replace' => '<div style="text-align:center;">$1</div>',
54
            'content' => '$1',
55
        ],
56
57
        'left' => [
58
            'pattern' => '/\[left\](.*?)\[\/left\]/s',
59
            'replace' => '<div style="text-align:left;">$1</div>',
60
            'content' => '$1',
61
        ],
62
63
        'right' => [
64
            'pattern' => '/\[right\](.*?)\[\/right\]/s',
65
            'replace' => '<div style="text-align:right;">$1</div>',
66
            'content' => '$1',
67
        ],
68
69
        'quote' => [
70
            'pattern' => '/\[quote\](.*?)\[\/quote\]/s',
71
            'replace' => '<blockquote>$1</blockquote>',
72
            'content' => '$1',
73
        ],
74
75
        'namedquote' => [
76
            'pattern' => '/\[quote\=(.*?)\](.*)\[\/quote\]/s',
77
            'replace' => '<blockquote><small>$1</small>$2</blockquote>',
78
            'content' => '$2',
79
        ],
80
81
        'link' => [
82
            'pattern' => '/\[url\](.*?)\[\/url\]/s',
83
            'replace' => '<a href="$1">$1</a>',
84
            'content' => '$1',
85
        ],
86
87
        'namedlink' => [
88
            'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s',
89
            'replace' => '<a href="$1">$2</a>',
90
            'content' => '$2',
91
        ],
92
93
        'image' => [
94
            'pattern' => '/\[img\](.*?)\[\/img\]/s',
95
            'replace' => '<img src="$1">',
96
            'content' => '$1',
97
        ],
98
99
        'orderedlistnumerical' => [
100
            'pattern' => '/\[list=1\](.*?)\[\/list\]/s',
101
            'replace' => '<ol>$1</ol>',
102
            'content' => '$1',
103
        ],
104
105
        'orderedlistalpha' => [
106
            'pattern' => '/\[list=a\](.*?)\[\/list\]/s',
107
            'replace' => '<ol type="a">$1</ol>',
108
            'content' => '$1',
109
        ],
110
111
        'unorderedlist' => [
112
            'pattern' => '/\[list\](.*?)\[\/list\]/s',
113
            'replace' => '<ul>$1</ul>',
114
            'content' => '$1',
115
        ],
116
117
        'listitem' => [
118
            'pattern' => '/\[\*\](.*)/',
119
            'replace' => '<li>$1</li>',
120
            'content' => '$1',
121
        ],
122
123
        'code' => [
124
            'pattern' => '/\[code\](.*?)\[\/code\]/s',
125
            'replace' => '<code>$1</code>',
126
            'content' => '$1',
127
        ],
128
129
        'youtube' => [
130
            'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s',
131
            'replace' => '<iframe width="560" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>',
132
            'content' => '$1',
133
        ],
134
135
        'linebreak' => [
136
            'pattern' => '/\r\n/',
137
            'replace' => '<br />',
138
            'content' => '',
139
        ],
140
    ];
141
142
    /**
143
     * @var array
144
     */
145
    protected $enabledParsers;
146
147
    public function __construct()
148
    {
149
        $this->enabledParsers = $this->parsers;
150
    }
151
152
    /**
153
     * Parses the BBCode string
154
     *
155
     * @param      $source
156
     * @param bool $caseInsensitive
157
     * @return string
158
     */
159
    public function parse($source, $caseInsensitive = false)
160
    {
161
        foreach ($this->enabledParsers as $name => $parser) {
162
            $pattern = ($caseInsensitive) ? $parser['pattern'] . 'i' : $parser['pattern'];
163
164
            $source = $this->searchAndReplace($pattern, $parser['replace'], $source);
165
        }
166
167
        return $source;
168
    }
169
170
    /**
171
     * Remove all BBCode
172
     *
173
     * @param string $source
174
     * @return string Parsed text
175
     */
176
    public function stripBBCodeTags($source)
177
    {
178
        foreach ($this->parsers as $name => $parser) {
179
            $source = $this->searchAndReplace($parser['pattern'] . 'i', $parser['content'], $source);
180
        }
181
182
        return $source;
183
    }
184
185
    /**
186
     * Searches after a specified pattern and replaces it with provided structure
187
     *
188
     * @param string $pattern Search pattern
189
     * @param string $replace Replacement structure
190
     * @param string $source  Text to search in
191
     * @return string Parsed text
192
     */
193
    protected function searchAndReplace($pattern, $replace, $source)
194
    {
195
        while (preg_match($pattern, $source)) {
196
            $source = preg_replace($pattern, $replace, $source);
197
        }
198
199
        return $source;
200
    }
201
202
    /**
203
     * Helper function to parse case sensitive
204
     *
205
     * @param string $source String containing the BBCode
206
     * @return string Parsed text
207
     */
208
    public function parseCaseSensitive($source)
209
    {
210
        return $this->parse($source, false);
211
    }
212
213
    /**
214
     * Helper function to parse case insensitive
215
     *
216
     * @param string $source String containing the BBCode
217
     * @return string Parsed text
218
     */
219
    public function parseCaseInsensitive($source)
220
    {
221
        return $this->parse($source, true);
222
    }
223
224
    /**
225
     * Limits the parsers to only those you specify
226
     *
227
     * @param mixed $only parsers
228
     * @return object BBCodeParser object
229
     */
230
    public function only($only = null)
231
    {
232
        $only = (is_array($only)) ? $only : func_get_args();
233
        $this->enabledParsers = $this->arrayOnly($this->parsers, $only);
234
235
        return $this;
236
    }
237
238
    /**
239
     * Removes the parsers you want to exclude
240
     *
241
     * @param mixed $except parsers
242
     * @return object BBCodeParser object
243
     */
244
    public function except($except = null)
245
    {
246
        $except = (is_array($except)) ? $except : func_get_args();
247
        $this->enabledParsers = $this->arrayExcept($this->parsers, $except);
248
249
        return $this;
250
    }
251
252
    /**
253
     * List of chosen parsers
254
     *
255
     * @return array array of parsers
256
     */
257
    public function getParsers()
258
    {
259
        return $this->enabledParsers;
260
    }
261
262
    /**
263
     * Sets the parser pattern and replace.
264
     * This can be used for new parsers or overwriting existing ones.
265
     *
266
     * @param string $name    Parser name
267
     * @param string $pattern Pattern
268
     * @param string $replace Replace pattern
269
     * @param string $content Parsed text pattern
270
     * @return void
271
     */
272
    public function setParser($name, $pattern, $replace, $content)
273
    {
274
        $this->parsers[$name] = [
275
            'pattern' => $pattern,
276
            'replace' => $replace,
277
            'content' => $content,
278
        ];
279
280
        $this->enabledParsers[$name] = [
281
            'pattern' => $pattern,
282
            'replace' => $replace,
283
            'content' => $content,
284
        ];
285
    }
286
}
287