Assembla home | Assembla project page
 

root/trunk/Phergie/Event/Request.php

Revision 215, 8.1 kB (checked in by Slynderdale, 5 months ago)

Cleaned up the code and beautified it. Fixed all the indentation and went with a unified code format.

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