|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Nopolabs\Yabot\Message\Message; |
|
7
|
|
|
|
|
8
|
|
|
trait MatcherTrait |
|
9
|
|
|
{ |
|
10
|
|
|
private $name; |
|
11
|
|
|
private $isBot; |
|
12
|
|
|
private $channels; |
|
13
|
|
|
private $users; |
|
14
|
|
|
private $patterns; |
|
15
|
|
|
|
|
16
|
|
|
abstract protected function debug($message, array $context = array()); |
|
17
|
|
|
|
|
18
|
|
|
public function matches(Message $message) : array |
|
19
|
|
|
{ |
|
20
|
|
|
if (!$this->matchesIsBot($message)) { |
|
21
|
|
|
return []; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
if (!$this->matchesChannels($message)) { |
|
25
|
|
|
return []; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if (!$this->matchesUsers($message)) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (empty($this->patterns)) { |
|
33
|
|
|
return [$message->getFormattedText()]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $this->matchPatterns($message); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function matchesIsBot(Message $message) : bool |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->isBot !== null && $this->isBot !== $message->isBot()) { |
|
42
|
|
|
$this->debug($this->name.': isBot match failed'); |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
protected function matchesChannels(Message $message) : bool |
|
50
|
|
|
{ |
|
51
|
|
|
if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) { |
|
52
|
|
|
$this->debug($this->name.': channels match failed '.json_encode($this->channels)); |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
protected function matchesUsers(Message $message) : bool |
|
60
|
|
|
{ |
|
61
|
|
|
if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) { |
|
62
|
|
|
$this->debug($this->name.': users match failed '.json_encode($this->users)); |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function matchPatterns(Message $message): array |
|
70
|
|
|
{ |
|
71
|
|
|
$matches = []; |
|
72
|
|
|
$text = $message->getPluginText(); |
|
73
|
|
|
foreach ($this->patterns as $pattern) { |
|
74
|
|
|
if (preg_match($pattern, $text, $matches)) { |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $matches; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function setName($name) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->name = $name; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function setIsBot($isBot) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->isBot = $isBot; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function setChannels(array $channels) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->channels = $channels; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function setUsers(array $users) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->users = $users; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function setPatterns(array $patterns) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->patterns = $patterns; |
|
105
|
|
|
} |
|
106
|
|
|
} |