* @license PHP License * @package WB * @subpackage base */ /** * String Address * * Simple string functions * * @todo merge with WBGeo_Address * @todo resolve persongenderid * @todo salutation stuff * @version 1.0.0 * @package WB * @subpackage base */ class WBString_Address extends WBStdClass { private $country = 'de'; private $address = array( 'persongenderid' => 0, 'title' => '', 'name' => '', 'address' => '', 'zip' => '', 'city' => '', 'country' => '' ); /** * Set Country Code of Address * * @param string ISO 2-letter country code */ public function setCountry($code) { $this->country = strtolower($code); } /** * Get Address Data * * @param string named address part or empty for all * @return string|array */ public function get($column = '') { if (empty($column)) { return $this->address; } // avoid notice if (!isset($this->address[$column])) { return ''; } return $this->address[$column]; } /** * Parse Address Label * * Convert multiline address label to assiative array * @param string */ public function parseLabel($label) { if (!is_array($label)) { $label = trim($label); $label = explode("\n", $label); } // German addresses only... :-/ while (!empty($label)) { $line = array_pop($label); $line = trim($line); // ignore empty lines if (empty($line)) { continue; } if (empty($this->address['zip'])) { // country is optional if (empty($this->address['country'])) { if (!preg_match('/\d+\s/', $line)) { if (empty($label)) { $this->parseNameLine($line); continue; } $this->address['country'] = $line; continue; } } // zip and city if (preg_match('/\d+\s/', $line)) { $tmp = explode(' ', $line); $this->address['zip'] = array_shift($tmp); $this->address['city'] = implode(' ', $tmp); continue; } } if (empty($this->address['address'])) { $this->address['address'] = $line; continue; } // additional address lines // wild guess address lines have a number in it if (preg_match('/\d+/', $line) && 0 < count($label)) { $this->address['address'] .= "\n" . $line; continue; } $this->parseNameLine($line); } } private function parseNameLine($line) { // Frau / Herr if (preg_match('/^Herr\s+/', $line)) { $this->address['title'] = 'Herr'; $line = preg_replace('/^Herr\s+/', '', $line); $this->address['persongenderid'] = 4; } else if (preg_match('/^Frau\s+/i', $line)) { $this->address['title'] = 'Frau'; $line = preg_replace('/^Frau\s+/', '', $line); $this->address['persongenderid'] = 3; } if (empty($this->address['name'])) { $this->address['name'] = $line; return; } $this->address['name'] .= "\n" . $line; } }