+ // Returns the address formated for postal use.
+ // The main rules are (cf AFNOR XPZ 10-011):
+ // -everything in upper case;
+ // -if there are more then than 38 characters in a lign, split it;
+ // -if there are more then than 32 characters in the description of the "street", use abbreviations.
+ private function getPostalAddress($text) {
+ static $abbreviations = array(
+ "IMPASSE" => "IMP",
+ "RUE" => "R",
+ "AVENUE" => "AV",
+ "BOULEVARD" => "BVD",
+ "ROUTE" => "R",
+ "STREET" => "ST",
+ "ROAD" => "RD",
+ );
+
+ $text = strtoupper($text);
+ $arrayText = explode("\n", $text);
+ $postalText = "";
+
+ foreach ($arrayText as $i => $lign) {
+ $postalText .= (($i == 0) ? "" : "\n");
+ if (($length = strlen($lign)) > 32) {
+ $words = explode(" ", $lign);
+ $count = 0;
+ foreach ($words as $word) {
+ if (isset($abbreviations[$word])) {
+ $word = $abbreviations[$word];
+ }
+ if ($count + ($wordLength = strlen($word)) <= 38) {
+ $postalText .= (($count == 0) ? "" : " ") . $word;
+ $count += (($count == 0) ? 0 : 1) + $wordLength;
+ } else {
+ $postalText .= "\n" . $word;
+ $count = strlen($word);
+ }
+ }
+ } else {
+ $postalText .= $lign;
+ }
+ }
+ return $postalText;
+ }
+
+ // Trims the name of the real country if it contains an ISO 3166-1 non-country
+ // item. For that purpose, we compare the last but one line of the address with
+ // all non-country items of ISO 3166-1.
+ private function getTextToGeocode($address)
+ {
+ $res = XDB::iterator('SELECT country, countryFR
+ FROM geoloc_countries
+ WHERE belongsTo IS NOT NULL');
+ $countries = array();
+ foreach ($res as $item) {
+ $countries[] = $item[0];
+ $countries[] = $item[1];
+ }
+ $textLines = explode("\n", $address['text']);
+ $countLines = count($textLines);
+ $needle = strtoupper(trim($textLines[$countLines - 2]));
+ $isPseudoCountry = false;
+ foreach ($countries as $country) {
+ if (strtoupper($country) == $needle) {
+ $isPseudoCountry = true;
+ break;
+ }
+ }
+
+ if ($isPseudoCountry) {
+ return $address['text'];
+ }
+ return implode("\n", array_slice($textLines, 0, -1));
+ }
+