1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Addwiki\Mediawiki\Api\Client; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @since 0.1 |
9
|
|
|
* |
10
|
|
|
* @author Addshore |
11
|
|
|
* @author RobinR1 |
12
|
|
|
* @author Bene |
13
|
|
|
* |
14
|
|
|
* Represents a user that can log in to the api |
15
|
|
|
*/ |
16
|
|
|
class ApiUser { |
17
|
|
|
|
18
|
|
|
private string $password; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private string $username; |
21
|
|
|
|
22
|
|
|
private ?string $domain; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $username The username. |
26
|
|
|
* @param string $password The user's password. |
27
|
|
|
* @param string|null $domain The domain (for authentication systems that support domains). |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public function __construct( string $username, string $password, ?string $domain = null ) { |
32
|
|
|
$domainIsStringOrNull = ( is_string( $domain ) || $domain === null ); |
33
|
|
|
if ( !is_string( $username ) || !is_string( $password ) || !$domainIsStringOrNull ) { |
34
|
|
|
throw new InvalidArgumentException( 'Username, Password and Domain must all be strings' ); |
35
|
|
|
} |
36
|
|
|
if ( empty( $username ) || empty( $password ) ) { |
37
|
|
|
throw new InvalidArgumentException( 'Username and Password are not allowed to be empty' ); |
38
|
|
|
} |
39
|
|
|
if ( $domain !== null && empty( $domain ) ) { |
40
|
|
|
throw new InvalidArgumentException( 'Domain is not allowed to be an empty string' ); |
41
|
|
|
} |
42
|
|
|
$this->username = $username; |
43
|
|
|
$this->password = $password; |
44
|
|
|
$this->domain = $domain; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @since 0.1 |
49
|
|
|
*/ |
50
|
|
|
public function getUsername(): string { |
51
|
|
|
return $this->username; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 0.1 |
56
|
|
|
*/ |
57
|
|
|
public function getPassword(): string { |
58
|
|
|
return $this->password; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @since 0.1 |
63
|
|
|
*/ |
64
|
|
|
public function getDomain(): ?string { |
65
|
|
|
return $this->domain; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @since 0.1 |
70
|
|
|
* @param mixed $other Another ApiUser object to compare with. |
71
|
|
|
*/ |
72
|
|
|
public function equals( $other ): bool { |
73
|
|
|
return $other instanceof self |
74
|
|
|
&& $this->username === $other->getUsername() |
75
|
|
|
&& $this->password === $other->getPassword() |
76
|
|
|
&& $this->domain === $other->getDomain(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|