-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpdateRentableRequest.php
99 lines (84 loc) · 3.46 KB
/
UpdateRentableRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace App\Http\Requests;
use App\Repositories\Interfaces\IRentableRepository;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRentableRequest extends FormRequest
{
protected $rentableRepo;
public function __construct(IRentableRepository $rentableRepo)
{
$this->rentableRepo = $rentableRepo;
}
/**
* Sanitize before rules()
*/
protected function sanitizeInput()
{
$input = $this->all();
$input['description'] = preg_replace("~[\p{M}]~uis", "", $this->input('description'));
$this->replace($input);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$todayDate = date('Y-m-d');
return [
'rentable_id' => 'required|integer|min:1|exists:App\Models\Rentable,id',
'adress' => 'required|string|min:3|max:150|regex:/^[a-zA-Z0-9_ .-]*$/', // Regex for Adress
'postal_code' => 'required|numeric|digits:4|min:1|max:9999',
'date_of_hire' => 'required|date_format:Y-m-d|after_or_equal:' . $todayDate,
'start_time' => 'required',
'end_time' => 'required',
'price' => 'required|numeric|min:0.01|max:1000|regex:/^[0-9]+(\.[0-9]{1,2})?$/', //Regex for Decimal with 2 decimal places
'bankaccount_nr' => 'required|string|regex:/^[A-Z]{2}(?:[ ]?[0-9]){14,20}$/', // Regex for IBAN numbers
'description' => 'required|string|max:150',
];
}
/**
* Validate request
*
* @return Illuminate\Foundation\Http\FormRequest::getValidatorInstance
*/
protected function getValidatorInstance()
{
$this->sanitizeInput();
return parent::getValidatorInstance()->after(function () {
// Get the current rentable
$rentable = $this->rentableRepo->getRentable($this->input('rentable_id'));
// Check if there are any leases on the current rentable
if (count($rentable->leases) > 0) {
$this->validator->errors()->add('rented', 'Another user has already rented your lease, you are no longer allowed to make any changes. If you wish to correct any wrong information please contact our helpfull support staff');
}
// convert to unix timestamps
$start_time = $this->input('start_time');
$end_time = $this->input('end_time');
$rentable_start_time = strtotime($start_time);
$rentable_end_time = strtotime($end_time);
// Check if start_time is before end_time
if ($rentable_start_time >= $rentable_end_time) {
$this->validator->errors()->add('end_time', 'End time cannot be before start time');
}
// Check if minimum rented time is greater then 30 minutes
if ($this->timeDiffInMinutes($rentable_start_time, $rentable_end_time) < 30) {
$this->validator->errors()->add('rented_time', 'At least 30 minutes have to be available for rent');
}
});
}
/**
* Calculates the time difference between two unix timestamps in minutes
*
* @param $firstTime
* @param $lastTime
*/
protected function timeDiffInMinutes($firstTime, $lastTime)
{
// perform subtraction to get the difference (in seconds) between times
$timeDiff = ($lastTime - $firstTime) / 60;
// return the difference
return $timeDiff;
}
}