sarga/packages/Webkul/Checkout/src/Http/Requests/CustomerAddressForm.php

112 lines
3.2 KiB
PHP
Raw Normal View History

2018-09-25 05:22:22 +00:00
<?php
namespace Webkul\Checkout\Http\Requests;
2018-09-25 05:22:22 +00:00
use Illuminate\Foundation\Http\FormRequest;
class CustomerAddressForm extends FormRequest
{
2020-12-09 09:11:23 +00:00
/**
* Rules.
*
* @var array
2020-12-09 09:11:23 +00:00
*/
protected $rules = [];
2018-09-25 05:22:22 +00:00
/**
* Determine if the product is authorized to make this request.
*
* @return bool
*/
2020-12-09 09:11:23 +00:00
public function authorize(): bool
2018-09-25 05:22:22 +00:00
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
2020-12-09 09:11:23 +00:00
public function rules(): array
2018-09-25 05:22:22 +00:00
{
2020-12-09 09:11:23 +00:00
$customerAddressIds = $this->getCustomerAddressIds();
2019-02-14 15:50:10 +00:00
if (isset($this->get('billing')['address_id'])) {
2020-12-09 09:11:23 +00:00
$billingAddressRules = [
2019-02-14 15:50:10 +00:00
'billing.address_id' => ['required'],
];
2020-12-09 09:11:23 +00:00
if (! empty($customerAddressIds)) {
$billingAddressRules['billing.address_id'][] = "in:{$customerAddressIds}";
}
$this->mergeWithRules($billingAddressRules);
2019-02-14 15:50:10 +00:00
} else {
2020-12-09 09:11:23 +00:00
$this->mergeWithRules([
2019-02-14 15:50:10 +00:00
'billing.first_name' => ['required'],
2020-02-19 12:14:07 +00:00
'billing.last_name' => ['required'],
'billing.email' => ['required'],
'billing.address1' => ['required'],
'billing.city' => ['required'],
'billing.state' => ['required'],
'billing.postcode' => ['required'],
'billing.phone' => ['required'],
2020-03-04 06:32:53 +00:00
'billing.country' => ['required'],
2020-12-09 09:11:23 +00:00
]);
2019-02-14 15:50:10 +00:00
}
2018-09-25 05:22:22 +00:00
2020-12-09 09:11:23 +00:00
if (isset($this->get('billing')['use_for_shipping']) && ! $this->get('billing')['use_for_shipping']) {
2019-02-14 15:50:10 +00:00
if (isset($this->get('shipping')['address_id'])) {
2020-12-09 09:11:23 +00:00
$shippingAddressRules = [
2019-02-14 15:50:10 +00:00
'shipping.address_id' => ['required'],
2020-12-09 09:11:23 +00:00
];
if (! empty($customerAddressIds)) {
$shippingAddressRules['shipping.address_id'][] = "in:{$customerAddressIds}";
}
$this->mergeWithRules($shippingAddressRules);
2019-02-14 15:50:10 +00:00
} else {
2020-12-09 09:11:23 +00:00
$this->mergeWithRules([
2019-02-14 15:50:10 +00:00
'shipping.first_name' => ['required'],
2020-02-19 12:14:07 +00:00
'shipping.last_name' => ['required'],
'shipping.email' => ['required'],
'shipping.address1' => ['required'],
'shipping.city' => ['required'],
'shipping.state' => ['required'],
'shipping.postcode' => ['required'],
'shipping.phone' => ['required'],
2020-03-04 06:32:53 +00:00
'shipping.country' => ['required'],
2019-02-14 15:50:10 +00:00
]);
}
2018-09-25 05:22:22 +00:00
}
return $this->rules;
}
2020-12-09 09:11:23 +00:00
/**
* Merge additional rules.
*
* @return string
*/
private function mergeWithRules($additionalRules): void
{
$this->rules = array_merge($this->rules, $additionalRules);
}
/**
* If customer is placing order then fetching all address ids to check with the request ids.
*
* @return string
*/
private function getCustomerAddressIds(): string
{
if ($customer = auth()->guard()->user()) {
2020-12-09 09:11:23 +00:00
return $customer->addresses->pluck('id')->join(',');
}
return '';
}
2018-09-25 05:22:22 +00:00
}