/** * @param string $keywords * @param string $encoding * @return Result */ public function search($keywords, $encoding = 'UTF-8') { if ($encoding !== 'UTF-8') { $keywords = mb_convert_encoding($keywords, 'UTF-8', $encoding); } // 검색 키워드의 유효성을 확인한다. if (($keywords = trim($keywords)) === '') { return new Result('Keyword Not Supplied'); } elseif (!mb_check_encoding($keywords, $encoding)) { return new Result('Keyword is Not Valid ' . $encoding); } elseif (($len = mb_strlen($keywords, 'UTF-8')) < 3 || $len > 80) { return new Result('Keyword is Too Long or Too Short'); } // 검색 시작 시각을 기록한다. $startTime = microtime(true); // 검색 키워드를 분석하여 쿼리 객체를 생성한다. $query = Query::parseKeyword($keywords); $result = null; if (!($result = $this->get($query))) { $result = $this->database->search($query); $this->put($query, $result); } else { $result->cache = 'HIT'; } // 반환할 인코딩이 UTF-8이 아닌 경우 여기서 변환한다. if ($encoding !== 'UTF-8') { foreach ($result->results as $record) { $properties = get_object_vars($record); foreach ($properties as $key => $value) { $record->{$key} = mb_convert_encoding($value, $encoding, 'UTF-8'); } } } // 검색 소요 시간을 기록한다. $result->time = number_format(microtime(true) - $startTime, 3); // 결과를 반환한다. return $result; }
/** @test */ public function it_should_find_jibeon_addresses() { $query = Query::parseKeyword('삼성동 123-4'); $result = $this->database->search($query); $this->assertNotEmpty($result->results); }