Completed
Push — master ( d0543d...aee1ad )
by Josh
19:39
created

Configurator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 273
ccs 88
cts 96
cp 0.9167
rs 10
c 1
b 0
f 0
wmc 23
lcom 1
cbo 7

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 2
A addAlias() 0 4 1
A removeAlias() 0 4 1
A omitImageSize() 0 5 1
A getAliases() 0 4 1
A useEmojiOne() 0 5 1
A usePNG() 0 5 1
A useSVG() 0 5 1
A useTwemoji() 0 5 1
B asConfig() 0 24 3
A getJSHints() 0 9 1
A getEmojiOneSrc() 0 9 1
A forceImageSize() 0 5 1
A setImageSize() 0 5 1
A getTemplate() 0 13 3
A getTwemojiSrc() 0 15 2
A resetTemplate() 0 4 1
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\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 bool Whether to force the image size in the img tag
29
	*/
30
	protected $forceImageSize = true;
31
32
	/**
33
	* @var string Emoji set to use
34
	*/
35
	protected $imageSet = 'twemoji';
36
37
	/**
38
	* @var integer Target size for the emoji images
39
	*/
40
	protected $imageSize = 16;
41
42
	/**
43
	* @var string Preferred image type
44
	*/
45
	protected $imageType = 'png';
46
47
	/**
48
	* @var string Name of the tag used by this plugin
49
	*/
50
	protected $tagName = 'EMOJI';
51
52
	/**
53
	* Plugin's setup
54
	*
55
	* Will create the tag used by this plugin
56
	*/
57 29
	protected function setUp()
58
	{
59 29
		if (isset($this->configurator->tags[$this->tagName]))
60 29
		{
61 1
			return;
62
		}
63
64 28
		$tag = $this->configurator->tags->add($this->tagName);
65 28
		$tag->attributes->add($this->attrName)->filterChain->append(
66 28
			$this->configurator->attributeFilters['#identifier']
67 28
		);
68 28
		$this->resetTemplate();
69 28
	}
70
71
	/**
72
	* Add an emoji alias
73
	*
74
	* @param  string $alias
75
	* @param  string $emoji
76
	* @return void
77
	*/
78 7
	public function addAlias($alias, $emoji)
79
	{
80 7
		$this->aliases[$alias] = $emoji;
81 7
	}
82
83
	/**
84
	* Force the size of the image to be set in the img element
85
	*
86
	* @return void
87
	*/
88
	public function forceImageSize()
89
	{
90
		$this->forceImageSize = true;
91
		$this->resetTemplate();
92
	}
93
94
	/**
95
	* Remove an emoji alias
96
	*
97
	* @param  string $alias
98
	* @return void
99
	*/
100 1
	public function removeAlias($alias)
101
	{
102 1
		unset($this->aliases[$alias]);
103 1
	}
104
105
	/**
106
	* Omit the size of the image in the img element
107
	*
108
	* @return void
109
	*/
110 2
	public function omitImageSize()
111
	{
112 2
		$this->forceImageSize = false;
113 2
		$this->resetTemplate();
114 2
	}
115
116
	/**
117
	* Get all emoji aliases
118
	*
119
	* @return array
120
	*/
121 1
	public function getAliases()
122
	{
123 1
		return $this->aliases;
124
	}
125
126
	/**
127
	* Set the size of the images used for emoji
128
	*
129
	* @param  integer $size Preferred size
130
	* @return void
131
	*/
132
	public function setImageSize($size)
133
	{
134
		$this->imageSize = (int) $size;
135
		$this->resetTemplate();
136
	}
137
138
	/**
139
	* Use the EmojiOne image set
140
	*
141
	* @return void
142
	*/
143 3
	public function useEmojiOne()
144
	{
145 3
		$this->imageSet = 'emojione';
146 3
		$this->resetTemplate();
147 3
	}
148
149
	/**
150
	* Use PNG images if available
151
	*
152
	* @return void
153
	*/
154 2
	public function usePNG()
155
	{
156 2
		$this->imageType = 'png';
157 2
		$this->resetTemplate();
158 2
	}
