Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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