formatAddress() public static méthode

Formats an address in an array form.
public static formatAddress ( array $address, string $sep = ', ' ) : string
$address array The address array (required keys: firstname, lastname, address1, postcode, city, country_code)
$sep string The address separator
Résultat string
 /**
  * Gets the HTML of an address
  *
  * @param \Twig_Environment $environment A Twig environment
  * @param mixed             $address     An instance of AddressInterface or array with keys: (id, firstname, lastname, address1, postcode, city, country_code and optionally name, address2, address3)
  * @param bool              $showName    Display address name?
  * @param bool              $showEdit    Display edit button?
  * @param string            $context     A context for edit link
  *
  * @throws InvalidParameterException
  *
  * @return string
  */
 public function renderAddress(\Twig_Environment $environment, $address, $showName = true, $showEdit = false, $context = null)
 {
     $requiredAddressKeys = array("firstname", "lastname", "address1", "postcode", "city", "country_code");
     if (!$address instanceof AddressInterface && (!is_array($address) || count(array_diff($requiredAddressKeys, array_keys($address))) !== 0)) {
         throw new InvalidParameterException(sprintf("sonata_address_render needs an AddressInterface instance or an array with keys (%s)", implode(", ", $requiredAddressKeys)));
     }
     if ($address instanceof AddressInterface) {
         $addressArray = array('id' => $showEdit ? $address->getId() : "", 'name' => $showName ? $address->getName() : "", 'address' => $address->getFullAddressHtml());
     } else {
         if ($showEdit && !array_key_exists("id", $address)) {
             throw new InvalidParameterException("sonata_address_render needs 'id' key to be set to render the edit button");
         }
         if ($showName && !array_key_exists('name', $address)) {
             $address["name"] = "";
             $showName = false;
         }
         $addressArray = array('id' => $showEdit ? $address['id'] : "", 'name' => $address['name'], 'address' => BaseAddress::formatAddress($address, "<br/>"));
     }
     return $environment->render('SonataCustomerBundle:Addresses:_address.html.twig', array('address' => $addressArray, 'showName' => $showName, 'showEdit' => $showEdit, 'context' => $context));
 }
Exemple #2
0
 /**
  * Returns formatted billing address.
  *
  * @param string $sep
  *
  * @return string
  */
 public function getFullBilling($sep = ', ')
 {
     return BaseAddress::formatAddress($this->getBillingAsArray(), $sep);
 }