Completed
Push — master ( 76e464...7cd1df )
by Josh
24:29
created

Configurator::getTwemojiSrc()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 3
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\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 = 'emojione';
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
	* @var string[] List of Twemoji sequences that do not match Emoji One's
54
	*/
55
	protected $twemojiAliases = [
56
		'1f3f3-fe0f-200d-1f308',
57
		'1f3f4-200d-2620-fe0f',
58
		'1f441-200d-1f5e8',
59
		'1f468-200d-1f468-200d-1f466-200d-1f466',
60
		'1f468-200d-1f468-200d-1f466',
61
		'1f468-200d-1f468-200d-1f467-200d-1f466',
62
		'1f468-200d-1f468-200d-1f467-200d-1f467',
63
		'1f468-200d-1f468-200d-1f467',
64
		'1f468-200d-1f469-200d-1f466-200d-1f466',
65
		'1f468-200d-1f469-200d-1f466',
66
		'1f468-200d-1f469-200d-1f467-200d-1f466',
67
		'1f468-200d-1f469-200d-1f467-200d-1f467',
68
		'1f468-200d-1f469-200d-1f467',
69
		'1f468-200d-2764-fe0f-200d-1f468',
70
		'1f468-200d-2764-fe0f-200d-1f48b-200d-1f468',
71
		'1f469-200d-1f469-200d-1f466-200d-1f466',
72
		'1f469-200d-1f469-200d-1f466',
73
		'1f469-200d-1f469-200d-1f467-200d-1f466',
74
		'1f469-200d-1f469-200d-1f467-200d-1f467',
75
		'1f469-200d-1f469-200d-1f467',
76
		'1f469-200d-2764-fe0f-200d-1f468',
77
		'1f469-200d-2764-fe0f-200d-1f469',
78
		'1f469-200d-2764-fe0f-200d-1f48b-200d-1f468',
79
		'1f469-200d-2764-fe0f-200d-1f48b-200d-1f469'
80
	];
81
82
	/**
83
	* Plugin's setup
84
	*
85
	* Will create the tag used by this plugin
86
	*/
87 29
	protected function setUp()
88
	{
89 29
		if (isset($this->configurator->tags[$this->tagName]))
90 29
		{
91 1
			return;
92
		}
93
94 28
		$tag = $this->configurator->tags->add($this->tagName);
95 28
		$tag->attributes->add($this->attrName)->filterChain->append(
96 28
			$this->configurator->attributeFilters['#identifier']
97 28
		);
98 28
		$this->resetTemplate();
99 28
	}
100
101
	/**
102
	* Add an emoji alias
103
	*
104
	* @param  string $alias
105
	* @param  string $emoji
106
	* @return void
107
	*/
108 7
	public function addAlias($alias, $emoji)
109
	{
110 7
		$this->aliases[$alias] = $emoji;
111 7
	}
112
113
	/**
114
	* Force the size of the image to be set in the img element
115
	*
116
	* @return void
117
	*/
118
	public function forceImageSize()
119
	{
120
		$this->forceImageSize = true;
121
		$this->resetTemplate();
122
	}
123
124
	/**
125
	* Remove an emoji alias
126
	*
127
	* @param  string $alias
128
	* @return void
129
	*/
130 1
	public function removeAlias($alias)
131
	{
132 1
		unset($this->aliases[$alias]);
133 1
	}
134
135
	/**
136
	* Omit the size of the image in the img element
137
	*
138
	* @return void
139
	*/
140 2
	public function omitImageSize()
141
	{
142 2
		$this->forceImageSize = false;
143 2
		$this->resetTemplate();
144 2
	}
145
146
	/**
147
	* Get all emoji aliases
148
	*
149
	* @return array
150
	*/
151 1
	public function getAliases()
152
	{
153 1
		return $this->aliases;
154
	}
155
156
	/**
157
	* Set the size of the images used for emoji
158
	*
159
	* @param  integer $size Preferred size
160
	* @return void
161
	*/
162
	public function setImageSize($size)
163
	{
164
		$this->imageSize = (int) $size;
165
		$this->resetTemplate();
166
	}
167
168
	/**
169
	* Use the EmojiOne image set
170
	*
171
	* @return void
172
	*/
173 28
	public function useEmojiOne()
174 28
	{
175 3
		$this->imageSet = 'emojione';
176 3
		$this->resetTemplate();
177 3
	}
178
179
	/**
180
	* Use PNG images if available
181
	*
182
	* @return void
183
	*/
184 2
	public function usePNG()
