|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Votifier PHP Client |
|
5
|
|
|
* |
|
6
|
|
|
* @package VotifierClient |
|
7
|
|
|
* @author Manuele Vaccari <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]> |
|
9
|
|
|
* @license https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0 |
|
10
|
|
|
* @link https://github.com/D3strukt0r/votifier-client-php |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace D3strukt0r\VotifierClient; |
|
14
|
|
|
|
|
15
|
|
|
use function func_num_args; |
|
16
|
|
|
use function is_array; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Internal use for translations. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Messages |
|
22
|
|
|
{ |
|
23
|
|
|
public const NOT_VOTIFIER = 1; |
|
24
|
|
|
public const NOT_SENT_PACKAGE = 2; |
|
25
|
|
|
public const NOT_RECEIVED_PACKAGE = 3; |
|
26
|
|
|
|
|
27
|
|
|
public const NUVOTIFIER_SERVER_ERROR = 100; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Translate and format a translation. |
|
31
|
|
|
* |
|
32
|
|
|
* @param int $messageCode (Required) The message code to identify the required resource |
|
33
|
|
|
* @param string|null $language (Optional) The language code (e. g. en, de, es). |
|
34
|
|
|
* |
|
35
|
|
|
* @return string returns the message in the specified language |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public static function get(int $messageCode, string $language = null): string |
|
38
|
|
|
{ |
|
39
|
|
|
$messages = [ |
|
40
|
|
|
'en' => [ |
|
41
|
2 |
|
self::NOT_VOTIFIER => 'The connection does not belong to Votifier', |
|
42
|
2 |
|
self::NOT_SENT_PACKAGE => 'Couldn\'t write to remote host', |
|
43
|
2 |
|
self::NOT_RECEIVED_PACKAGE => 'Unable to read server response', |
|
44
|
2 |
|
self::NUVOTIFIER_SERVER_ERROR => 'Votifier server error: {0}: {1}', |
|
45
|
|
|
], |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
2 |
|
$requestedMessage = $messages[$language ?: 'en'][$messageCode]; |
|
49
|
|
|
|
|
50
|
2 |
|
$argsCount = func_num_args(); |
|
51
|
|
|
|
|
52
|
2 |
|
if ($argsCount > 2) { |
|
53
|
1 |
|
$firstArg = func_get_arg(2); |
|
54
|
1 |
|
if (is_array($firstArg)) { |
|
55
|
1 |
|
foreach ($firstArg as $key => $value) { |
|
56
|
1 |
|
$requestedMessage = str_replace('{'.$key.'}', $value, $requestedMessage); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
1 |
|
for ($i = 2; $i < $argsCount; ++$i) { |
|
60
|
1 |
|
$arg = func_get_arg($i); |
|
61
|
1 |
|
$requestedMessage = str_replace('{'.($i - 2).'}', $arg, $requestedMessage); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
return $requestedMessage; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|