1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests\Denomination; |
4
|
|
|
|
5
|
|
|
use App\Enum\DenominationType; |
6
|
|
|
use App\Models\Denomination; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Illuminate\Validation\Rule; |
9
|
|
|
|
10
|
|
|
class UpdateRequest extends AbstractRequest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritDoc} |
14
|
|
|
*/ |
15
|
1 |
|
public function rules() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
1 |
|
'key' => ['required', 'string', 'max:255', Rule::unique(Denomination::class)->ignoreModel($this->route('denomination'))], |
19
|
1 |
|
'name' => 'required|string|max:255', |
20
|
1 |
|
'value' => 'required|numeric|min:0', |
21
|
|
|
'type' => 'required|enum:' . DenominationType::class, |
22
|
1 |
|
'quantity_per_bundle' => 'required|numeric|min:0', |
23
|
1 |
|
'minimum_order_bundle' => 'required|numeric|min:0', |
24
|
1 |
|
'maximum_order_bundle' => 'required|numeric|gte:minimum_order_bundle', |
25
|
1 |
|
'minimum_order_quantity' => 'sometimes|nullable|numeric|min:0', |
26
|
1 |
|
'maximum_order_quantity' => 'sometimes|nullable|numeric|gte:minimum_order_quantity', |
27
|
1 |
|
'can_order_custom_quantity' => 'required|boolean', |
28
|
1 |
|
'is_visible' => 'required|boolean', |
29
|
1 |
|
'image' => 'sometimes|nullable|image', |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Update the image file from the incoming request. |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
* @return string|null |
38
|
|
|
*/ |
39
|
1 |
|
public function updateImage(string $key = 'image'): ?string |
40
|
|
|
{ |
41
|
1 |
|
if (!$this->hasFile($key)) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @var \App\Models\Denomination $model */ |
46
|
1 |
|
$model = $this->route('denomination'); |
47
|
|
|
|
48
|
1 |
|
if ($model->getRawOriginal('image') && $this->hasFile($key)) { |
49
|
1 |
|
Storage::delete(Denomination::IMAGE_PATH . '/' . $model->getRawOriginal('image')); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$file = $this->file($key); |
53
|
|
|
|
54
|
1 |
|
$file->storeAs( |
55
|
1 |
|
Denomination::IMAGE_PATH, |
56
|
1 |
|
$filename = ($this->input('type') . '-' . $this->input('value') . '.' . $file->getClientOriginalExtension()) |
57
|
|
|
); |
58
|
|
|
|
59
|
1 |
|
return $filename; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|