185
	{
186 2
		$this->imageType = 'png';
187 2
		$this->resetTemplate();
188 2
	}
189
190
	/**
191
	* Use SVG images if available
192
	*
193
	* @return void
194
	*/
195 2
	public function useSVG()
196
	{
197 2
		$this->imageType = 'svg';
198 2
		$this->resetTemplate();
199 2
	}
200
201
	/**
202
	* Use the Twemoji image set
203
	*
204
	* @return void
205
	*/
206 3
	public function useTwemoji()
207
	{
208 3
		$this->imageSet = 'twemoji';
209 3
		$this->resetTemplate();
210 3
	}
211
212
	/**
213
	* {@inheritdoc}
214
	*/
215 7
	public function asConfig()
216
	{
217
		$config = [
218 7
			'attrName' => $this->attrName,
219 7
			'tagName'  => $this->tagName
220 7
		];
221
222 7
		if (!empty($this->aliases))
223 7
		{
224 4
			$aliases = array_keys($this->aliases);
225 4
			$regexp  = '/' . RegexpBuilder::fromList($aliases) . '/';
226
227 4
			$config['aliases']       = $this->aliases;
228 4
			$config['aliasesRegexp'] = new Regexp($regexp, true);
229
230 4
			$quickMatch = ConfigHelper::generateQuickMatchFromList($aliases);
231 4
			if ($quickMatch !== false)
232 4
			{
233 3
				$config['aliasesQuickMatch'] = $quickMatch;
234 3
			}
235 4
		}
236
237 7
		return $config;
238
	}
239
240
	/**
241
	* {@inheritdoc}
242
	*/
243 4
	public function getJSHints()
244
	{
245 4
		$quickMatch = ConfigHelper::generateQuickMatchFromList(array_keys($this->aliases));
246
247
		return [
248 4
			'EMOJI_HAS_ALIASES'          => !empty($this->aliases),
249 4
			'EMOJI_HAS_ALIAS_QUICKMATCH' => ($quickMatch !== false)
250 4
		];
251
	}
252
253
	/**
254
	* Get the content of the src attribute used to display EmojiOne's images
255
	*
256
	* @return string
257
	*/
258 28
	protected function getEmojiOneSrc()
259
	{
260 28
		$src  = '//cdn.jsdelivr.net/emojione/assets/' . $this->imageType . '/';
261 28
		$src .= "<xsl:if test=\"contains(@seq, '-20e3') or @seq = 'a9' or @seq = 'ae'\">00</xsl:if>";
262 28
		$src .= '<xsl:value-of select="@seq"/>';
263 28
		$src .= '.' . $this->imageType;
264
265 28
		return $src;
266
	}
267
268
	/**
269
	* Get this tag's template
270
	*
271
	* @return string
272
	*/
273 28
	protected function getTemplate()
274
	{
275 28
		$template = '<img alt="{.}" class="emoji" draggable="false"';
276 28
		if ($this->forceImageSize)
277 28
		{
278 28
			$template .= ' width="' . $this->imageSize . '" height="' . $this->imageSize . '"';
279 28
		}
280 28
		$template .= '><xsl:attribute name="src">';
281 28
		$template .= ($this->imageSet === 'emojione') ? $this->getEmojiOneSrc() :  $this->getTwemojiSrc();
282 28
		$template .= '</xsl:attribute></img>';
283
284 28
		return $template;
285
	}
286
287
	/**
288
	* Get the content of the src attribute used to display Twemoji's images
289
	*
290
	* @return string
291
	*/
292 3
	protected function getTwemojiSrc()
293
	{
294 3
		$src  = '//twemoji.maxcdn.com/2/';
295 3
		$src .= ($this->imageType === 'svg') ? 'svg' : '72x72';
296 3
		$src .= '/<xsl:choose>';
297 3
		foreach ($this->twemojiAliases as $seq)
298
		{
299 3
			$src .= '<xsl:when test="@seq=\'' . str_replace('-200d', '', str_replace('-fe0f', '', $seq)) . '\'">' . $seq . '</xsl:when>';
300 3
		}
301 3
		$src .= '<xsl:otherwise><xsl:value-of select="@seq"/></xsl:otherwise></xsl:choose>';
302 3
		$src .= '.' . $this->imageType;
303
304 3
		return $src;
305
	}
306
307
	/**
308
	* Reset the template used by this plugin's tag
309
	*
310
	* @return void
311
	*/
312 28
	protected function resetTemplate()
313
	{
314 28
		$this->getTag()->template = $this->getTemplate();
315
	}
316
}