1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Repositories; |
26
|
|
|
|
27
|
|
|
use Validator; |
28
|
|
|
use Pterodactyl\Models; |
29
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
30
|
|
|
|
31
|
|
|
class LocationRepository |
32
|
|
|
{ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
// |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new location on the system. |
40
|
|
|
* |
41
|
|
|
* @param array $data |
42
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
43
|
|
|
* @return \Pterodactyl\Models\Location |
44
|
|
|
*/ |
45
|
|
|
public function create(array $data) |
46
|
|
|
{ |
47
|
|
|
$validator = Validator::make($data, [ |
48
|
|
|
'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short', |
49
|
|
|
'long' => 'required|string|min:1|max:255', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
// Run validator, throw catchable and displayable exception if it fails. |
53
|
|
|
// Exception includes a JSON result of failed validation rules. |
54
|
|
|
if ($validator->fails()) { |
55
|
|
|
throw new DisplayValidationException($validator->errors()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$location = Models\Location::create([ |
59
|
|
|
'long' => $data['long'], |
60
|
|
|
'short' => $data['short'], |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
return $location; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Modifies a location based on the fields passed in $data. |
68
|
|
|
* @param int $id |
69
|
|
|
* @param array $data |
70
|
|
|
* @throws Pterodactyl\Exceptions\DisplayValidationException |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function edit($id, array $data) |
74
|
|
|
{ |
75
|
|
|
$location = Models\Location::findOrFail($id); |
76
|
|
|
|
77
|
|
|
$validator = Validator::make($data, [ |
78
|
|
|
'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short,' . $location->id, |
79
|
|
|
'long' => 'required|string|min:1|max:255', |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
// Run validator, throw catchable and displayable exception if it fails. |
83
|
|
|
// Exception includes a JSON result of failed validation rules. |
84
|
|
|
if ($validator->fails()) { |
85
|
|
|
throw new DisplayValidationException($validator->errors()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$location->fill($data); |
89
|
|
|
|
90
|
|
|
return $location->save(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|