|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Validation\Guild; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
|
6
|
|
|
use Siak\Tontine\Service\LocaleService; |
|
7
|
|
|
use Siak\Tontine\Validation\AbstractValidator; |
|
8
|
|
|
use Siak\Tontine\Validation\Traits\ValidationTrait; |
|
9
|
|
|
use Siak\Tontine\Validation\ValidationException; |
|
10
|
|
|
|
|
11
|
|
|
use function trans; |
|
12
|
|
|
|
|
13
|
|
|
class PoolValidator extends AbstractValidator |
|
14
|
|
|
{ |
|
15
|
|
|
use ValidationTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param LocaleService $localeService |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(private LocaleService $localeService) |
|
21
|
|
|
{} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array<string> |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function amountFields(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return ['amount']; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $values |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function validateItem(array $values): array |
|
37
|
|
|
{ |
|
38
|
|
|
$validator = Validator::make($this->values($values), [ |
|
39
|
|
|
'title' => 'required|string|min:5', |
|
40
|
|
|
'amount' => $this->amountRule(), |
|
41
|
|
|
'notes' => 'nullable|string', |
|
42
|
|
|
'properties' => 'required|array:deposit,remit', |
|
43
|
|
|
'properties.deposit' => 'required|array:fixed,lendable', |
|
44
|
|
|
'properties.deposit.fixed' => 'required|boolean', |
|
45
|
|
|
'properties.deposit.lendable' => 'required|boolean', |
|
46
|
|
|
'properties.remit' => 'required|array:planned,auction', |
|
47
|
|
|
'properties.remit.planned' => 'required|boolean', |
|
48
|
|
|
'properties.remit.auction' => 'required|boolean', |
|
49
|
|
|
]); |
|
50
|
|
|
$validator->after(function($validator) use($values) { |
|
51
|
|
|
// The amount must be greater than 0 when the deposit property is set to fixed. |
|
52
|
|
|
if($values['properties']['deposit']['fixed'] && (float)$values['amount'] <= 0) |
|
53
|
|
|
{ |
|
54
|
|
|
$validator->errors() |
|
55
|
|
|
->add('principal', trans('validation.gt.numeric', [ |
|
56
|
|
|
'attribute' => 'amount', |
|
57
|
|
|
'value' => 0, |
|
58
|
|
|
])); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Enforce rules on properties values. |
|
62
|
|
|
if(!$values['properties']['deposit']['fixed']) |
|
63
|
|
|
{ |
|
64
|
|
|
$values['properties']['deposit']['lendable'] = false; |
|
65
|
|
|
$values['properties']['remit']['planned'] = true; |
|
66
|
|
|
$values['properties']['remit']['auction'] = false; |
|
67
|
|
|
} |
|
68
|
|
|
}); |
|
69
|
|
|
if($validator->fails()) |
|
70
|
|
|
{ |
|
71
|
|
|
throw new ValidationException($validator); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$validated = $validator->validated(); |
|
75
|
|
|
$validated['amount'] = $this->localeService->convertMoneyToInt((float)$validated['amount']); |
|
76
|
|
|
return $validated; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|