Completed
Push — master ( 8f4937...fedf49 )
by smiley
03:03
created

MyAwesomeParserExtension   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 49
rs 10
1
<?php
2
/**
3
 * Class MyAwesomeParserExtension
4
 *
5
 * @filesource   MyAwesomeParserExtension.php
6
 * @created      19.09.2015
7
 * @package      Example
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace Example;
14
15
use chillerlan\bbcode\ParserExtensionInterface;
16
17
/**
18
 * A tiny custom preparser as ground to start from
19
 */
20
class MyAwesomeParserExtension implements ParserExtensionInterface{
21
22
	/**
23
	 * Pre-parser
24
	 *
25
	 * The bbcode you receive is already sanitized, which means: any replacements you do here won't be sanitized any further. Take care!
26
	 * Do anything here to the unparsed bbcode, just don't touch newlines - these will be replaced with a placeholder after this step.
27
	 *
28
	 * @param string $bbcode bbcode
29
	 *
30
	 * @return string preparsed bbcode
31
	 */
32
	public function pre($bbcode){
33
34
		$search = [
35
			"\t", // lets convert all tabs into 4 spaces
36
		    '{__BASE_URL__}', // assume we use a special token for our base url
37
		];
38
39
		$replace = [
40
			'    ',
41
		    'https://your.base/url/'
42
		];
43
44
		return str_replace($search, $replace, $bbcode);
45
	}
46
47
	/**
48
	 * Post-parser
49
	 *
50
	 * Use this method in case you want to alter the parsed bbcode.
51
	 * The newline placeholders are still available and any remaining will be removed in the last step before output
52
	 *
53
	 * Example: you want the "img" bbcode to use database images instead of user URLs.
54
	 * You'd go and change the tag so that it only accepts digits like [img=123456]
55
	 * and replace any occurence with a unique placeholder like {__IMG#ID__}.
56
	 * Now the post-parser gets into play: you preg_match_all() out all your placeholders,
57
	 * grab the images in a single query from the database and replace them with their respective <img> tag
58
	 * or whatever replacement and any corrupt id with a placeholder image. Profit!
59
	 *
60
	 * @param string $bbcode bbcode
61
	 *
62
	 * @return string postparsed bbcode
63
	 */
64
	public function post($bbcode){
65
		return $bbcode;
66
	}
67
68
}
69