Passed
Push — master ( 89d164...fd4e50 )
by Daimona
01:38
created

User::setInfo()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki;
4
5
use BotRiconferme\Wiki\Page\Page;
6
use BotRiconferme\Wiki\Page\PageBotList;
7
8
/**
9
 * Class representing a single user. NOTE: this can only represent users stored in the JSON list
10
 */
11
class User extends Element {
12
	/** @var string */
13
	private $name;
14
	/** @var string[]|null Info contained in the JSON page */
15
	private $info;
16
17
	/**
18
	 * @param string $name
19
	 * @param Wiki $wiki
20
	 */
21
	public function __construct( string $name, Wiki $wiki ) {
22
		parent::__construct( $wiki );
23
		$this->name = $name;
24
	}
25
26
	/**
27
	 * @return string
28
	 */
29
	public function getName() : string {
30
		return $this->name;
31
	}
32
33
	/**
34
	 * Get a list of groups this user belongs to
35
	 *
36
	 * @return string[]
37
	 */
38
	public function getGroups() : array {
39
		return array_diff( array_keys( $this->getUserInfo() ), PageBotList::NON_GROUP_KEYS );
40
	}
41
42
	/**
43
	 * Get some info about this user, including flag dates.
44
	 *
45
	 * @return string[]
46
	 */
47
	public function getUserInfo() : array {
48
		if ( $this->info === null ) {
49
			$usersList = PageBotList::get( $this->wiki )->getAdminsList();
50
			$this->info = $usersList[ $this->name ]->getUserInfo();
51
		}
52
		return $this->info;
53
	}
54
55
	/**
56
	 * @param array|null $info
57
	 */
58
	public function setInfo( ?array $info ) : void {
59
		$this->info = $info;
60
	}
61
62
	/**
63
	 * Whether the user is in the given group
64
	 *
65
	 * @param string $groupName
66
	 * @return bool
67
	 */
68
	public function inGroup( string $groupName ) : bool {
69
		return in_array( $groupName, $this->getGroups() );
70
	}
71
72
	/**
73
	 * Returns a regex for matching the name of this user
74
	 *
75
	 * @inheritDoc
76
	 */
77
	public function getRegex() : string {
78
		$bits = $this->getAliases();
79
		$bits[] = $this->name;
80
		$regexify = function ( $el ) {
81
			return str_replace( ' ', '[ _]', preg_quote( $el ) );
82
		};
83
		return '(?:' . implode( '|', array_map( $regexify, $bits ) ) . ')';
84
	}
85
86
	/**
87
	 * Get a list of aliases for this user.
88
	 *
89
	 * @return string[]
90
	 */
91
	public function getAliases() : array {
92
		return $this->getUserInfo()['aliases'] ?? [];
93
	}
94
95
	/**
96
	 * @return Page
97
	 */
98
	public function getTalkPage() : Page {
99
		return new Page( "User talk:{$this->name}", $this->wiki );
100
	}
101
102
	/**
103
	 * @return string
104
	 */
105
	public function __toString() {
106
		return $this->name;
107
	}
108
}
109