Example #1
0
 public static function getFormattedAddressFieldsValues($address, $addressFormat, $id_lang = null)
 {
     if (!$id_lang) {
         $id_lang = Context::getContext()->language->id;
     }
     $tab = array();
     $temporyObject = array();
     // Check if $address exist and it's an instanciate object of Address
     if ($address && $address instanceof Address) {
         foreach ($addressFormat as $line) {
             if (($keyList = preg_split(self::_CLEANING_REGEX_, $line, -1, PREG_SPLIT_NO_EMPTY)) && is_array($keyList)) {
                 foreach ($keyList as $pattern) {
                     if ($associateName = explode(':', $pattern)) {
                         $totalName = count($associateName);
                         if ($totalName == 1 && isset($address->{$associateName[0]})) {
                             $tab[$associateName[0]] = $address->{$associateName[0]};
                         } else {
                             $tab[$pattern] = '';
                             // Check if the property exist in both classes
                             if ($totalName == 2 && class_exists($associateName[0]) && property_exists($associateName[0], $associateName[1]) && property_exists($address, 'id_' . strtolower($associateName[0]))) {
                                 $idFieldName = 'id_' . strtolower($associateName[0]);
                                 if (!isset($temporyObject[$associateName[0]])) {
                                     $temporyObject[$associateName[0]] = new $associateName[0]($address->{$idFieldName});
                                 }
                                 if ($temporyObject[$associateName[0]]) {
                                     $tab[$pattern] = is_array($temporyObject[$associateName[0]]->{$associateName[1]}) ? isset($temporyObject[$associateName[0]]->{$associateName[1]}[$id_lang]) ? $temporyObject[$associateName[0]]->{$associateName[1]}[$id_lang] : '' : $temporyObject[$associateName[0]]->{$associateName[1]};
                                 }
                             }
                         }
                     }
                 }
                 AddressFormat::_setOriginalDisplayFormat($tab, $line, $keyList);
             }
         }
     }
     AddressFormat::cleanOrderedAddress($addressFormat);
     // Free the instanciate objects
     foreach ($temporyObject as &$object) {
         unset($object);
     }
     return $tab;
 }