Completed
Push — master ( 5f8f5a...37b8bd )
by Josh
19:37
created

Configurator::getRegexp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Autolink;
9
10
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
11
use s9e\TextFormatter\Plugins\ConfiguratorBase;
12
13
class Configurator extends ConfiguratorBase
14
{
15
	/**
16
	* @var string Name of attribute that stores the link's URL
17
	*/
18
	protected $attrName = 'url';
19
20
	/**
21
	* @var bool Whether to match strings that start with "www."
22
	*/
23
	public $matchWww = false;
24
25
	/**
26
	* @var string Name of the tag used to represent links
27
	*/
28
	protected $tagName = 'URL';
29
30
	/**
31
	* Creates the tag used by this plugin
32
	*
33
	* @return void
34
	*/
35 17
	protected function setUp()
36
	{
37 17
		if (isset($this->configurator->tags[$this->tagName]))
38 17
		{
39 1
			return;
40
		}
41
42
		// Create a tag
43 16
		$tag = $this->configurator->tags->add($this->tagName);
44
45
		// Add an attribute using the default url filter
46 16
		$filter = $this->configurator->attributeFilters->get('#url');
47 16
		$tag->attributes->add($this->attrName)->filterChain->append($filter);
48
49
		// Set the default template
50 16
		$tag->template = '<a href="{@' . $this->attrName . '}"><xsl:apply-templates/></a>';
51 16
	}
52
53
	/**
54
	* {@inheritdoc}
55
	*/
56 10
	public function asConfig()
57
	{
58
		$config = [
59 10
			'attrName'   => $this->attrName,
60 10
			'regexp'     => $this->getRegexp(),
61 10
			'tagName'    => $this->tagName
62 10
		];
63 10
		if (!$this->matchWww)
64 10
		{
65 9
			$config['quickMatch'] = '://';
66 9
		}
67
68 10
		return $config;
69
	}
70
71
	/**
72
	* Return the regexp used to match URLs
73
	*
74
	* @return strings
75
	*/
76 10
	protected function getRegexp()
77
	{
78 10
		$anchor = RegexpBuilder::fromList($this->configurator->urlConfig->getAllowedSchemes()) . '://';
79 10
		if ($this->matchWww)
80 10
		{
81 1
			$anchor = '(?:' . $anchor . '|www\\.)';
82 1
		}
83
84 10
		$regexp = '#\\b' . $anchor . '\\S(?>[^\\s\\[\\]'
85 10
		        . '\\x{FF01}-\\x{FF0F}\\x{FF1A}-\\x{FF20}\\x{FF3B}-\\x{FF40}\\x{FF5B}-\\x{FF65}'
86 10
		        . ']|\\[\\w*\\])++#Siu';
87
88 10
		return $regexp;
89
	}
90
}