Passed
Push — master ( fc826e...290697 )
by Joas
44:06 queued 12s
created

EmojiHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A doesPlatformSupportEmoji() 0 3 2
D isValidSingleEmoji() 0 52 18
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
			// Unicode chars need 2 or more chars
64
			// The characterCount before this loop already validate if is a single emoji
65
			// This condition is to don't continue if non emoji chars
66
			if (strlen($emoji) >= 2) {
67
				// If the current code-point is an emoji or a modifier (like a skin-tone)
68
				// just continue and check the next character
69
				if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
70
					$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
71
					$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL ||
72
					$codePointType === \IntlChar::CHAR_CATEGORY_FORMAT_CHAR ||          // i.e. 🏴󠁧󠁢󠁥󠁮󠁧󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿
73
					$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_PUNCTUATION ||    // i.e. ‼️ ⁉️ #⃣
74
					$codePointType === \IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER ||     // i.e. ℹ️
75
					$codePointType === \IntlChar::CHAR_CATEGORY_MATH_SYMBOL ||          // i.e. ↔️ ◻️ ⤴️ ⤵️
76
					$codePointType === \IntlChar::CHAR_CATEGORY_ENCLOSING_MARK ||       // i.e. 0⃣..9⃣
77
					$codePointType === \IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER || // i.e. 0⃣..9⃣
78
					$codePointType === \IntlChar::CHAR_CATEGORY_DASH_PUNCTUATION ||     // i.e. 〰️
79
					$codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES
80
				) {
81
					continue;
82
				}
83
			}
84
85
			// If it's neither a modifier nor an emoji, we only allow
86
			// a zero-width-joiner or a variation selector 16
87
			$codePointValue = \IntlChar::ord($codePoint);
88
			if ($codePointValue === 8205 || $codePointValue === 65039) {
89
				continue;
90
			}
91
92
			return false;
93
		}
94
95
		return true;
96
	}
97
}
98