Completed
Push — master ( 4e5e31...e8f514 )
by Matt
02:38
created

remove_bbcodes_legacy::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * Topic Preview
5
 *
6
 * @copyright (c) 2016 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\topicpreview\core\trim\tools;
12
13
use phpbb\config\config;
14
15
class remove_bbcodes_legacy extends base
16
{
17
	/** @var config */
18
	protected $config;
19
20
	/** @var string Regex data of BBCodes to remove */
21
	protected $data;
22
23
	/**
24
	 * Constructor
25
	 *
26
	 * @param config $config
27
	 * @access public
28
	 */
29
	public function __construct(config $config)
30
	{
31
		$this->config = $config;
32
	}
33
34
	/**
35
	 * @inheritdoc
36
	 */
37
	public function run()
38
	{
39
		return $this->set_data()->process();
40
	}
41
42
	/**
43
	 * @inheritdoc
44
	 */
45
	public function set_data()
46
	{
47
		if (!isset($this->data) || is_array($this->data))
48
		{
49
			$strip_bbcodes = (!empty($this->config['topic_preview_strip_bbcodes'])) ? 'flash|' . trim($this->config['topic_preview_strip_bbcodes']) : 'flash';
50
			$this->data = '#\[(' . $strip_bbcodes . ')[^\[\]]*\]((?:(?!\[\1[^\[\]]*\]).)*)\[\/\1[^\[\]]*\]#Usi';
51
		}
52
53
		return $this;
54
	}
55
56
	/**
57
	 * Remove specified BBCodes and their contents
58
	 * Uses recursion to handle nested BBCodes
59
	 *
60
	 * @return string Stripped message text
61
	 * @access protected
62
	 */
63
	protected function process()
64
	{
65
		if (preg_match($this->data, $this->text))
66
		{
67
			$this->text = preg_replace($this->data, '', $this->text);
68
			return $this->process();
69
		}
70
71
		return $this->text;
72
	}
73
}
74