Passed
Push — master ( 2d938a...0d8733 )
by Joas
14:58 queued 12s
created

EmojiHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B isValidSingleEmoji() 0 39 10
A doesPlatformSupportEmoji() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
namespace OC;
28
29
use OCP\IDBConnection;
30
use OCP\IEmojiHelper;
31
32
class EmojiHelper implements IEmojiHelper {
33
	private IDBConnection $db;
34
35
	public function __construct(IDBConnection $db) {
36
		$this->db = $db;
37
	}
38
39
	public function doesPlatformSupportEmoji(): bool {
40
		return $this->db->supports4ByteText() &&
41
			\class_exists(\IntlBreakIterator::class);
42
	}
43
44
	public function isValidSingleEmoji(string $emoji): bool {
45
		$intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
46
		$intlBreakIterator->setText($emoji);
47
48
		$characterCount = 0;
49
		while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
50
			$characterCount++;
51
		}
52
53
		if ($characterCount !== 1) {
54
			return false;
55
		}
56
57
		$codePointIterator = \IntlBreakIterator::createCodePointInstance();
58
		$codePointIterator->setText($emoji);
59
60
		foreach ($codePointIterator->getPartsIterator() as $codePoint) {
61
			$codePointType = \IntlChar::charType($codePoint);
62
63
			// If the current code-point is an emoji or a modifier (like a skin-tone)
64
			// just continue and check the next character
65
			if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
66
				$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
67
				$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL ||
68
				$codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES) {
69
				continue;
70
			}
71
72
			// If it's neither a modifier nor an emoji, we only allow
73
			// a zero-width-joiner or a variation selector 16
74
			$codePointValue = \IntlChar::ord($codePoint);
75
			if ($codePointValue === 8205 || $codePointValue === 65039) {
76
				continue;
77
			}
78
79
			return false;
80
		}
81
82
		return true;
83
	}
84
}
85