Completed
Push — master ( 6323ac...52893e )
by
unknown
6s
created

RawMessage::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
declare(strict_types=1);
3
4
namespace Spires\Irc\Message\Inbound;
5
6
class RawMessage extends \Spires\Irc\Message\Outbound\RawMessage
7
{
8
    protected $nickname;
9
    protected $username;
10
    protected $hostname;
11
    protected $serverName;
12
13
    public function __construct(
14
        string $nickname,
15
        string $username,
16
        string $hostname,
17
        string $serverName,
18
        string $command,
19
        string $params
20
    ) {
21
        $this->nickname = $nickname;
22
        $this->username = $username;
23
        $this->hostname = $hostname;
24
        $this->serverName = $serverName;
25
26
        parent::__construct($command, $params);
27
    }
28
29
    /**
30
     * Create a RawMessage from the output of Parser::parse()
31
     *
32
     * @param array $messageParts
33
     * @return RawMessage
34
     */
35
    public static function fromArray($messageParts)
36
    {
37
        return new RawMessage(
38
            $messageParts['nickname'],
39
            $messageParts['username'],
40
            $messageParts['hostname'],
41
            $messageParts['serverName'],
42
            $messageParts['command'],
43
            $messageParts['params']
44
        );
45
    }
46
47
    public static function from(RawMessage $message)
48
    {
49
        return new static(
50
            $message->nickname(),
51
            $message->username(),
52
            $message->hostname(),
53
            $message->serverName(),
54
            $message->command(),
55
            $message->params()
56
        );
57
    }
58
59
    public function nickname() : string
60
    {
61
        return $this->nickname;
62
    }
63
64
    public function username() : string
65
    {
66
        return $this->username;
67
    }
68
69
    public function hostname() : string
70
    {
71
        return $this->hostname;
72
    }
73
74
    public function serverName() : string
75
    {
76
        return $this->serverName;
77
    }
78
79
    public function prefix()
80
    {
81
        $prefix = '';
82
83
        $prefix .= empty($this->nickname) ? '' : ":{$this->nickname}";
84
        $prefix .= empty($this->username) ? '' : "!{$this->username}";
85
        $prefix .= empty($this->hostname) ? '' : "@{$this->hostname}";
86
        $prefix .= empty($this->serverName) ? '' : ":{$this->serverName}";
87
88
        return trim($prefix);
89
    }
90
91
    public function __toString() : string
92
    {
93
        return trim($this->prefix() . ' ' . trim($this->command() . ' ' . $this->params));
94
    }
95
96
    public function toArray() : array
97
    {
98
        return [
99
            'nickname' => $this->nickname,
100
            'username' => $this->username,
101
            'hostname' => $this->hostname,
102
            'serverName' => $this->serverName,
103
            'command' => $this->command,
104
            'params' => $this->params,
105
        ];
106
    }
107
}
108