Completed
Branch Scrutinizer (3da711)
by Josh
03:32
created

Parser::parse()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 2
crap 6
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Censor;
9
10
use s9e\TextFormatter\Plugins\ParserBase;
11
12
class Parser extends ParserBase
13
{
14
	/**
15
	* {@inheritdoc}
16
	*/
17 22
	public function parse($text, array $matches)
18
	{
19 22
		$tagName      = $this->config['tagName'];
20 22
		$attrName     = $this->config['attrName'];
21 22
		$replacements = (isset($this->config['replacements'])) ? $this->config['replacements'] : [];
22 22
		foreach ($matches as $m)
23
		{
24 22
			if ($this->isAllowed($m[0][0]))
25
			{
26 1
				continue;
27
			}
28
29 22
			$tag = $this->parser->addSelfClosingTag($tagName, $m[0][1], strlen($m[0][0]));
30 22
			foreach ($replacements as list($regexp, $replacement))
31
			{
32 5
				if (preg_match($regexp, $m[0][0]))
33
				{
34 5
					$tag->setAttribute($attrName, $replacement);
35 5
					break;
36
				}
37
			}
38
		}
39
	}
40
41
	/**
42
	* Test whether given word is allowed
43
	*
44
	* @param  string $word
45
	* @return bool
46
	*/
47 22
	protected function isAllowed($word)
48
	{
49 22
		return (isset($this->config['allowed']) && preg_match($this->config['allowed'], $word));
50
	}
51
}