Completed
Push — master ( baad29...0e040d )
by Josh
18:45 queued 15:21
created

Configurator::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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