00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 final class FullTextUtils extends StaticFactory
00019 {
00020 public static function lookup(FullTextDAO $dao, ObjectQuery $oq, $string)
00021 {
00022 return
00023 $dao->getByQuery(
00024 self::makeFullTextQuery($dao, $oq, $string)->limit(1)
00025 );
00026 }
00027
00028 public static function lookupList(
00029 FullTextDAO $dao, ObjectQuery $oq, $string
00030 )
00031 {
00032 return
00033 $dao->getListByQuery(
00034 self::makeFullTextQuery($dao, $oq, $string)
00035 );
00036 }
00037
00042 public static function makeFullTextQuery(
00043 FullTextDAO $dao, ObjectQuery $oq, $string
00044 )
00045 {
00046 Assert::isString(
00047 $string,
00048 'only strings accepted today'
00049 );
00050
00051 $array = self::prepareSearchString($string);
00052
00053 if (!$array)
00054 throw new ObjectNotFoundException();
00055
00056 if (!($field = $dao->getIndexField()) instanceof DBField)
00057 $field = new DBField(
00058 $dao->getIndexField(),
00059 $dao->getTable()
00060 );
00061
00062 return
00063 $oq->toSelectQuery($dao)->
00064 andWhere(
00065 Expression::fullTextOr($field, $array)
00066 )->
00067 prependOrderBy(
00068 Expression::fullTextRankAnd($field, $array)
00069 )->desc();
00070 }
00071
00072 public static function prepareSearchString($string)
00073 {
00074 $array = preg_split('/[\s\pP]+/u', $string);
00075
00076 $out = array();
00077
00078 for ($i = 0, $size = count($array); $i < $size; ++$i)
00079 if (
00080 !empty($array[$i])
00081 && (
00082 $element = preg_replace(
00083 '/[^\pL\d\-\+\.\/]/u', null, $array[$i]
00084 )
00085 )
00086 )
00087 $out[] = $element;
00088
00089 return $out;
00090 }
00091 }
00092 ?>