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 /* $Id: FullTextUtils.class.php 4687 2007-12-09 18:57:18Z voxus $ */
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 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4