Passed
Push — master ( 98e5c5...7ca7e9 )
by Daimona
01:28
created

Message::parsePlurals()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
/**
6
 *
7
 */
8
class Message {
9
	/** @var string */
10
	private $key;
11
	/** @var string */
12
	private $value;
13
14
	/**
15
	 * @param string $key
16
	 */
17
	public function __construct( string $key ) {
18
		$this->key = $key;
19
		$this->value = Config::getInstance()->get( $key );
20
	}
21
22
	/**
23
	 * @param string[] $args
24
	 * @return self
25
	 */
26
	public function params( $args ) : self {
27
		$this->value = strtr( $this->value, $args );
28
		return $this;
29
	}
30
31
	/**
32
	 * @return string
33
	 */
34
	public function text() : string {
35
		$this->parsePlurals();
36
		return $this->value;
37
	}
38
39
	/**
40
	 * Replace {{$plur|<amount>|sing|plur}}
41
	 */
42
	protected function parsePlurals() {
43
		$this->value = preg_replace_callback(
44
			'!\{\{$plur|(?P<amount>\d+)|(?P<sing>[^}|]+)|(?P<plur>[^|}]+)}}!',
45
			function ( $matches ) {
46
				return intval( $matches['amount'] ) > 1 ? trim( $matches['plur'] ) : trim( $matches['sing'] );
47
			},
48
			$this->value
49
		);
50
	}
51
}
52