Completed
Push — master ( 79e013...193dec )
by Josh
14:03
created

Configurator::omitImageSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Emoji;
9
10
use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
11
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
12
use s9e\TextFormatter\Configurator\Items\Regexp;
13
use s9e\TextFormatter\Plugins\ConfiguratorBase;
14
15
class Configurator extends ConfiguratorBase
16
{
17
	/**
18
	* @var string Name of the attribute used by this plugin
19
	*/
20
	protected $attrName = 'seq';
21
22
	/**
23
	* @var array Associative array of alias => emoji
24
	*/
25
	protected $aliases = [];
26
27
	/**
28
	* @var string Name of the tag used by this plugin
29
	*/
30
	protected $tagName = 'EMOJI';
31
32
	/**
33
	* Plugin's setup
34
	*
35
	* Will create the tag used by this plugin
36
	*/
37 17
	protected function setUp()
38
	{
39 17
		if (isset($this->configurator->tags[$this->tagName]))
40
		{
41 1
			return;
42
		}
43
44 16
		$tag = $this->configurator->tags->add($this->tagName);
45 16
		$tag->attributes->add($this->attrName)->filterChain->append(
46 16
			$this->configurator->attributeFilters['#identifier']
47
		);
48 16
		$tag->template = '<img alt="{.}" class="emoji" draggable="false" src="//cdn.jsdelivr.net/emojione/assets/svg/{@seq}.svg"/>';
49 16
	}
50
51
	/**
52
	* Add an emoji alias
53
	*
54
	* @param  string $alias
55
	* @param  string $emoji
56
	* @return void
57
	*/
58 7
	public function addAlias($alias, $emoji)
59
	{
60 7
		$this->aliases[$alias] = $emoji;
61 7
	}
62
63
	/**
64
	* Remove an emoji alias
65
	*
66
	* @param  string $alias
67
	* @return void
68
	*/
69 1
	public function removeAlias($alias)
70
	{
71 1
		unset($this->aliases[$alias]);
72 1
	}
73
74
	/**
75
	* Get all emoji aliases
76
	*
77
	* @return array
78
	*/
79 1
	public function getAliases()
80
	{
81 1
		return $this->aliases;
82
	}
83
84
	/**
85
	* {@inheritdoc}
86
	*/
87 7
	public function asConfig()
88
	{
89
		$config = [
90 7
			'attrName' => $this->attrName,
91 7
			'tagName'  => $this->tagName
92
		];
93
94 7
		if (!empty($this->aliases))
95
		{
96 4
			$aliases = array_keys($this->aliases);
97 4
			$regexp  = '/' . RegexpBuilder::fromList($aliases) . '/';
98
99 4
			$config['aliases']       = $this->aliases;
100 4
			$config['aliasesRegexp'] = new Regexp($regexp, true);
101
102 4
			$quickMatch = ConfigHelper::generateQuickMatchFromList($aliases);
103 4
			if ($quickMatch !== false)
104
			{
105 3
				$config['aliasesQuickMatch'] = $quickMatch;
106
			}
107
		}
108
109 7
		return $config;
110
	}
111
112
	/**
113
	* {@inheritdoc}
114
	*/
115 4
	public function getJSHints()
116
	{
117 4
		$quickMatch = ConfigHelper::generateQuickMatchFromList(array_keys($this->aliases));
118
119
		return [
120 4
			'EMOJI_HAS_ALIASES'          => !empty($this->aliases),
121
			'EMOJI_HAS_ALIAS_QUICKMATCH' => ($quickMatch !== false)
122
		];
123
	}
124
}