|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface; |
|
6
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
7
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ChatNotification |
|
11
|
|
|
* |
|
12
|
|
|
* @package eXpansion\Framework\Core\Helpers; |
|
13
|
|
|
* @author oliver de Cramer <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ChatNotification implements ChatNotificationInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Connection */ |
|
18
|
|
|
protected $connection; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Translations */ |
|
21
|
|
|
protected $translations; |
|
22
|
|
|
|
|
23
|
|
|
/** @var PlayerStorage */ |
|
24
|
|
|
protected $playerStorage; |
|
25
|
|
|
|
|
26
|
|
|
protected $colorCodes = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* ChatNotification constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Connection $connection |
|
32
|
|
|
* @param Translations $translations |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function __construct( |
|
35
|
|
|
Connection $connection, |
|
36
|
|
|
Translations $translations, |
|
37
|
|
|
PlayerStorage $playerStorage, |
|
38
|
|
|
$colorCodes |
|
39
|
|
|
){ |
|
40
|
6 |
|
$this->connection = $connection; |
|
41
|
6 |
|
$this->translations = $translations; |
|
42
|
6 |
|
$this->playerStorage = $playerStorage; |
|
43
|
|
|
|
|
44
|
6 |
|
foreach ($colorCodes as $code => $colorCode) { |
|
45
|
6 |
|
$this->colorCodes["{" . $code . "}"] = '$z' . $colorCode; |
|
46
|
|
|
} |
|
47
|
6 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Send message. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $messageId |
|
53
|
|
|
* @param string|string[]|null $to |
|
54
|
|
|
* @param string[] $parameters |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function sendMessage($messageId, $to = null, $parameters = []) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$parameters = array_merge($this->colorCodes, $parameters); |
|
59
|
|
|
|
|
60
|
2 |
|
if (is_string($to)) { |
|
61
|
1 |
|
$player = $this->playerStorage->getPlayerInfo($to); |
|
62
|
1 |
|
$message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage())); |
|
63
|
|
|
} else { |
|
64
|
1 |
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$this->connection->chatSendServerMessage($message, $to); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.