53 lines
1.2 KiB
PHP
53 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Basket;
|
|
|
|
use App\Cqs\Actions\Basket\RemoveProductFromBasketInput;
|
|
use App\Cqs\Actions\Basket\RemoveQuantityFromBasketInput;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
class RemoveBasketRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function toRemoveFromBasketInput(): RemoveQuantityFromBasketInput | RemoveProductFromBasketInput
|
|
{
|
|
$qty = $this->integer('qty');
|
|
|
|
if ($qty >= 1) {
|
|
return new RemoveQuantityFromBasketInput(
|
|
$this->getProductId(),
|
|
$qty
|
|
);
|
|
}
|
|
|
|
return new RemoveProductFromBasketInput(
|
|
$this->getProductId()
|
|
);
|
|
}
|
|
|
|
public function getProductId(): ProductId
|
|
{
|
|
return new ProductId(
|
|
UuidHelper::fromString(
|
|
$this->string('rowId')->value()
|
|
)
|
|
);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'rowId' => ['required', 'string', 'uuid'],
|
|
'qty' => ['sometimes', 'integer', 'gt:0'],
|
|
];
|
|
}
|
|
}
|