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

remove_bbcodes::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
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
use phpbb\textformatter\s9e\utils;
15
16
class remove_bbcodes extends base
17
{
18
	/** @var config */
19
	protected $config;
20
21
	/** @var remove_bbcodes_legacy */
22
	protected $remove_bbcodes_legacy;
23
24
	/** @var utils|null */
25
	protected $text_formatter_utils;
26
27
	/** @var array Data array of BBCodes to remove */
28
	protected $data;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param config                $config
34
	 * @param remove_bbcodes_legacy $remove_bbcodes_legacy
35
	 * @param utils|null            $text_formatter_utils
36
	 * @access public
37
	 */
38
	public function __construct(config $config, remove_bbcodes_legacy $remove_bbcodes_legacy, utils $text_formatter_utils = null)
39
	{
40
		$this->config = $config;
41
		$this->remove_bbcodes_legacy = $remove_bbcodes_legacy;
42
		$this->text_formatter_utils = $text_formatter_utils;
43
	}
44
45
	/**
46
	 * @inheritdoc
47
	 */
48
	public function is_available()
49
	{
50
		return ($this->text_formatter_utils !== null);
51
	}
52
53
	/**
54
	 * @inheritdoc
55
	 */
56
	public function run()
57
	{
58
		// If text is not formatted as expected, use legacy bbcode stripper
59
		if (!$this->s9e_format())
60
		{
61
			return $this->remove_bbcodes_legacy
62
				->set_text($this->text)
63
				->run();
64
		}
65
66
		return $this->set_data()->process();
67
	}
68
69
	/**
70
	 * @inheritdoc
71
	 */
72
	public function set_data()
73
	{
74
		if (!isset($this->data) || !is_array($this->data))
75
		{
76
			$this->data = (!empty($this->config['topic_preview_strip_bbcodes'])) ? explode('|', $this->config['topic_preview_strip_bbcodes']) : array();
77
			array_unshift($this->data, 'flash');
78
		}
79
80
		return $this;
81
	}
82
83
	/**
84
	 * Remove specified BBCodes and their contents
85
	 *
86
	 * @return string Stripped message text
87
	 * @access protected
88
	 */
89
	protected function process()
90
	{
91
		foreach ($this->data as $bbcode)
92
		{
93
			$this->text = $this->text_formatter_utils->remove_bbcode($this->text, $bbcode);
94
		}
95
96
		return $this->text_formatter_utils->unparse($this->text);
97
	}
98
99
	/**
100
	 * Is the message s9e formatted
101
	 *
102
	 * @return bool True if message is s9e formatted, false otherwise
103
	 */
104
	protected function s9e_format()
105
	{
106
		return (bool) preg_match('/^<[rt][ >]/s', $this->text);
107
	}
108
}
109