/** * Validate address attribute values * * @return bool|array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validate() { $errors = []; if (!\Zend_Validate::is($this->getFirstname(), 'NotEmpty')) { $errors[] = __('Please enter the first name.'); } if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) { $errors[] = __('Please enter the last name.'); } if (!\Zend_Validate::is($this->getStreetLine(1), 'NotEmpty')) { $errors[] = __('Please enter the street.'); } if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) { $errors[] = __('Please enter the city.'); } if (!\Zend_Validate::is($this->getTelephone(), 'NotEmpty')) { $errors[] = __('Please enter the phone number.'); } $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip(); if (!in_array($this->getCountryId(), $_havingOptionalZip) && !\Zend_Validate::is($this->getPostcode(), 'NotEmpty')) { $errors[] = __('Please enter the zip/postal code.'); } if (!\Zend_Validate::is($this->getCountryId(), 'NotEmpty')) { $errors[] = __('Please enter the country.'); } if ($this->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is($this->getRegionId(), 'NotEmpty') && $this->_directoryData->isRegionRequired($this->getCountryId())) { $errors[] = __('Please enter the state/province.'); } if (empty($errors) || $this->getShouldIgnoreValidation()) { return true; } return $errors; }
/** * Convert collection items to select options array * * @param string|boolean $emptyLabel * @return array */ public function toOptionArray($emptyLabel = ' ') { $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']); $sort = []; foreach ($options as $data) { $name = (string) $this->_localeLists->getCountryTranslation($data['value']); if (!empty($name)) { $sort[$name] = $data['value']; } } $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale()); foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) { $name = array_search($foregroundCountry, $sort); unset($sort[$name]); $sort = [$name => $foregroundCountry] + $sort; } $options = []; foreach ($sort as $label => $value) { $option = ['value' => $value, 'label' => $label]; if ($this->helperData->isRegionRequired($value)) { $option['is_region_required'] = true; } $options[] = $option; } if (count($options) > 0 && $emptyLabel !== false) { array_unshift($options, ['value' => '', 'label' => $emptyLabel]); } return $options; }
/** * Validate Customer Addresses attribute values. * * @param CustomerAddressModel $customerAddressModel the model to validate * @return InputException * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function _validate(CustomerAddressModel $customerAddressModel) { $exception = new InputException(); if ($customerAddressModel->getShouldIgnoreValidation()) { return $exception; } if (!\Zend_Validate::is($customerAddressModel->getFirstname(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname'])); } if (!\Zend_Validate::is($customerAddressModel->getLastname(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'lastname'])); } if (!\Zend_Validate::is($customerAddressModel->getStreetLine(1), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'street'])); } if (!\Zend_Validate::is($customerAddressModel->getCity(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'city'])); } if (!\Zend_Validate::is($customerAddressModel->getTelephone(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'telephone'])); } $havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip(); if (!in_array($customerAddressModel->getCountryId(), $havingOptionalZip) && !\Zend_Validate::is($customerAddressModel->getPostcode(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode'])); } if (!\Zend_Validate::is($customerAddressModel->getCountryId(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'countryId'])); } if ($customerAddressModel->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is($customerAddressModel->getRegionId(), 'NotEmpty') && $this->directoryData->isRegionRequired($customerAddressModel->getCountryId())) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'regionId'])); } return $exception; }
/** * Update region data * * @param array $attributeValues * @return void * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function updateRegionData(&$attributeValues) { if ($this->helperData->isRegionRequired($attributeValues['country_id'])) { $newRegion = $this->regionFactory->create()->load($attributeValues['region_id']); $attributeValues['region_code'] = $newRegion->getCode(); $attributeValues['region'] = $newRegion->getDefaultName(); } else { $attributeValues['region_id'] = null; } $regionData = [RegionInterface::REGION_ID => !empty($attributeValues['region_id']) ? $attributeValues['region_id'] : null, RegionInterface::REGION => !empty($attributeValues['region']) ? $attributeValues['region'] : null, RegionInterface::REGION_CODE => !empty($attributeValues['region_code']) ? $attributeValues['region_code'] : null]; $region = $this->regionDataFactory->create(); $this->dataObjectHelper->populateWithArray($region, $regionData, '\\Magento\\Customer\\Api\\Data\\RegionInterface'); $attributeValues['region'] = $region; }
/** * Checks if state for current country id is required * * @param string $countryId * @return bool */ protected function isStateRequired($countryId) { $country = $this->countryFactory->create()->load($countryId); return $this->directoryHelper->isRegionRequired($countryId) && $country->getRegionCollection()->getSize(); }