Passed
Push — master ( b3e686...761a52 )
by Daimona
01:54
created

User::getRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki;
4
5
use BotRiconferme\Wiki\Page\PageBotList;
6
7
/**
8
 * Class representing a single user.
9
 */
10
class User extends Element {
11
	/** @var string */
12
	private $name;
13
	/** @var string[] */
14
	private $groups;
15
16
	/**
17
	 * @param string $name
18
	 */
19
	public function __construct( string $name ) {
20
		$this->name = $name;
21
	}
22
23
	/**
24
	 * @return string
25
	 */
26
	public function getName() : string {
27
		return $this->name;
28
	}
29
30
	/**
31
	 * @return string[]
32
	 */
33
	public function getGroups() : array {
34
		if ( $this->groups === null ) {
35
			$usersList = PageBotList::get()->getAdminsList();
36
			$this->groups = array_keys( $usersList[ $this->name ] );
37
		}
38
		return $this->groups;
39
	}
40
41
	/**
42
	 * Whether the user is in the given group
43
	 *
44
	 * @param string $groupName
45
	 * @return bool
46
	 */
47
	public function inGroup( string $groupName ) : bool {
48
		return in_array( $groupName, $this->getGroups() );
49
	}
50
51
	/**
52
	 * Returns a regex for matching the name of this user
53
	 *
54
	 * @inheritDoc
55
	 */
56
	public function getRegex() : string {
57
		return str_replace( ' ', '[ _]', preg_quote( $this->name ) );
58
	}
59
60
	/**
61
	 * @return string
62
	 */
63
	public function __toString() {
64
		return $this->name;
65
	}
66
}
67