Example #1
0
 /**
  * Make address Data Object out of an address model
  *
  * @param AbstractAddress $addressModel
  * @param int $defaultBillingId
  * @param int $defaultShippingId
  * @return Address
  */
 public function createAddressFromModel(AbstractAddress $addressModel, $defaultBillingId, $defaultShippingId)
 {
     $addressId = $addressModel->getId();
     $attributes = $this->_metadataService->getAllAddressAttributeMetadata();
     $addressData = array();
     foreach ($attributes as $attribute) {
         $code = $attribute->getAttributeCode();
         if (!is_null($addressModel->getData($code))) {
             $addressData[$code] = $addressModel->getData($code);
         }
     }
     $isDefaultBilling = $addressModel->getData('is_default_billing') === null && intval($addressId) ? $addressId === $defaultBillingId : $addressModel->getData('is_default_billing');
     $isDefaultShipping = $addressModel->getData('is_default_shipping') === null && intval($addressId) ? $addressId === $defaultShippingId : $addressModel->getData('is_default_shipping');
     $this->_addressBuilder->populateWithArray(array_merge($addressData, array(Address::KEY_STREET => $addressModel->getStreet(), Address::KEY_DEFAULT_BILLING => $isDefaultBilling, Address::KEY_DEFAULT_SHIPPING => $isDefaultShipping, Address::KEY_REGION => array(Region::KEY_REGION => $addressModel->getRegion(), Region::KEY_REGION_ID => $addressModel->getRegionId(), Region::KEY_REGION_CODE => $addressModel->getRegionCode()))));
     if ($addressId) {
         $this->_addressBuilder->setId($addressId);
     }
     if ($addressModel->getCustomerId() || $addressModel->getParentId()) {
         $customerId = $addressModel->getCustomerId() ?: $addressModel->getParentId();
         $this->_addressBuilder->setCustomerId($customerId);
     }
     $addressDataObject = $this->_addressBuilder->create();
     return $addressDataObject;
 }