/** * Validate * * @param mixed $data Data * * @return void * @throws \XLite\Core\Validator\Exception */ public function validate($data) { // Check country if (!isset($data[static::FIELD_COUNTRY])) { if (!$this->isFieldAvailable(static::FIELD_COUNTRY)) { $data[static::FIELD_COUNTRY] = \XLite\Model\Address::getDefaultFieldValue('country')->getCode(); } if (empty($data[static::FIELD_COUNTRY])) { throw $this->throwError('Country is not defined'); } } $countryCodeValidator = new \XLite\Core\Validator\Pair\Simple(); $countryCodeValidator->setName(static::FIELD_COUNTRY); $countryCodeValidator->setValidator(new \XLite\Core\Validator\String\ObjectId\Country(true)); $countryCodeValidator->validate($data); // Check state if (isset($data[static::FIELD_STATE])) { $stateValidator = new \XLite\Core\Validator\Pair\Simple(); $stateValidator->setName(static::FIELD_STATE); $stateValidator->setValidator(new \XLite\Core\Validator\String\ObjectId\State(true)); $stateValidator->validate($data); } $data = $this->sanitize($data); if (empty($data['state']) && $data['country']->hasStates()) { throw $this->throwError('State is not defined'); } elseif ($data['state'] && $data['state']->getCountry()->getCode() != $data['country']->getCode()) { throw $this->throwError('Country has not specified state'); } }
/** * Validate * * @param mixed $data Data * * @return void * @throws \XLite\Core\Validator\Exception */ public function validate($data) { // Check country if (!isset($data['country'])) { throw $this->throwError('Country is not defined'); } $countryCodeValidator = new \XLite\Core\Validator\Pair\Simple(); $countryCodeValidator->setName('country'); $countryCodeValidator->setValidator(new \XLite\Core\Validator\String\ObjectId\Country(true)); $countryCodeValidator->validate($data); // Check custom state flag $customState = isset($data['is_custom_state']) ? (bool) $data['is_custom_state'] : false; // Check state if (!isset($data['state'])) { throw $this->throwError('State is not defined'); } $stateValidator = new \XLite\Core\Validator\Pair\Simple(); $stateValidator->setName('state'); $stateCellValidator = $customState ? new \XLite\Core\Validator\String(true) : new \XLite\Core\Validator\String\ObjectId\State(true); $stateValidator->setValidator($stateCellValidator); $stateValidator->validate($data); if (!$customState) { $data = $this->sanitize($data); if ($data['state']->getCountry()->getCode() != $data['country']->getCode()) { throw $this->throwError('Country has not specified state'); } } }