You are here

public function UcAddressesAddressBook::compareAddress in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 class/UcAddressesAddressBook.class.php \UcAddressesAddressBook::compareAddress()

Checks if given address looks like an address already in the address book.

Ignores the case if the address to compare is included in this address book.

The common case to use this method is when you have a new address and you want to make sure an address that looks the same is not already in the address book.

@access public

Parameters

UcAddressesAddress $address: The address to compare with other addresses in the address book.

boolean $compareUnsaved: (optional) If the address should be compared with addresses that are not yet saved. Defaults to FALSE.

Return value

UcAddressesAddress in case a match is found. FALSE otherwise.

File

class/UcAddressesAddressBook.class.php, line 374
Contains the UcAddressesAddressBook class.

Class

UcAddressesAddressBook
The address book class

Code

public function compareAddress(UcAddressesAddress $address, $compareUnsaved = FALSE) {
  if (!$this->allLoaded) {
    $this
      ->loadAll();
  }
  foreach ($this->addresses as $addressBookAddress) {
    if (!$compareUnsaved && $addressBookAddress
      ->isNew()) {

      // Don't compare the addresses with unsaved addresses.
      continue;
    }
    if ($address === $addressBookAddress) {

      // We don't need to compare the address with itself.
      continue;
    }
    if ($address
      ->getId() === $addressBookAddress
      ->getId()) {

      // Somehow we ended up with two addresses with the same ID.
      // This can happen when address objects get serialized and unserialized.
      // Ideally, this case should be prevented, but I'm not sure how. We can't
      // reassign "$this" in __wakeup().
      // Anyway, in this case we need to skip the comparison too.
      continue;
    }
    if ($addressBookAddress
      ->compareAddress($address)) {

      // Found a match! No need to look further.
      return $addressBookAddress;
    }
  }
  return FALSE;
}