# FullTextUtils.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00017     final class FullTextUtils extends StaticFactory
    00018     {
    00019         public static function lookup(
    00020             FullTextDAO $dao,
    00021             Criteria $criteria,
    00022             $string
    00023         )
    00024         {
    00025             return
    00026                 $dao->getByQuery(
    00027                     self::makeFullTextQuery($dao, $criteria, $string)->limit(1)
    00028                 );
    00029         }
    00030
    00031         public static function lookupList(
    00032             FullTextDAO $dao, Criteria $criteria, $string
    00033         )
    00034         {
    00035             return
    00036                 $dao->getListByQuery(
    00037                     self::makeFullTextQuery($dao, $criteria, $string)
    00038                 );
    00039         }
    00040
    00045         public static function makeFullTextQuery(
    00046             FullTextDAO $dao, Criteria $criteria, $string
    00047         )
    00048         {
    00049             Assert::isString(
    00050                 $string,
    00051                 'only strings accepted today'
    00052             );
    00053
    00054             $array = self::prepareSearchString($string);
    00055
    00056             if (!$array)
    00057                 throw new ObjectNotFoundException();
    00058
    00059             if (!($field = $dao->getIndexField()) instanceof DBField)
    00060                 $field = new DBField(
    00061                     $dao->getIndexField(),
    00062                     $dao->getTable()
    00063                 );
    00064
    00065             return
    00066                 $criteria->toSelectQuery()->
    00067                 andWhere(
    00068                     Expression::fullTextOr($field, $array)
    00069                 )->
    00070                 prependOrderBy(
    00071                     Expression::fullTextRankAnd($field, $array)
    00072                 )->desc();
    00073         }
    00074
    00075         public static function prepareSearchString($string)
    00076         {
    00077             $array = preg_split('/[\s\pP]+/u', $string);
    00078
    00079             $out = array();
    00080
    00081             for ($i = 0, $size = count($array); $i < $size; ++$i)
    00082                 if (
    00083                     !empty($array[$i])
    00084                     && (
    00085                         $element = preg_replace(
    00086                             '/[^\pL\d\-\+\.\/]/u', null, $array[$i]
    00087                         )
    00088                     )
    00089                 )
    00090                     $out[] = $element;
    00091
    00092             return $out;
    00093         }
    00094     }
    00095 ?>