mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-30 02:16:47 -04:00
79 lines
1.9 KiB
PHP
79 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Subscription\Rules;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
use Modules\ShippingZone\Repositories\ShippingZoneRepository;
|
|
|
|
class ZipCodeDeliverability implements Rule
|
|
{
|
|
private $shippingZoneRepository;
|
|
|
|
/**
|
|
* Create a new rule instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
if (isModuleActive('ShippingZone')) {
|
|
$this->shippingZoneRepository = new ShippingZoneRepository();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function passes($attribute, $value)
|
|
{
|
|
if ($this->shippingZoneRepository) {
|
|
$zones = $this->shippingZoneRepository->getActiveAll();
|
|
|
|
foreach ($zones as $zone) {
|
|
if ($zone->deliverability == 'select') {
|
|
$areas = $zone->areas;
|
|
foreach ($areas as $area) {
|
|
if (trim($value) == trim($area->area)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
$available_zones = [];
|
|
$zones = $this->shippingZoneRepository->getActiveAll();
|
|
|
|
foreach ($zones as $zone) {
|
|
if ($zone->deliverability == 'select') {
|
|
$areas = $zone->areas;
|
|
foreach ($areas as $area) {
|
|
$available_zones[] = $area->area;
|
|
}
|
|
}
|
|
}
|
|
|
|
$available_zones = implode(', ', $available_zones);
|
|
|
|
return 'You must be within the Geological delivery zones '.$available_zones;
|
|
}
|
|
}
|