|
1
|
|
|
<?php namespace VojtaSvoboda\Reservations\Validators; |
|
2
|
|
|
|
|
3
|
|
|
use App; |
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
use Illuminate\Validation\Validator; |
|
6
|
|
|
use VojtaSvoboda\Reservations\Facades\ReservationsFacade; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Custom Reservations validator. |
|
10
|
|
|
* |
|
11
|
|
|
* @package VojtaSvoboda\Reservations\Validators |
|
12
|
|
|
*/ |
|
13
|
|
|
class ReservationsValidators extends Validator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Validate reservation. Called on date attribute. Validate date availability. |
|
17
|
|
|
* |
|
18
|
|
|
* Other available variables: $this->translator, $this->data, $this->rules a $this->messages. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $attribute Name of the validated field. |
|
21
|
|
|
* @param mixed $value Field value. |
|
22
|
|
|
* |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
|
|
public function validateReservation($attribute, $value) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($attribute === 'date') { |
|
28
|
|
|
$date = $this->getDateAsCarbon($value); |
|
29
|
|
|
$reservationId = isset($this->data['id']) ? $this->data['id'] : null; |
|
30
|
|
|
|
|
31
|
|
|
return $this->getFacade()->isDateAvailable($date, $reservationId); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Replace placeholder :reservation with custom text. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $message |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function replaceReservation($message) |
|
45
|
|
|
{ |
|
46
|
|
|
$date = $this->getDateAsCarbon($this->data['date']); |
|
47
|
|
|
|
|
48
|
|
|
return str_replace(':reservation', $date->format('d.m.Y H:i'), $message); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get date as Carbon instance. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $date |
|
55
|
|
|
* |
|
56
|
|
|
* @return Carbon |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getDateAsCarbon($date) |
|
59
|
|
|
{ |
|
60
|
|
|
return Carbon::createFromFormat('Y-m-d H:i:s', $date); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get Reservations facade. |
|
65
|
|
|
* |
|
66
|
|
|
* @return ReservationsFacade |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getFacade() |
|
69
|
|
|
{ |
|
70
|
|
|
return App::make('vojtasvoboda.reservations.facade'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|