/** * @param $postcode * * @return array */ public function geocode($postcode) { preg_match('/^([A-Z]{1,2})([0-9][^\\ ]?) ?([0-9]?)([A-Z]{2})?$/', strtoupper($postcode), $matches); for ($index = 0; $index < 5; $index++) { if (!isset($matches[$index])) { $matches[$index] = ''; } } list($full_match, $area, $district, $sector_prefix, $sector_suffix) = $matches; foreach (array($this->db_base_dir . "/{$area}/{$district}/{$sector_prefix}.json", $this->db_base_dir . "/{$area}/{$district}/centre.json") as $key => $json_path) { $result = $this->file_reader->json_read($json_path); if ($result !== NULL) { if ($key === 0) { if (isset($result[$sector_suffix])) { return array('lat' => $result[$sector_suffix][0], 'lon' => $result[$sector_suffix][1], 'exact' => TRUE); } } else { return array('lat' => $result[0], 'lon' => $result[1], 'exact' => FALSE); } } } }
/** * @param \Ingenerator\CodePointSimple\Helper\FileReader $file_reader */ function it_handles_gratuitous_whitespace_in_partial_postcodes($file_reader) { $file_reader->json_read(self::DB_BASE_DIR . '/EH/41/centre.json')->willReturn(array('EH41', 'centre')); $file_reader->json_read(self::DB_BASE_DIR . '/EH/4/centre.json')->willReturn(array('EH4', 'centre')); $this->subject->geocode('EH4 ')->shouldBe(array('lat' => 'EH4', 'lon' => 'centre', 'exact' => FALSE)); $this->subject->geocode('EH41 ')->shouldBe(array('lat' => 'EH41', 'lon' => 'centre', 'exact' => FALSE)); }