You are here

class Address in Ubercart 8.4

Defines an object to hold Ubercart mailing address information.

Hierarchy

Expanded class hierarchy of Address

16 files declare their use of Address
AddressForm.php in shipping/uc_fulfillment/src/Form/AddressForm.php
AddressPaneBase.php in uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php
AddressTest.php in uc_store/tests/src/Functional/AddressTest.php
AddressTestTrait.php in uc_store/tests/src/Traits/AddressTestTrait.php
Check.php in payment/uc_payment_pack/src/Plugin/Ubercart/PaymentMethod/Check.php

... See full list

2 string references to 'Address'
AddressPaneBase::review in uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php
Returns the review contents of a checkout pane.
uc_store_token_info in uc_store/uc_store.tokens.inc
Implements hook_token_info().

File

uc_store/src/Address.php, line 11

Namespace

Drupal\uc_store
View source
class Address implements AddressInterface {
  use AddressTrait;
  use StringTranslationTrait;

  /**
   * Store default country code.
   *
   * @var string
   */
  protected $defaultCountry;

  /**
   * Constructor.
   *
   * For convenience, country defaults to store country.
   */
  protected function __construct() {
    $this->defaultCountry = \Drupal::config('uc_store.settings')
      ->get('address.country');
    $this->country = $this->defaultCountry;
  }

  /**
   * Creates an Address.
   *
   * @param array $values
   *   (optional) Array of initialization values.
   *
   * @return \Drupal\uc_store\AddressInterface
   *   An Address object.
   */
  public static function create(array $values = NULL) {
    $address = new Address();
    if (isset($values)) {
      foreach ($values as $key => $value) {
        if (property_exists($address, $key)) {
          $address->{$key} = $value;
        }
      }
    }
    return $address;
  }

  /**
   * Formats the address for display based on the country's address format.
   *
   * @return string
   *   A formatted string containing the address.
   */
  public function __toString() {
    $variables = [
      '!company' => $this->company,
      '!first_name' => $this->first_name,
      '!last_name' => $this->last_name,
      '!street1' => $this->street1,
      '!street2' => $this->street2,
      '!city' => $this->city,
      '!postal_code' => $this->postal_code,
    ];
    $country = $this->country ? \Drupal::service('country_manager')
      ->getCountry($this->country) : NULL;
    if ($country) {
      $variables += [
        '!zone_code' => $this->zone ?: $this
          ->t('N/A'),
        '!zone_name' => isset($country
          ->getZones()[$this->zone]) ? $country
          ->getZones()[$this->zone] : $this
          ->t('Unknown'),
        '!country_name' => $this
          ->t($country
          ->getName()),
        '!country_code2' => $country
          ->id(),
        '!country_code3' => $country
          ->getAlpha3(),
      ];
      if ($this->country != $this->defaultCountry) {
        $variables['!country_name_if'] = $variables['!country_name'];
        $variables['!country_code2_if'] = $variables['!country_code2'];
        $variables['!country_code3_if'] = $variables['!country_code3'];
      }
      else {
        $variables['!country_name_if'] = '';
        $variables['!country_code2_if'] = '';
        $variables['!country_code3_if'] = '';
      }
      $format = implode("\n", $country
        ->getAddressFormat());
    }
    else {
      $format = "!company\n!first_name !last_name\n!street1\n!street2\n!city\n!postal_code";
    }
    $address = Html::escape(strtr($format, $variables));

    // Remove empty lines in the middle of an address string (0 or more
    // whitespace characters bracketed by \n) then remove (trim) whitespace
    // from the beginning and end of the string.
    $address = trim(preg_replace("/\n\\s*\n/", "\n", $address));
    if (\Drupal::config('uc_store.settings')
      ->get('capitalize_address')) {
      $address = mb_strtoupper($address);
    }

    // <br> instead of <br />, because Twig will change it to <br> anyway and
    // it's nice to be able to test the Raw output.
    return nl2br($address, FALSE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Address::$defaultCountry protected property Store default country code.
Address::create public static function Creates an Address.
Address::__construct protected function Constructor.
Address::__toString public function Formats the address for display based on the country's address format.
AddressTrait::$city public property City name.
AddressTrait::$company public property Company or organization.
AddressTrait::$country public property ISO 3166-1 2-character numeric country code.
AddressTrait::$email public property Email address.
AddressTrait::$first_name public property Given name.
AddressTrait::$id protected property The unique address identifier.
AddressTrait::$label protected property The human-readable nickname of the location.
AddressTrait::$last_name public property Surname.
AddressTrait::$phone public property Telephone number.
AddressTrait::$postal_code public property Postal code.
AddressTrait::$street1 public property First line of street address.
AddressTrait::$street2 public property Second line of street address.
AddressTrait::$zone public property State, provence, or region id.
AddressTrait::getCity public function
AddressTrait::getCompany public function
AddressTrait::getCountry public function
AddressTrait::getEmail public function
AddressTrait::getFirstName public function
AddressTrait::getLastName public function
AddressTrait::getPhone public function
AddressTrait::getPostalCode public function
AddressTrait::getStreet1 public function
AddressTrait::getStreet2 public function
AddressTrait::getZone public function
AddressTrait::isSamePhysicalLocation public function
AddressTrait::makeCanonical public static function
AddressTrait::setCity public function
AddressTrait::setCompany public function
AddressTrait::setCountry public function
AddressTrait::setEmail public function
AddressTrait::setFirstName public function
AddressTrait::setId public function
AddressTrait::setLabel public function
AddressTrait::setLastName public function
AddressTrait::setPhone public function
AddressTrait::setPostalCode public function
AddressTrait::setStreet1 public function
AddressTrait::setStreet2 public function
AddressTrait::setZone public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.