root/trunk/Phergie/Event/Request.php @ 83

Revision 83, 7.4 KB (checked in by tobias382, 5 years ago)

Fixes #17
* Added named argument support to the request event class

Line 
1<?php
2
3/**
4* Autonomous event originating from a user or the server.
5*
6* @see http://www.irchelp.org/irchelp/rfc/chapter4.html
7*/
8class Phergie_Event_Request
9{
10    /**
11    * Nick message
12    *
13    * @const string
14    */
15    const TYPE_NICK = 'nick';
16
17    /**
18    * Quit command
19    *
20    * @const string
21    */
22    const TYPE_QUIT = 'quit';
23
24    /**
25    * Join message
26    *
27    * @const string
28    */
29    const TYPE_JOIN = 'join';
30
31    /**
32    * Kick message
33    *
34    * @const string
35    */
36    const TYPE_KICK = 'kick';
37
38    /**
39    * Part message
40    *
41    * @const string
42    */
43    const TYPE_PART = 'part';
44
45    /**
46    * Mode message
47    *
48    * @const string
49    */
50    const TYPE_MODE = 'mode';
51
52    /**
53    * Topic message
54    *
55    * @const string
56    */
57    const TYPE_TOPIC = 'topic';
58
59    /**
60    * Private message command
61    *
62    * @const string
63    */
64    const TYPE_PRIVMSG = 'privmsg';
65
66    /**
67    * Notice message
68    *
69    * @const string
70    */
71    const TYPE_NOTICE = 'notice';
72
73    /**
74    * Pong message
75    *
76    * @const string
77    */
78    const TYPE_PONG = 'pong';
79
80    /**
81    * CTCP ACTION command
82    *
83    * @const string
84    */
85    const TYPE_ACTION = 'action';
86
87    /**
88    * Mapping of event types to their named parameters
89    *
90    * @var array
91    */
92    protected static $map = array
93    (
94        self::TYPE_QUIT => array(
95            'message' => 0
96        ),
97
98        self::TYPE_JOIN => array(
99            'channel' => 0
100        ),
101       
102        self::TYPE_KICK => array(
103            'channel' => 0,
104            'user'    => 1,
105            'comment' => 2
106        ),
107
108        self::TYPE_PART => array(
109            'channel' => 0
110        ),
111
112        self::TYPE_MODE => array(
113            'channel|nickname' => 0,
114            'mode'             => 1,
115            'limit'            => 2,
116            'user'             => 3,
117            'ban mask'         => 4
118        ),
119
120        self::TYPE_TOPIC => array(
121            'channel' => 0,
122            'topic'   => 1
123        ),
124
125        self::TYPE_PRIVMSG => array(
126            'receiver' => 0,
127            'text'     => 1
128        ),
129
130        self::TYPE_NOTICE => array(
131            'nickname' => 0,
132            'text'     => 1
133        ),
134           
135        self::TYPE_ACTION => array(
136            'action' => 0
137        )
138    );
139
140    /**
141    * Host name for the originating server or user
142    *
143    * @var string
144    */
145    protected $host;
146
147    /**
148    * Username of the user from which the event originates
149    *
150    * @var string
151    */
152    protected $username;
153
154    /**
155    * Nick of the user from which the event originates
156    *
157    * @var string
158    */
159    protected $nick;
160
161    /**
162    * Request type, which can be compared to the TYPE_* constants
163    *
164    * @var string
165    */
166    protected $type;
167
168    /**
169    * Arguments included with the message
170    *
171    * @var array
172    */
173    protected $arguments;
174
175    /**
176    * Returns the hostmask for the originating server or user.
177    *
178    * @return string
179    */
180    public function getHostmask()
181    {
182        return $this->nick . '!' . $this->username . '@' . $this->host;
183    }
184
185    /**
186    * Sets the host name for the originating server or user.
187    *
188    * @param string $host
189    * @return void
190    */
191    public function setHost($host)
192    {
193        $this->host = $host;
194    }
195
196    /**
197    * Returns the host name for the originating server or user.
198    *
199    * @return string
200    */
201    public function getHost()
202    {
203        return $this->host;
204    }
205
206    /**
207    * Sets the username of the user from which the event originates.
208    *
209    * @param string $username
210    * @return void
211    */
212    public function setUsername($username)
213    {
214        $this->username = $username;
215    }
216
217    /**
218    * Returns the username of the user from which the event originates.
219    *
220    * @return string
221    * @return void
222    */
223    public function getUsername()
224    {
225        return $this->username;
226    }
227
228    /**
229    * Sets the nick of the user from which the event originates.
230    *
231    * @param string $nick
232    * @return void
233    */
234    public function setNick($nick)
235    {
236        $this->nick = $nick;
237    }
238
239    /**
240    * Returns the nick of the user from which the event originates.
241    *
242    * @return string
243    * @return void
244    */
245    public function getNick()
246    {
247        return $this->nick;
248    }
249
250    /**
251    * Sets the request type.
252    *
253    * @param string $type
254    * @return void
255    */
256    public function setType($type)
257    {
258        $this->type = strtolower($type);
259    }
260
261    /**
262    * Returns the request type, which can be compared to the TYPE_*
263    * constants.
264    *
265    * @return string
266    */
267    public function getType()
268    {
269        return $this->type;
270    }
271
272    /**
273    * Sets the arguments for the request in the order they are to be sent.
274    *
275    * @param array $arguments
276    * @return void
277    */
278    public function setArguments($arguments)
279    {
280        $this->arguments = $arguments;
281    }
282
283    /**
284    * Returns the arguments for the request in the order they are to be sent.
285    *
286    * @return array
287    */
288    public function getArguments()
289    {
290        return $this->arguments;
291    }
292
293    /**
294    * Returns a single specified argument for the request.
295    *
296    * @param int $argument Position of the argument in the list, starting
297    *                      from 0
298    * @return string
299    */
300    public function getArgument($argument)
301    {
302        if (isset($this->arguments[$argument])) {
303            return $this->arguments[$argument];
304        } else {
305            $argument = strtolower($argument);
306            if (isset(self::$map[$this->type][$argument])
307                && isset($this->arguments[self::$map[$this->type][$argument]])) {
308                echo 'returning ' . $this->arguments[self::$map[$this->type][$argument]] . "\n";
309                return $this->arguments[self::$map[$this->type][$argument]];
310            }
311        }
312        return null;
313    }
314
315    /**
316    * Returns the channel name or user nick representing the source of the
317    * event.
318    *
319    * @return string
320    */
321    public function getSource()
322    {
323        if ($this->arguments[0][0] == '#') {
324            return $this->arguments[0];
325        }
326        return $this->nick;
327    }
328
329    /**
330    * Returns whether or not the event occurred within a channel.
331    *
332    * @return TRUE if the event is in a channel, FALSE otherwise
333    */
334    public function isInChannel()
335    {
336        return (substr($this->getSource(), 0, 1) == '#');
337    }
338
339    /**
340    * Returns whether or not the event originated from a user.
341    *
342    * @return TRUE if the event is from a user, FALSE otherwise
343    */
344    public function isFromUser()
345    {
346        return ! empty($this->username);
347    }
348
349    /**
350    * Returns whether or not the event originated from the server.
351    *
352    * @return TRUE if the event is from the server, FALSE otherwise
353    */
354    public function isFromServer()
355    {
356        return empty($this->username);
357    }
358
359    /**
360    * Provides access to named parameters via virtual "getter" methods.
361    *
362    * @param string $name Name of the method called
363    * @param array $arguments Arguments passed to the method (should always
364    *                         be empty)
365    * @return mixed
366    */
367    public function __call($name, array $arguments)
368    {
369        $index = strtolower(substr($name, 4));
370        if (count($arguments) == 0
371            && substr($name, 0, 3) == 'get') {
372            return $this->getArgument(substr($name, 3));
373        }
374    }
375}
Note: See TracBrowser for help on using the browser.