¨4.0.1¨
This commit is contained in:
@@ -3,11 +3,41 @@
|
||||
namespace Modules\Cart\Http\Requests;
|
||||
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Option\Entities\Option;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Core\Http\Requests\Request;
|
||||
use Modules\Product\Entities\ProductVariant;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class StoreCartItemRequest extends Request
|
||||
{
|
||||
|
||||
protected Product|null $product;
|
||||
protected ProductVariant|null $variant;
|
||||
protected Product|ProductVariant $item;
|
||||
|
||||
|
||||
public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
|
||||
{
|
||||
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
|
||||
$this->product = $this->getProduct();
|
||||
$this->variant = $this->getVariant();
|
||||
|
||||
$this->item = $this->variant ?? $this->product;
|
||||
}
|
||||
|
||||
|
||||
public function authorize()
|
||||
{
|
||||
if ($this->item->is_active) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
@@ -15,61 +45,11 @@ class StoreCartItemRequest extends Request
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$product = Product::with('options')
|
||||
->select('id', 'manage_stock', 'qty')
|
||||
->findOrFail($this->product_id);
|
||||
|
||||
return array_merge([
|
||||
'qty' => ['required', 'numeric', $this->maxQty($product)],
|
||||
], $this->getOptionsRules($product->options));
|
||||
'qty' => ['required', 'numeric', $this->maxQty($this->item)],
|
||||
], $this->getOptionsRules($this->product->options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the max qty rule for the given product.
|
||||
*
|
||||
* @param \Modules\Product\Entities\Product $product
|
||||
* @return string|null
|
||||
*/
|
||||
private function maxQty($product)
|
||||
{
|
||||
if ($product->manage_stock) {
|
||||
return "max:{$product->qty}";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rules for the given options.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Collection $options
|
||||
* @return array
|
||||
*/
|
||||
private function getOptionsRules($options)
|
||||
{
|
||||
return $options->flatMap(function ($option) {
|
||||
return ["options.{$option->id}" => $this->getOptionRules($option)];
|
||||
})->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rules for the given option.
|
||||
*
|
||||
* @param \Modules\Option\Entities\Option $option
|
||||
* @return array
|
||||
*/
|
||||
private function getOptionRules($option)
|
||||
{
|
||||
$rules = [];
|
||||
|
||||
if ($option->is_required) {
|
||||
$rules[] = 'required';
|
||||
}
|
||||
|
||||
if (in_array($option->type, ['dropdown', 'radio'])) {
|
||||
$rules[] = Rule::in($option->values->map->id->all());
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data to be validated from the request.
|
||||
@@ -86,6 +66,7 @@ class StoreCartItemRequest extends Request
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*
|
||||
@@ -98,4 +79,91 @@ class StoreCartItemRequest extends Request
|
||||
'options.*.in' => trans('cart::validation.the_selected_option_is_invalid'),
|
||||
], parent::messages());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'qty' => (int)$this->qty,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
private function getProduct()
|
||||
{
|
||||
$product_id = request()->input('product_id');
|
||||
|
||||
return Product::with('options')
|
||||
->select('id', 'manage_stock', 'qty', 'is_active')
|
||||
->find($product_id);
|
||||
}
|
||||
|
||||
|
||||
private function getVariant()
|
||||
{
|
||||
$variant_id = request()->input('variant_id');
|
||||
|
||||
if ($variant_id) {
|
||||
return ProductVariant::select('id', 'manage_stock', 'qty', 'is_active')
|
||||
->find($variant_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the max qty rule for the given product.
|
||||
*
|
||||
* @param Product|ProductVariant $item
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function maxQty(Product|ProductVariant $item)
|
||||
{
|
||||
if ($item->manage_stock) {
|
||||
return "max:{$item->qty}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get rules for the given options.
|
||||
*
|
||||
* @param Collection $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getOptionsRules($options)
|
||||
{
|
||||
return $options->flatMap(function ($option) {
|
||||
return ["options.{$option->id}" => $this->getOptionRules($option)];
|
||||
})->all();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get rules for the given option.
|
||||
*
|
||||
* @param Option $option
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getOptionRules($option)
|
||||
{
|
||||
$rules = [];
|
||||
|
||||
if ($option->is_required) {
|
||||
$rules[] = 'required';
|
||||
}
|
||||
|
||||
if (in_array($option->type, ['dropdown', 'radio'])) {
|
||||
$rules[] = Rule::in($option->values->map->id->all());
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user