/** * @param AbstractAddress|BAPRegion $source * * @return array magento correspondent fields data * @throws \InvalidArgumentException */ public function toMagentoData($source) { $data = ['region' => null, 'region_id' => null]; if (!$source instanceof AbstractAddress && !$source instanceof BAPRegion) { throw new \InvalidArgumentException('Source should be instance of AbstractAddress or Region'); } if ($source instanceof AbstractAddress && !$source->getRegion()) { $data['region'] = $source->getRegionText(); } elseif ($source instanceof AbstractAddress) { $source = $source->getRegion(); } if ($source instanceof BAPRegion) { $magentoRegion = $this->tryGetMRByCode($source->getCombinedCode()); if (!$magentoRegion) { $data['region'] = $source->getName(); } else { $data['region_id'] = $magentoRegion->getRegionId(); } } return $data; }
/** * @param AbstractAddress $address * @param string $countryCode * @param int|string|null $mageRegionId * @throws InvalidItemException */ public function updateRegionByMagentoRegionId(AbstractAddress $address, $countryCode, $mageRegionId = null) { if (!empty($mageRegionId) && empty($this->mageRegionsCache[$mageRegionId]) && is_numeric($mageRegionId)) { $this->mageRegionsCache[$mageRegionId] = $this->findRegionByRegionId($mageRegionId); } if (!empty($this->mageRegionsCache[$mageRegionId])) { /** @var Region $mageRegion */ $mageRegion = $this->mageRegionsCache[$mageRegionId]; $combinedCode = $mageRegion->getCombinedCode(); $regionCode = $mageRegion->getCode(); if (!array_key_exists($combinedCode, $this->regionsCache)) { $this->regionsCache[$combinedCode] = $this->loadRegionByCode($combinedCode, $countryCode, $regionCode); } /** * no region found in system db for corresponding magento region, use region text */ if (empty($this->regionsCache[$combinedCode])) { $address->setRegion(null); $address->setRegionText($mageRegion->getName()); } else { $this->regionsCache[$combinedCode] = $this->doctrineHelper->merge($this->regionsCache[$combinedCode]); $address->setRegion($this->regionsCache[$combinedCode]); $address->setRegionText(null); } } elseif ($address->getRegionText() || $address->getCountry()) { $address->setRegion(null); // unable to find corresponding region and region text is empty, // it's correct case for UK addresses, if country present } else { throw new InvalidItemException('Unable to handle region for address', [$address]); } }