1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crummy\Phlack\Bot; |
4
|
|
|
|
5
|
|
|
use Crummy\Phlack\Common\Exception\InvalidArgumentException; |
6
|
|
|
use Crummy\Phlack\Common\Iterocitor; |
7
|
|
|
use Crummy\Phlack\Common\Matcher; |
8
|
|
|
use Crummy\Phlack\Common\Responder\ResponderInterface; |
9
|
|
|
use Crummy\Phlack\Message\MessageInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractBot implements BotInterface, Matcher\MatcherAggregate |
12
|
|
|
{ |
13
|
|
|
private $matcher; |
14
|
|
|
private $responder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Matcher\MatcherInterface $matcher |
18
|
|
|
* @param array|ResponderInterface $options |
19
|
|
|
*/ |
20
|
11 |
|
public function __construct($matcher = null, $options = []) |
21
|
|
|
{ |
22
|
11 |
|
if (!$matcher) { |
23
|
9 |
|
$matcher = new Matcher\DefaultMatcher(); |
24
|
8 |
|
} |
25
|
|
|
|
26
|
11 |
|
$this->setMatcher($matcher); |
27
|
11 |
|
$this->responder = $options instanceof ResponderInterface ? $options : new Iterocitor($options); |
28
|
11 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Matcher\MatcherInterface $matcher |
32
|
|
|
* |
33
|
|
|
* @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher. |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
11 |
|
public function setMatcher($matcher) |
38
|
|
|
{ |
39
|
11 |
|
if (!$matcher instanceof Matcher\MatcherInterface && !is_callable($matcher)) { |
40
|
1 |
|
throw new InvalidArgumentException(sprintf( |
41
|
1 |
|
'The matcher must be callable, or implement \Crummy\Phlack\Common\Matcher\MatcherInterface. "%" given.', |
42
|
1 |
|
is_object($matcher) ? get_class($matcher) : gettype($matcher) |
43
|
1 |
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
11 |
|
$this->matcher = $matcher; |
47
|
|
|
|
48
|
11 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Matcher\MatcherInterface|callable |
53
|
|
|
*/ |
54
|
2 |
|
public function getMatcher() |
55
|
|
|
{ |
56
|
2 |
|
return $this->matcher; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $text |
61
|
|
|
* |
62
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
63
|
|
|
*/ |
64
|
2 |
|
protected function say($text) |
65
|
|
|
{ |
66
|
2 |
|
return $this->responder->say($text); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $text |
71
|
|
|
* |
72
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
73
|
|
|
*/ |
74
|
|
|
protected function emote($text) |
75
|
|
|
{ |
76
|
|
|
return $this->responder->emote($text); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $user The user_id to tell |
81
|
|
|
* @param string $text |
82
|
|
|
* |
83
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
84
|
|
|
*/ |
85
|
|
|
protected function tell($user, $text) |
86
|
|
|
{ |
87
|
|
|
return $this->responder->tell($user, $text); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \Crummy\Phlack\WebHook\CommandInterface $user The user_id, or a CommandInterface to inspect. |
92
|
|
|
* @param string $text |
93
|
|
|
* |
94
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
95
|
|
|
*/ |
96
|
4 |
|
protected function reply($user, $text) |
97
|
|
|
{ |
98
|
4 |
|
return $this->responder->reply($user, $text); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $text |
103
|
|
|
* |
104
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\Reply |
105
|
|
|
*/ |
106
|
|
|
protected function shout($text) |
107
|
|
|
{ |
108
|
|
|
return $this->responder->shout($text); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param MessageInterface $message |
113
|
|
|
* |
114
|
|
|
* @return \Crummy\Phlack\WebHook\Reply\EmptyReply |
115
|
|
|
*/ |
116
|
|
|
protected function send(MessageInterface $message) |
117
|
|
|
{ |
118
|
|
|
return $this->responder->send($message); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|