|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Validation\Planning; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Illuminate\Validation\Validator as RealValidator; |
|
7
|
|
|
use Siak\Tontine\Service\Planning\SessionService; |
|
8
|
|
|
use Siak\Tontine\Validation\AbstractValidator; |
|
9
|
|
|
use Siak\Tontine\Validation\ValidationException; |
|
10
|
|
|
|
|
11
|
|
|
class PoolRoundValidator extends AbstractValidator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private array $values = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param SessionService $sessionService |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(private SessionService $sessionService) |
|
22
|
|
|
{} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $values |
|
26
|
|
|
* @param string $key |
|
27
|
|
|
* @param RealValidator $validator |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
private function getSession(array $values, string $key, RealValidator $validator): void |
|
32
|
|
|
{ |
|
33
|
|
|
if(!isset($values[$key])) |
|
34
|
|
|
{ |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
$this->values["{$key}_id"] = (int)$values[$key]; |
|
38
|
|
|
$session = $this->sessionService->getTontineSession($this->values["{$key}_id"]); |
|
39
|
|
|
if($session !== null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->values[$key] = $session; |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
$validator->errors()->add($key, trans("tontine.pool_round.errors.$key")); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $values |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function validateItem(array $values): array |
|
53
|
|
|
{ |
|
54
|
|
|
$validator = Validator::make($this->values($values), [ |
|
55
|
|
|
'start_session' => 'required_without:end_session|integer|min:1', |
|
56
|
|
|
'end_session' => 'required_without:start_session|integer|min:1', |
|
57
|
|
|
], [ |
|
58
|
|
|
'start_session' => trans('tontine.pool_round.errors.start_session'), |
|
59
|
|
|
'end_session' => trans('tontine.pool_round.errors.end_session'), |
|
60
|
|
|
]); |
|
61
|
|
|
$this->values = []; |
|
62
|
|
|
$validator->after(function($validator) use($values) { |
|
63
|
|
|
// No more check if there's already an error. |
|
64
|
|
|
if($validator->errors()->count() > 0) |
|
65
|
|
|
{ |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->getSession($values, 'start_session', $validator); |
|
70
|
|
|
$this->getSession($values, 'end_session', $validator); |
|
71
|
|
|
// Todo: check that the start session comes before the end session. |
|
72
|
|
|
// if($endSession->id === $startSession->id || $endSession->start_at <= $startSession->start_at) |
|
73
|
|
|
// { |
|
74
|
|
|
// $validator->errors()->add('end_session', trans('tontine.pool_round.errors.session_dates')); |
|
75
|
|
|
// } |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
if($validator->fails()) |
|
79
|
|
|
{ |
|
80
|
|
|
throw new ValidationException($validator); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->values; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|