1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Validation\Planning; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
6
|
|
|
use Siak\Tontine\Service\TenantService; |
7
|
|
|
use Siak\Tontine\Validation\AbstractValidator; |
8
|
|
|
use Siak\Tontine\Validation\ValidationException; |
9
|
|
|
|
10
|
|
|
class SessionValidator extends AbstractValidator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param TenantService $tenantService |
14
|
|
|
*/ |
15
|
|
|
public function __construct(protected TenantService $tenantService) |
16
|
|
|
{} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Check duplicates in session date |
20
|
|
|
* |
21
|
|
|
* @param string $startAt |
22
|
|
|
* @param int $sessionId |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
private function sessionDateExists(string $startAt, int $sessionId): bool |
27
|
|
|
{ |
28
|
|
|
return $this->tenantService->tontine()->sessions() |
29
|
|
|
->where('sessions.id', '!=', $sessionId) |
30
|
|
|
->whereDate('sessions.start_at', $startAt) |
31
|
|
|
->first() !== null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $values |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function validateItem(array $values): array |
40
|
|
|
{ |
41
|
|
|
$validator = Validator::make($this->values($values), [ |
42
|
|
|
'title' => 'required|string|min:1', |
43
|
|
|
'date' => 'required|date_format:Y-m-d', |
44
|
|
|
'start' => 'required|date_format:H:i', |
45
|
|
|
'end' => 'required|date_format:H:i', |
46
|
|
|
'host_id' => 'integer|min:0', |
47
|
|
|
]); |
48
|
|
|
$validator->after(function($validator) use($values) { |
49
|
|
|
if($this->sessionDateExists($values['date'], $values['id'] ?? 0)) |
50
|
|
|
{ |
51
|
|
|
$validator->errors()->add('date', trans('tontine.session.errors.date_dup')); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
if($validator->fails()) |
55
|
|
|
{ |
56
|
|
|
throw new ValidationException($validator); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$validated = $validator->validated(); |
60
|
|
|
if(!$validated['host_id']) |
61
|
|
|
{ |
62
|
|
|
$validated['host_id'] = null; |
63
|
|
|
} |
64
|
|
|
return $validated; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $values |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function validateVenue(array $values): array |
73
|
|
|
{ |
74
|
|
|
$validator = Validator::make($this->values($values), [ |
75
|
|
|
'venue' => 'nullable|string', |
76
|
|
|
'notes' => 'nullable|string', |
77
|
|
|
]); |
78
|
|
|
if($validator->fails()) |
79
|
|
|
{ |
80
|
|
|
throw new ValidationException($validator); |
81
|
|
|
} |
82
|
|
|
return $validator->validated(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|