|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Validation\Planning; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Siak\Tontine\Service\Planning\SessionService; |
|
7
|
|
|
use Siak\Tontine\Validation\AbstractValidator; |
|
8
|
|
|
use Siak\Tontine\Validation\ValidationException; |
|
9
|
|
|
|
|
10
|
|
|
class PoolRoundValidator extends AbstractValidator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param SessionService $sessionService |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct(private SessionService $sessionService) |
|
16
|
|
|
{} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $values |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function validateItem(array $values): array |
|
24
|
|
|
{ |
|
25
|
|
|
$validator = Validator::make($this->values($values), [ |
|
26
|
|
|
'start_session' => 'required|integer|min:1', |
|
27
|
|
|
'end_session' => 'required|integer|min:1', |
|
28
|
|
|
], [ |
|
29
|
|
|
'start_session' => trans('tontine.pool.errors.start_session'), |
|
30
|
|
|
'end_session' => trans('tontine.pool.errors.end_session'), |
|
31
|
|
|
]); |
|
32
|
|
|
$validator->after(function($validator) use($values) { |
|
33
|
|
|
// No more check if there's already an error. |
|
34
|
|
|
if($validator->errors()->count() > 0) |
|
35
|
|
|
{ |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$startSession = $this->sessionService->getTontineSession((int)$values['start_session']); |
|
40
|
|
|
if(!$startSession) |
|
41
|
|
|
{ |
|
42
|
|
|
$validator->errors()->add('start_session', trans('tontine.pool.errors.start_session')); |
|
43
|
|
|
} |
|
44
|
|
|
$endSession = $this->sessionService->getTontineSession((int)$values['end_session']); |
|
45
|
|
|
if(!$endSession) |
|
46
|
|
|
{ |
|
47
|
|
|
$validator->errors()->add('end_session', trans('tontine.pool.errors.end_session')); |
|
48
|
|
|
} |
|
49
|
|
|
if($endSession->id === $startSession->id || $endSession->start_at <= $startSession->start_at) |
|
50
|
|
|
{ |
|
51
|
|
|
$validator->errors()->add('end_session', trans('tontine.pool.errors.session_dates')); |
|
52
|
|
|
} |
|
53
|
|
|
}); |
|
54
|
|
|
if($validator->fails()) |
|
55
|
|
|
{ |
|
56
|
|
|
throw new ValidationException($validator); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'start_session_id' => (int)$values['start_session'], |
|
61
|
|
|
'end_session_id' => (int)$values['end_session'], |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|