/** * Adds address to address book. * * @param UcAddressesAddress $address * (optional) An instance of UcAddressesAddress to add. * Defaults to a new instance of UcAddressesAddress. * * @access public * @return UcAddressesAddress * The instance of UcAddressesAddress that was added. * @throws UcAddressesInvalidParameterException * @throws UcAddressesNameCollisionException */ public function addAddress(UcAddressesAddress $address = NULL) { // If we add an address, then we'll probably save it, which // requires loading all addresses for error checking. if ($this->performanceHint == self::PERF_HINT_LOAD_ONE) { $this->performanceHint = self::PERF_HINT_LOAD_ALL; } if ($address) { // In case of a new address with an address name, // load other addresses to do a name check comparison. if ($address->isNew() && $address->getName() && !$this->allLoaded) { $this->loadAll(); } // Check if address is already in addressbook. foreach ($this->addresses as $aid => $addressBookAddress) { if ($address === $addressBookAddress) { throw new UcAddressesInvalidParameterException(t('Tried to add an address already in the address book')); } if ($address->getName() && $address->getName() == $addressBookAddress->getName()) { throw new UcAddressesNameCollisionException(t('Tried to add an address with a name matching that of an address already in the address book')); } } // Check if address belongs to this address book. if ($address->getAddressBook() !== $this && $address->isOwned()) { throw new UcAddressesInvalidParameterException(t('Tried to add an address already in an other address book')); } } if (!$address) { $address = new UcAddressesAddress($this); } $this->addresses[$address->getId()] = $address; if ($address->isDefault('shipping')) { $this->defaultAddresses['shipping'] = $address; } if ($address->isDefault('billing')) { $this->defaultAddresses['billing'] = $address; } // Make sure this becomes one of our addresses. if ($address->getAddressBook() !== $this) { $address->privChangeAddressBook($this); } return $address; }