Completed
Push — master ( d09163...346149 )
by Matt
06:42
created

bbcodes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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 bbcodes extends base
17
{
18
	/** @var config */
19
	protected $config;
20
21
	/** @var bbcodes_legacy */
22
	protected $trim_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               Config object
34
	 * @param bbcodes_legacy $trim_bbcodes_legacy  Legacy BBCodes trim tool
35
	 * @param utils|null     $text_formatter_utils Text Formatter Utils
36
	 */
37 25
	public function __construct(config $config, bbcodes_legacy $trim_bbcodes_legacy, utils $text_formatter_utils = null)
38
	{
39 25
		$this->config = $config;
40 25
		$this->trim_bbcodes_legacy = $trim_bbcodes_legacy;
41 25
		$this->text_formatter_utils = $text_formatter_utils;
42 25
	}
43
44
	/**
45
	 * @inheritdoc
46
	 */
47 25
	public function is_available()
48
	{
49 25
		return ($this->text_formatter_utils !== null);
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55
	public function run()
56
	{
57
		// If text is not formatted as expected, use legacy bbcode stripper
58
		if (!$this->s9e_format())
59
		{
60
			return $this->trim_bbcodes_legacy
61
				->set_text($this->text)
62
				->run();
63
		}
64
65
		return $this->set_data()->process();
66
	}
67
68
	/**
69
	 * @inheritdoc
70
	 */
71
	public function set_data()
72
	{
73
		if (!isset($this->data) || !is_array($this->data))
74
		{
75
			$this->data = (!empty($this->config['topic_preview_strip_bbcodes'])) ? explode('|', $this->config['topic_preview_strip_bbcodes']) : array();
76
			array_unshift($this->data, 'flash');
77
		}
78
79
		return $this;
80
	}
81
82
	/**
83
	 * Remove specified BBCodes and their contents
84
	 *
85
	 * @return string Stripped message text
86
	 */
87
	protected function process()
88
	{
89
		foreach ($this->data as $bbcode)
90
		{
91
			$this->text = $this->text_formatter_utils->remove_bbcode($this->text, $bbcode);
92
		}
93
94
		return $this->text_formatter_utils->unparse($this->text);
95
	}
96
97
	/**
98
	 * Is the message s9e formatted
99
	 *
100
	 * @return bool True if message is s9e formatted, false otherwise
101
	 */
102
	protected function s9e_format()
103
	{
104
		return (bool) preg_match('/^<[rt][ >]/s', $this->text);
105
	}
106
}
107