|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace BristolSU\ControlDB\Repositories; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
class DataRole implements \BristolSU\ControlDB\Contracts\Repositories\DataRole |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get a data role by ID |
|
15
|
|
|
* |
|
16
|
|
|
* @param int $id |
|
|
|
|
|
|
17
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataRole |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
18 |
|
public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\DataRole |
|
20
|
|
|
{ |
|
21
|
18 |
|
return \BristolSU\ControlDB\Models\DataRole::findOrFail($id); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get a data role with the given attributes, including additional attributes. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $attributes |
|
|
|
|
|
|
28
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataRole |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function getWhere($attributes = []): \BristolSU\ControlDB\Contracts\Models\DataRole |
|
31
|
|
|
{ |
|
32
|
3 |
|
$roles = $this->getAllWhere($attributes); |
|
33
|
|
|
|
|
34
|
3 |
|
if($roles->count() > 0) { |
|
|
|
|
|
|
35
|
2 |
|
return $roles->first(); |
|
36
|
|
|
} |
|
37
|
1 |
|
throw (new ModelNotFoundException())->setModel(DataRole::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Create a new data role |
|
42
|
|
|
* |
|
43
|
|
|
* @param string|null $roleName |
|
|
|
|
|
|
44
|
|
|
* @param string|null $email |
|
|
|
|
|
|
45
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataRole |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function create(?string $roleName = null, ?string $email = null): \BristolSU\ControlDB\Contracts\Models\DataRole |
|
48
|
|
|
{ |
|
49
|
5 |
|
return \BristolSU\ControlDB\Models\DataRole::create([ |
|
|
|
|
|
|
50
|
5 |
|
'role_name' => $roleName, |
|
51
|
5 |
|
'email' => $email, |
|
52
|
|
|
]); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all data roles where the given attributes match, including additional attributes. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $attributes |
|
|
|
|
|
|
59
|
|
|
* @return Collection |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function getAllWhere($attributes = []): Collection |
|
62
|
|
|
{ |
|
63
|
4 |
|
$baseAttributes = $attributes; |
|
64
|
4 |
|
$additionalAttributes = []; |
|
65
|
4 |
|
foreach (\BristolSU\ControlDB\Models\DataRole::getAdditionalAttributes() as $property) { |
|
66
|
2 |
|
if (array_key_exists($property, $baseAttributes)) { |
|
67
|
1 |
|
$additionalAttributes[$property] = $baseAttributes[$property]; |
|
68
|
2 |
|
unset($baseAttributes[$property]); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return \BristolSU\ControlDB\Models\DataRole::where(function($query) use ($baseAttributes) { |
|
|
|
|
|
|
72
|
4 |
|
foreach($baseAttributes as $key => $value) { |
|
|
|
|
|
|
73
|
4 |
|
$query = $query->orWhere($key, 'LIKE', '%' . $value . '%'); |
|
74
|
|
|
} |
|
75
|
4 |
|
return $query; |
|
76
|
|
|
})->get()->filter(function (\BristolSU\ControlDB\Models\DataRole $dataRole) use ($additionalAttributes) { |
|
|
|
|
|
|
77
|
3 |
|
foreach ($additionalAttributes as $additionalAttribute => $value) { |
|
78
|
1 |
|
if ($dataRole->getAdditionalAttribute($additionalAttribute) !== $value) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
3 |
|
return true; |
|
83
|
4 |
|
})->values(); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |