-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageCollection.php
59 lines (50 loc) · 1.33 KB
/
MessageCollection.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
<?php
declare(strict_types=1);
namespace voniersa\twitch\livechat;
use ArrayIterator;
class MessageCollection
{
private array $collection;
/**
* @throws IncorrectDataProvidedException
*/
private function __construct(array $messages)
{
foreach ($messages as $message) {
if (!($message instanceof Message)) {
throw new IncorrectDataProvidedException("Instance of Message was expected.");
}
$this->add($message);
}
}
/**
* @param array $messages array of messages that were sent
* @return MessageCollection collection of messages that were sent
* @throws IncorrectDataProvidedException
*/
public static function fromArray(array $messages): MessageCollection
{
return new self($messages);
}
/**
* @return ArrayIterator iterator for all sent messages
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->collection);
}
/**
* @return int number of messages in the collection
*/
public function count(): int
{
return count($this->collection);
}
/**
* @param Message $Message message that was sent
*/
public function add(Message $Message): void
{
$this->collection[] = $Message;
}
}