Passed
Push — master ( 5cd30c...c20c86 )
by Daimona
01:42
created

User::getGroupsWithDates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
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
	 * Like getGroups(), but includes flag dates.
44
	 *
45
	 * @return string[] [ group => date ]
46
	 */
47
	public function getGroupsWithDates() : array {
48
		return array_intersect_key( $this->getUserInfo(), array_fill_keys( $this->getGroups(), 1 ) );
49
	}
50
51
	/**
52
	 * Get some info about this user, including flag dates.
53
	 *
54
	 * @return string[]
55
	 */
56
	public function getUserInfo() : array {
57
		if ( $this->info === null ) {
58
			$usersList = PageBotList::get( $this->wiki )->getAdminsList();
59
			$this->info = $usersList[ $this->name ]->getUserInfo();
60
		}
61
		return $this->info;
62
	}
63
64
	/**
65
	 * @param array|null $info
66
	 */
67
	public function setInfo( ?array $info ) : void {
68
		$this->info = $info;
69
	}
70
71
	/**
72
	 * Whether the user is in the given group
73
	 *
74
	 * @param string $groupName
75
	 * @return bool
76
	 */
77
	public function inGroup( string $groupName ) : bool {
78
		return in_array( $groupName, $this->getGroups() );
79
	}
80
81
	/**
82
	 * Returns a regex for matching the name of this user
83
	 *
84
	 * @inheritDoc
85
	 */
86
	public function getRegex() : string {
87
		$bits = $this->getAliases();
88
		$bits[] = $this->name;
89
		$regexify = function ( $el ) {
90
			return str_replace( ' ', '[ _]', preg_quote( $el ) );
91
		};
92
		return '(?:' . implode( '|', array_map( $regexify, $bits ) ) . ')';
93
	}
94
95
	/**
96
	 * Get a list of aliases for this user.
97
	 *
98
	 * @return string[]
99
	 */
100
	public function getAliases() : array {
101
		return $this->getUserInfo()['aliases'] ?? [];
102
	}
103
104
	/**
105
	 * @return Page
106
	 */
107
	public function getTalkPage() : Page {
108
		return new Page( "User talk:{$this->name}", $this->wiki );
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	public function __toString() {
115
		return $this->name;
116
	}
117
}
118