|
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 DataPosition implements \BristolSU\ControlDB\Contracts\Repositories\DataPosition |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get a data position by ID |
|
15
|
|
|
* |
|
16
|
|
|
* @param int $id |
|
|
|
|
|
|
17
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
17 |
|
public function getById(int $id): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
20
|
|
|
{ |
|
21
|
17 |
|
return \BristolSU\ControlDB\Models\DataPosition::findOrFail($id); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get a data position with the given attributes, including additional attributes. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $attributes |
|
|
|
|
|
|
28
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function getWhere($attributes = []): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
31
|
|
|
{ |
|
32
|
3 |
|
$positions = $this->getAllWhere($attributes); |
|
33
|
|
|
|
|
34
|
3 |
|
if($positions->count() > 0) { |
|
|
|
|
|
|
35
|
2 |
|
return $positions->first(); |
|
36
|
|
|
} |
|
37
|
1 |
|
throw (new ModelNotFoundException())->setModel(DataPosition::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Create a new data position |
|
42
|
|
|
* |
|
43
|
|
|
* @param string|null $name |
|
|
|
|
|
|
44
|
|
|
* @param string|null $description |
|
|
|
|
|
|
45
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function create(?string $name = null, ?string $description = null): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
48
|
|
|
{ |
|
49
|
5 |
|
return \BristolSU\ControlDB\Models\DataPosition::create([ |
|
|
|
|
|
|
50
|
5 |
|
'name' => $name, |
|
51
|
5 |
|
'description' => $description, |
|
52
|
|
|
]); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all data positions where the given attributes match, including additional attributes. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $attributes |
|
|
|
|
|
|
59
|
|
|
* @return Collection |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function getAllWhere($attributes = []): Collection |
|
62
|
|
|
{ |
|
63
|
6 |
|
$baseAttributes = $attributes; |
|
64
|
6 |
|
$additionalAttributes = []; |
|
65
|
6 |
|
foreach (\BristolSU\ControlDB\Models\DataPosition::getAdditionalAttributes() as $property) { |
|
66
|
2 |
|
if (array_key_exists($property, $baseAttributes)) { |
|
67
|
1 |
|
$additionalAttributes[$property] = $baseAttributes[$property]; |
|
68
|
1 |
|
unset($baseAttributes[$property]); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return \BristolSU\ControlDB\Models\DataPosition::where(function($query) use ($baseAttributes) { |
|
|
|
|
|
|
72
|
6 |
|
foreach($baseAttributes as $key => $value) { |
|
|
|
|
|
|
73
|
6 |
|
$query = $query->where($key, 'LIKE', '%' . $value . '%'); |
|
74
|
|
|
} |
|
75
|
6 |
|
return $query; |
|
76
|
|
|
})->get()->filter(function (\BristolSU\ControlDB\Models\DataPosition $dataPosition) use ($additionalAttributes) { |
|
|
|
|
|
|
77
|
5 |
|
foreach ($additionalAttributes as $additionalAttribute => $value) { |
|
78
|
1 |
|
if ($dataPosition->getAdditionalAttribute($additionalAttribute) !== $value) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
5 |
|
return true; |
|
83
|
6 |
|
})->values(); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Update a data position with the given attributes |
|
88
|
|
|
* |
|
89
|
|
|
* @param int $id |
|
|
|
|
|
|
90
|
|
|
* @param string|null $name Name of the position |
|
|
|
|
|
|
91
|
|
|
* @param string|null $description Description of the position |
|
92
|
|
|
* @return \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
|
|
|
|
|
93
|
|
|
*/ |
|
94
|
6 |
|
public function update(int $id, ?string $name = null, ?string $description = null): \BristolSU\ControlDB\Contracts\Models\DataPosition |
|
95
|
|
|
{ |
|
96
|
6 |
|
$dataPosition = $this->getById($id)->fill([ |
|
|
|
|
|
|
97
|
6 |
|
'name' => $name, 'description' => $description |
|
98
|
|
|
]); |
|
|
|
|
|
|
99
|
6 |
|
$dataPosition->save(); |
|
100
|
6 |
|
return $dataPosition; |
|
101
|
|
|
} |
|
102
|
|
|
} |