159
160
	/**
161
	* Use SVG images if available
162
	*
163
	* @return void
164
	*/
165 2
	public function useSVG()
166
	{
167 2
		$this->imageType = 'svg';
168 2
		$this->resetTemplate();
169 2
	}
170
171
	/**
172
	* Use the Twemoji image set
173
	*
174
	* @return void
175
	*/
176 3
	public function useTwemoji()
177
	{
178 3
		$this->imageSet = 'twemoji';
179 3
		$this->resetTemplate();
180 3
	}
181
182
	/**
183
	* {@inheritdoc}
184
	*/
185 7
	public function asConfig()
186
	{
187
		$config = [
188 7
			'attrName' => $this->attrName,
189 7
			'tagName'  => $this->tagName
190 7
		];
191
192 7
		if (!empty($this->aliases))
193 7
		{
194 4
			$aliases = array_keys($this->aliases);
195 4
			$regexp  = '/' . RegexpBuilder::fromList($aliases) . '/';
196
197 4
			$config['aliases']       = $this->aliases;
198 4
			$config['aliasesRegexp'] = new Regexp($regexp, true);
199
200 4
			$quickMatch = ConfigHelper::generateQuickMatchFromList($aliases);
201 4
			if ($quickMatch !== false)
202 4
			{
203 3
				$config['aliasesQuickMatch'] = $quickMatch;
204 3
			}
205 4
		}
206
207 7
		return $config;
208
	}
209
210
	/**
211
	* {@inheritdoc}
212
	*/
213 4
	public function getJSHints()
214
	{
215 4
		$quickMatch = ConfigHelper::generateQuickMatchFromList(array_keys($this->aliases));
216
217
		return [
218 4
			'EMOJI_HAS_ALIASES'          => !empty($this->aliases),
219 4
			'EMOJI_HAS_ALIAS_QUICKMATCH' => ($quickMatch !== false)
220 4
		];
221
	}
222
223
	/**
224
	* Get the content of the src attribute used to display EmojiOne's images
225
	*
226
	* @return string
227
	*/
228 3
	protected function getEmojiOneSrc()
229
	{
230 3
		$src  = '//cdn.jsdelivr.net/emojione/assets/' . $this->imageType . '/';
231 3
		$src .= "<xsl:if test=\"contains(@seq, '-20e3') or @seq = 'a9' or @seq = 'ae'\">00</xsl:if>";
232 3
		$src .= '<xsl:value-of select="@seq"/>';
233 3
		$src .= '.' . $this->imageType;
234
235 3
		return $src;
236
	}
237
238
	/**
239
	* Get this tag's template
240
	*
241
	* @return string
242
	*/
243 28
	protected function getTemplate()
244
	{
245 28
		$template = '<img alt="{.}" class="emoji" draggable="false"';
246 28
		if ($this->forceImageSize)
247 28
		{
248 28
			$template .= ' width="' . $this->imageSize . '" height="' . $this->imageSize . '"';
249 28
		}
250 28
		$template .= '><xsl:attribute name="src">';
251 28
		$template .= ($this->imageSet === 'emojione') ? $this->getEmojiOneSrc() :  $this->getTwemojiSrc();
252 28
		$template .= '</xsl:attribute></img>';
253
254 28
		return $template;
255
	}
256
257
	/**
258
	* Get the content of the src attribute used to display Twemoji's images
259
	*
260
	* @return string
261
	*/
262 28
	protected function getTwemojiSrc()
263
	{
264 28
		$src = '//twemoji.maxcdn.com/2/';
265 28
		if ($this->imageType === 'svg')
266 28
		{
267 2
			$src .= 'svg';
268 2
		}
269
		else
270
		{
271 28
			$src .= '72x72';
272
		}
273 28
		$src .= '/<xsl:value-of select="@seq"/>.' . $this->imageType;
274
275 28
		return $src;
276
	}
277
278
	/**
279
	* Reset the template used by this plugin's tag
280
	*
281
	* @return void
282
	*/
283 28
	protected function resetTemplate()
284
	{
285 28
		$this->getTag()->template = $this->getTemplate();
286
	}
287
}