Total Complexity | 8 |
Total Lines | 62 |
Duplicated Lines | 0 % |
Coverage | 88.24% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class MultipleRoles implements IteratorAggregate, RoleInterface, Stringable |
||
18 | { |
||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | private array $roles = []; |
||
23 | |||
24 | /** |
||
25 | * @param string[] $roles |
||
26 | */ |
||
27 | 5 | public function __construct(array $roles = []) |
|
28 | { |
||
29 | 5 | foreach ($roles as $role) { |
|
30 | 4 | $this->addRole($role); |
|
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Add a role to the list. |
||
36 | */ |
||
37 | 4 | public function addRole(string $role): void |
|
38 | { |
||
39 | 4 | $this->roles[] = $role; |
|
40 | |||
41 | 4 | $this->roles = array_unique($this->roles); |
|
42 | 4 | sort($this->roles); |
|
43 | } |
||
44 | |||
45 | 1 | public function getRoleId(): never |
|
46 | { |
||
47 | 1 | throw new Exception('This should never be called. If it is, then it means this class is not used correctly'); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Return the role list. |
||
52 | * |
||
53 | * @return string[] |
||
54 | */ |
||
55 | 4 | public function getRoles(): array |
|
56 | { |
||
57 | 4 | return $this->roles; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Return whether at least one of the given role is included in the list. |
||
62 | */ |
||
63 | 1 | public function has(string ...$roles): bool |
|
64 | { |
||
65 | 1 | return (bool) array_intersect($this->roles, $roles); |
|
66 | } |
||
67 | |||
68 | 2 | public function __toString(): string |
|
69 | { |
||
70 | 2 | return '[' . implode(', ', $this->roles) . ']'; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return Traversable<int, string> |
||
75 | */ |
||
76 | public function getIterator(): Traversable |
||
79 | } |
||
80 | } |
||
81 |