1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Domains\Roles; |
4
|
|
|
|
5
|
|
|
use Omatech\Mage\Core\Domains\Roles\Contracts\RoleInterface; |
6
|
|
|
use Omatech\Mage\Core\Domains\Roles\Exceptions\RoleIsNotSavedException; |
7
|
|
|
|
8
|
|
|
class RoleModel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param array $currentRoles |
12
|
|
|
* @param RoleInterface $assignRole |
13
|
|
|
* @return array |
14
|
|
|
* @throws RoleIsNotSavedException |
15
|
|
|
*/ |
16
|
7 |
|
public function assignRole(array $currentRoles, RoleInterface $assignRole): array |
17
|
|
|
{ |
18
|
7 |
|
if (null === $assignRole->getId()) { |
19
|
1 |
|
throw new RoleIsNotSavedException(); |
20
|
|
|
} |
21
|
|
|
|
22
|
6 |
|
if (! in_array($assignRole, $currentRoles, true)) { |
23
|
6 |
|
$currentRoles[] = $assignRole; |
24
|
|
|
} |
25
|
|
|
|
26
|
6 |
|
return $currentRoles; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $currentRoles |
31
|
|
|
* @param array $assignRoles |
32
|
|
|
* @return array |
33
|
|
|
* @throws RoleIsNotSavedException |
34
|
|
|
*/ |
35
|
7 |
|
public function assignRoles(array $currentRoles, array $assignRoles): array |
36
|
|
|
{ |
37
|
7 |
|
foreach ($assignRoles as $role) { |
38
|
6 |
|
if ($role instanceof RoleInterface) { |
39
|
6 |
|
$currentRoles = $this->assignRole($currentRoles, $role); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
return $currentRoles; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $currentRoles |
48
|
|
|
* @param RoleInterface $deleteRole |
49
|
|
|
* @return array |
50
|
|
|
* @throws RoleIsNotSavedException |
51
|
|
|
*/ |
52
|
3 |
|
public function removeRole(array $currentRoles, RoleInterface $deleteRole): array |
53
|
|
|
{ |
54
|
3 |
|
if (null === $deleteRole->getId()) { |
55
|
1 |
|
throw new RoleIsNotSavedException(); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
$currentRoles = array_values(array_filter( |
59
|
2 |
|
$currentRoles, |
60
|
|
|
static function ($currentRole) use ($deleteRole) { |
61
|
2 |
|
return $currentRole !== $deleteRole; |
62
|
2 |
|
} |
63
|
|
|
)); |
64
|
|
|
|
65
|
2 |
|
return $currentRoles; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $currentRoles |
70
|
|
|
* @param array $deleteRoles |
71
|
|
|
* @return array |
72
|
|
|
* @throws RoleIsNotSavedException |
73
|
|
|
*/ |
74
|
2 |
|
public function removeRoles(array $currentRoles, array $deleteRoles): array |
75
|
|
|
{ |
76
|
2 |
|
foreach ($deleteRoles as $role) { |
77
|
2 |
|
if ($role instanceof RoleInterface) { |
78
|
2 |
|
$currentRoles = $this->removeRole($currentRoles, $role); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $currentRoles; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|