[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/search/Zend/Search/Lucene/Search/ -> QueryTokenizer.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to license@zend.com so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Search_Lucene
  17   * @subpackage Search
  18   * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   */
  21  
  22  
  23  /** Zend_Search_Lucene_Search_QueryToken */
  24  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Search/QueryToken.php';
  25  
  26  /** Zend_Search_Lucene_Exception */
  27  require_once $CFG->dirroot.'/search/Zend/Search/Lucene/Exception.php';
  28  
  29  
  30  /**
  31   * @category   Zend
  32   * @package    Zend_Search_Lucene
  33   * @subpackage Search
  34   * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  35   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  36   */
  37  class Zend_Search_Lucene_Search_QueryTokenizer implements Iterator
  38  {
  39      /**
  40       * inputString tokens.
  41       *
  42       * @var array
  43       */
  44      protected $_tokens = array();
  45  
  46      /**
  47       * tokens pointer.
  48       *
  49       * @var integer
  50       */
  51      protected $_currToken = 0;
  52  
  53  
  54      /**
  55       * QueryTokenize constructor needs query string as a parameter.
  56       *
  57       * @param string $inputString
  58       */
  59      public function __construct($inputString)
  60      {
  61          if (!strlen($inputString)) {
  62              throw new Zend_Search_Lucene_Exception('Cannot tokenize empty query string.');
  63          }
  64  
  65          $currentToken = '';
  66          for ($count = 0; $count < strlen($inputString); $count++) {
  67              if (ctype_alnum( $inputString{$count} ) ||
  68                  $inputString{$count} == '_') {
  69                  $currentToken .= $inputString{$count};
  70              } else if ($inputString{$count} == '\\') { // Escaped character
  71                  $count++;
  72  
  73                  if ($count == strlen($inputString)) {
  74                      throw new Zend_Search_Lucene_Exception('Non finished escape sequence.');
  75                  }
  76  
  77                  $currentToken .= $inputString{$count};
  78              } else {
  79                  // Previous token is finished
  80                  if (strlen($currentToken)) {
  81                      $this->_tokens[] = new Zend_Search_Lucene_Search_QueryToken(Zend_Search_Lucene_Search_QueryToken::TOKTYPE_WORD,
  82                                                                  $currentToken);
  83                      $currentToken = '';
  84                  }
  85  
  86                  if ($inputString{$count} == '+' || $inputString{$count} == '-') {
  87                      $this->_tokens[] = new Zend_Search_Lucene_Search_QueryToken(Zend_Search_Lucene_Search_QueryToken::TOKTYPE_SIGN,
  88                                                                  $inputString{$count});
  89                  } elseif ($inputString{$count} == '(' || $inputString{$count} == ')') {
  90                      $this->_tokens[] = new Zend_Search_Lucene_Search_QueryToken(Zend_Search_Lucene_Search_QueryToken::TOKTYPE_BRACKET,
  91                                                                  $inputString{$count});
  92                  } elseif ($inputString{$count} == ':' && $this->count()) {
  93                      if ($this->_tokens[count($this->_tokens)-1]->type == Zend_Search_Lucene_Search_QueryToken::TOKTYPE_WORD) {
  94                          $this->_tokens[count($this->_tokens)-1]->type = Zend_Search_Lucene_Search_QueryToken::TOKTYPE_FIELD;
  95                      }
  96                  }
  97              }
  98          }
  99  
 100          if (strlen($currentToken)) {
 101              $this->_tokens[] = new Zend_Search_Lucene_Search_QueryToken(Zend_Search_Lucene_Search_QueryToken::TOKTYPE_WORD, $currentToken);
 102          }
 103      }
 104  
 105  
 106      /**
 107       * Returns number of tokens
 108       *
 109       * @return integer
 110       */
 111      public function count()
 112      {
 113          return count($this->_tokens);
 114      }
 115  
 116  
 117      /**
 118       * Returns TRUE if a token exists at the current position.
 119       *
 120       * @return boolean
 121       */
 122      public function valid()
 123      {
 124          return $this->_currToken < $this->count();
 125      }
 126  
 127  
 128      /**
 129       * Resets token stream.
 130       *
 131       * @return integer
 132       */
 133      public function rewind()
 134      {
 135          $this->_currToken = 0;
 136      }
 137  
 138  
 139      /**
 140       * Returns the token at the current position or FALSE if
 141       * the position does not contain a valid token.
 142       *
 143       * @return mixed
 144       */
 145      public function current()
 146      {
 147          return $this->valid() ? $this->_tokens[$this->_currToken] : false;
 148      }
 149  
 150  
 151      /**
 152       * Returns next token
 153       *
 154       * @return Zend_Search_Lucene_Search_QueryToken
 155       */
 156      public function next()
 157      {
 158          return ++$this->_currToken;
 159      }
 160  
 161  
 162      /**
 163       * Return the position of the current token.
 164       *
 165       * @return integer
 166       */
 167      public function key()
 168      {
 169          return $this->_currToken;
 170      }
 171  
 172  }
 173  


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7