-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart.php
96 lines (83 loc) · 2.48 KB
/
Part.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Copyright 2019 The WildPHP Team
*
* You should have received a copy of the MIT license with the project.
* See the LICENSE file for more information.
*/
declare(strict_types=1);
namespace WildPHP\Messages;
use InvalidArgumentException;
use WildPHP\Messages\Generics\BaseIRCMessageImplementation;
use WildPHP\Messages\Generics\Prefix;
use WildPHP\Messages\Interfaces\IncomingMessageInterface;
use WildPHP\Messages\Interfaces\IrcMessageInterface;
use WildPHP\Messages\Interfaces\OutgoingMessageInterface;
use WildPHP\Messages\Traits\MessageTrait;
use WildPHP\Messages\Traits\NicknameTrait;
use WildPHP\Messages\Traits\PrefixTrait;
/**
* Class PART
* @package WildPHP\Messages
*
* Syntax: prefix PART #channel [:message]
* Syntax (sender): PART #channels [:message]
*/
class Part extends BaseIRCMessageImplementation implements IncomingMessageInterface, OutgoingMessageInterface
{
/**
* @var string
*/
protected static $verb = 'PART';
use Traits\ChannelsTrait;
use NicknameTrait;
use PrefixTrait;
use MessageTrait;
/**
* PART constructor.
*
* @param $channels
* @param string $message
*/
public function __construct($channels, $message = '')
{
if (!is_array($channels)) {
$channels = [$channels];
}
$this->setChannels($channels);
$this->setMessage($message);
}
/**
* @param IrcMessageInterface $incomingMessage
*
* @return self
*/
public static function fromIncomingMessage(IrcMessageInterface $incomingMessage): self
{
if ($incomingMessage->getVerb() !== self::getVerb()) {
throw new InvalidArgumentException(sprintf(
'Expected incoming %s; got %s',
self::getVerb(),
$incomingMessage->getVerb()
));
}
$prefix = Prefix::fromIncomingMessage($incomingMessage);
$args = $incomingMessage->getArgs();
$channel = $args[0];
$message = $args[1] ?? '';
$object = new self($channel, $message);
$object->setPrefix($prefix);
$object->setNickname($prefix->getNickname());
$object->setTags($incomingMessage->getTags());
return $object;
}
/**
* @return string
*/
public function __toString()
{
$channels = implode(',', $this->getChannels());
$message = $this->getMessage();
return 'PART ' . $channels . (!empty($message) ? ' :' . $message : '') . "\r\n";
}
}