1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme\Message; |
4
|
|
|
|
5
|
|
|
class Message { |
6
|
|
|
public const MONTHS = [ |
7
|
|
|
'January' => 'gennaio', |
8
|
|
|
'February' => 'febbraio', |
9
|
|
|
'March' => 'marzo', |
10
|
|
|
'April' => 'aprile', |
11
|
|
|
'May' => 'maggio', |
12
|
|
|
'June' => 'giugno', |
13
|
|
|
'July' => 'luglio', |
14
|
|
|
'August' => 'agosto', |
15
|
|
|
'September' => 'settembre', |
16
|
|
|
'October' => 'ottobre', |
17
|
|
|
'November' => 'novembre', |
18
|
|
|
'December' => 'dicembre' |
19
|
|
|
]; |
20
|
|
|
/** @var string */ |
21
|
|
|
private $value; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $value |
25
|
|
|
*/ |
26
|
|
|
public function __construct( string $value ) { |
27
|
|
|
$this->value = $value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $args |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
|
|
public function params( array $args ) : self { |
35
|
|
|
$this->value = strtr( $this->value, $args ); |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function text() : string { |
43
|
|
|
$this->parsePlurals(); |
44
|
|
|
return $this->value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Replace {{$plur|<amount>|sing|plur}} |
49
|
|
|
*/ |
50
|
|
|
protected function parsePlurals() : void { |
51
|
|
|
$reg = '!\{\{\$plur\|(?P<amount>\d+)\|(?P<sing>[^}|]+)\|(?P<plur>[^|}]+)}}!'; |
52
|
|
|
|
53
|
|
|
if ( preg_match( $reg, $this->value ) === 0 ) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
$this->value = preg_replace_callback( |
57
|
|
|
$reg, |
58
|
|
|
static function ( $matches ) { |
59
|
|
|
return (int)$matches['amount'] > 1 ? trim( $matches['plur'] ) : trim( $matches['sing'] ); |
60
|
|
|
}, |
61
|
|
|
$this->value |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a timestamp from a localized time string |
67
|
|
|
* |
68
|
|
|
* @param string $timeString Full format, e.g. "15 aprile 2019 18:27" |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public static function getTimestampFromLocalTime( string $timeString ) : int { |
72
|
|
|
$englishTime = str_ireplace( |
73
|
|
|
array_values( self::MONTHS ), |
74
|
|
|
array_keys( self::MONTHS ), |
75
|
|
|
$timeString |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return strtotime( $englishTime ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Given an array of data, returns a list of its elements using commas, and " e " before |
83
|
|
|
* the last one. $emptyText can be used to specify the text in case $data is empty. |
84
|
|
|
* |
85
|
|
|
* @param array $data |
86
|
|
|
* @param string $emptyText |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public static function commaList( array $data, string $emptyText = 'nessuno' ) : string { |
90
|
|
|
if ( count( $data ) > 1 ) { |
91
|
|
|
$last = array_pop( $data ); |
92
|
|
|
$ret = implode( ', ', $data ) . " e $last"; |
93
|
|
|
} elseif ( $data ) { |
94
|
|
|
$ret = (string)$data[0]; |
95
|
|
|
} else { |
96
|
|
|
$ret = $emptyText; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $ret; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function __toString() { |
103
|
|
|
return $this->text(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|