| Conditions | 5 |
| Paths | 2 |
| Total Lines | 41 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | } |
||
| 79 |