1
|
|
|
<?php |
2
|
|
|
namespace Buttress\IRC\Message; |
3
|
|
|
|
4
|
|
|
use Buttress\IRC\Connection\ConnectionInterface; |
5
|
|
|
|
6
|
|
|
class MessageFactory |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
protected $constructors; |
10
|
|
|
|
11
|
|
|
public function register($command, callable $generator) |
12
|
|
|
{ |
13
|
|
|
$this->constructors[$command] = $generator; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $raw |
18
|
|
|
* @param ConnectionInterface $connection |
19
|
|
|
* @return MessageInterface |
20
|
|
|
*/ |
21
|
|
|
public function getMessageFromRaw($raw, ConnectionInterface $connection = null) |
22
|
|
|
{ |
23
|
|
|
list($prefix, $command, $params) = $this->deconstructRaw($raw); |
24
|
|
|
$message = $this->make($command, $prefix, $params, $connection); |
25
|
|
|
|
26
|
|
|
return $message; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $raw_message |
31
|
|
|
*/ |
32
|
|
|
public function deconstructRaw($raw_message) |
33
|
|
|
{ |
34
|
|
|
$prefix = ""; |
35
|
|
|
|
36
|
|
|
if (substr($raw_message, 0, 1) == ':') { |
37
|
|
|
$prefix_end = strpos($raw_message, ' '); |
38
|
|
|
$prefix = substr($raw_message, 1, $prefix_end - 1); |
39
|
|
|
$raw = trim(substr($raw_message, $prefix_end + 1)); |
40
|
|
|
} else { |
41
|
|
|
$raw = trim($raw_message); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$last_param = null; |
45
|
|
|
if ($last_param_pos = strpos($raw, ' :')) { |
46
|
|
|
$last_param = substr($raw, $last_param_pos + 2); |
47
|
|
|
$raw = substr($raw, 0, $last_param_pos); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$exploded = explode(' ', $raw); |
51
|
|
|
$command = array_shift($exploded); |
52
|
|
|
|
53
|
|
|
if ($last_param) { |
|
|
|
|
54
|
|
|
$exploded[] = $last_param; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return array($prefix, $command, $exploded); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $command |
62
|
|
|
* @param string $prefix |
63
|
|
|
* @param array $params |
64
|
|
|
* @param ConnectionInterface $connection |
65
|
|
|
* @return GenericMessage |
66
|
|
|
*/ |
67
|
|
|
public function make($command, $prefix = "", array $params = array(), ConnectionInterface $connection = null) |
68
|
|
|
{ |
69
|
|
|
$command = strtoupper($command); |
70
|
|
|
|
71
|
|
|
if (isset($this->constructors[$command])) { |
72
|
|
|
return $this->constructors[$command]($command, $prefix, $params, $connection); |
73
|
|
|
} else { |
74
|
|
|
$method = "construct" . substr($command, 0, 1) . substr($command, 1); |
75
|
|
|
if (method_exists($this, $method)) { |
76
|
|
|
return $this->{$method}($command, $prefix, $params, $connection); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new GenericMessage($command, $prefix, $params, $connection); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function constructPrivmsg($command, $prefix, $params, $connection) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return new PrivmsgMessage($prefix, $params, $connection); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function constructPing($command, $prefix, $params, $connection) |
89
|
|
|
{ |
90
|
|
|
return new PingMessage($command, $prefix, $params, $connection); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: