Message::commaList()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
	 * @phan-param array<string,int|string> $args
33
	 * @return self
34
	 */
35
	public function params( array $args ): self {
36
		$this->value = strtr( $this->value, $args );
37
		return $this;
38
	}
39
40
	/**
41
	 * @return string
42
	 */
43
	public function text(): string {
44
		$this->parsePlurals();
45
		return $this->value;
46
	}
47
48
	/**
49
	 * Replace {{$plur|<amount>|sing|plur}}
50
	 */
51
	protected function parsePlurals(): void {
52
		$reg = '!\{\{\$plur\|(?P<amount>\d+)\|(?P<sing>[^}|]+)\|(?P<plur>[^|}]+)}}!';
53
54
		if ( preg_match( $reg, $this->value ) === 0 ) {
55
			return;
56
		}
57
		$this->value = preg_replace_callback(
58
			$reg,
59
			/**
60
			 * @param string[] $matches
61
			 * @return string
62
			 */
63
			static function ( array $matches ): string {
64
				return (int)$matches['amount'] > 1 ? trim( $matches['plur'] ) : trim( $matches['sing'] );
65
			},
66
			$this->value
67
		);
68
	}
69
70
	/**
71
	 * Get a timestamp from a localized time string
72
	 *
73
	 * @param string $timeString Full format, e.g. "15 aprile 2019 18:27"
74
	 * @return int
75
	 */
76
	public static function getTimestampFromLocalTime( string $timeString ): int {
77
		$englishTime = str_ireplace(
78
			array_values( self::MONTHS ),
79
			array_keys( self::MONTHS ),
80
			$timeString
81
		);
82
83
		return strtotime( $englishTime );
84
	}
85
86
	/**
87
	 * Given an array of data, returns a list of its elements using commas, and " e " before
88
	 * the last one. $emptyText can be used to specify the text in case $data is empty.
89
	 *
90
	 * @param array $data
91
	 * @phan-param array<int|string|\Stringable> $data
92
	 * @param string $emptyText
93
	 * @return string
94
	 */
95
	public static function commaList( array $data, string $emptyText = 'nessuno' ): string {
96
		if ( count( $data ) > 1 ) {
97
			$last = array_pop( $data );
98
			$ret = implode( ', ', $data ) . " e $last";
99
		} elseif ( $data ) {
100
			$ret = (string)$data[0];
101
		} else {
102
			$ret = $emptyText;
103
		}
104
105
		return $ret;
106
	}
107
108
	public function __toString(): string {
109
		return $this->text();
110
	}
111
}
112