1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Emoji\Generator; |
4
|
|
|
|
5
|
|
|
class Parser |
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
protected $body; |
9
|
|
|
|
10
|
|
|
/** @var string */ |
11
|
|
|
protected $group; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $subgroup; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $emojis = []; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $groups = []; |
21
|
|
|
|
22
|
|
|
public function __construct(string $body) |
23
|
|
|
{ |
24
|
|
|
$this->body = $body; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function parse() |
28
|
|
|
{ |
29
|
|
|
$lines = explode("\n", $this->body); |
30
|
|
|
|
31
|
|
|
foreach ($lines as $line) { |
32
|
|
|
$this->parseLine(trim($line)); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getEmojis(): array |
37
|
|
|
{ |
38
|
|
|
return $this->emojis; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getGroups(): array |
42
|
|
|
{ |
43
|
|
|
return $this->groups; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function parseLine(string $line) |
47
|
|
|
{ |
48
|
|
|
if (strlen($line) === 0) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (strpos($line, '# group:') === 0) { |
53
|
|
|
$this->group = trim(str_replace('# group:', '', $line)); |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (strpos($line, '# subgroup:') === 0) { |
59
|
|
|
$this->subgroup = trim(str_replace('# subgroup:', '', $line)); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (strpos($line, '#') === 0) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
preg_match('/^([A-Z\d ]+)\s+;\s+(fully-qualified|component)\s+# [^a-z ]* (.+)$/', $line, $matches); |
69
|
|
|
if (count($matches) === 4) { |
70
|
|
|
[, $code, , $name] = $matches; |
|
|
|
|
71
|
|
|
|
72
|
|
|
if ( |
73
|
|
|
$this->subgroup === 'country-flag' |
74
|
|
|
|| $this->subgroup === 'subdivision-flag' |
75
|
|
|
) { |
76
|
|
|
$name = 'flags for: '.$name; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->addEmoji(trim($code), trim($name)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function addEmoji(string $code, string $name) |
84
|
|
|
{ |
85
|
|
|
$emoji = new Emoji($name, $code); |
86
|
|
|
|
87
|
|
|
$this->groups[$this->group][$this->subgroup][$code] = $emoji; |
88
|
|
|
$this->emojis[$code] = $emoji; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.