Passed
Push — master ( 03db17...536cbd )
by Josh
02:43
created

AbstractConfigurator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 17
c 1
b 0
f 1
dl 0
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 2
A getJSParser() 0 3 1
A finalize() 0 3 1
A updateRegexp() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2023 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\AbstractStaticUrlReplacer;
9
10
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
11
use s9e\TextFormatter\Plugins\ConfiguratorBase;
12
13
abstract class AbstractConfigurator extends ConfiguratorBase
14
{
15
	/**
16
	* @var string[] File extensions allowed in URLs
17
	*/
18
	public array $fileExtensions = [];
19
20
	protected $attrName;
21
	protected $quickMatch = '://';
22
	protected $regexp;
23
	protected $tagName;
24
25
	public function finalize(): void
26
	{
27
		$this->updateRegexp();
28
	}
29
30
	public function getJSParser()
31
	{
32
		return parent::getJSParser() . "\n" . file_get_contents(__DIR__ . '/Parser.js');
33
	}
34
35
	abstract protected function getTemplate(): string;
36
37
	/**
38
	* Creates the tag used by this plugin
39
	*
40
	* @return void
41
	*/
42
	protected function setUp(): void
43
	{
44
		$this->updateRegexp();
45
46
		if (isset($this->configurator->tags[$this->tagName]))
47
		{
48
			return;
49
		}
50
51
		// Create a tag
52
		$tag = $this->configurator->tags->add($this->tagName);
53
54
		// Add an attribute using the default url filter
55
		$filter = $this->configurator->attributeFilters['#url'];
56
		$tag->attributes->add($this->attrName)->filterChain->append($filter);
57
58
		// Set the default template
59
		$tag->template = $this->getTemplate();
60
61
		// Allow URL tags to be used as fallback
62
		$tag->rules->allowChild('URL');
63
	}
64
65
	protected function updateRegexp(): void
66
	{
67
		$this->regexp = '#\\bhttps?://[-.\\w]+/(?:[-+.:/\\w]|%[0-9a-f]{2}|\\(\\w+\\))+\\.' . RegexpBuilder::fromList($this->fileExtensions, ['caseInsensitive' => true, 'delimiter' => '#', 'unicode' => false]) . '(?!\\S)#i';
68
	}
69
}