PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 0ms

/library/Zend/Search/Lucene/Index/Term.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 143 lines | 61 code | 16 blank | 66 comment | 13 complexity | 1042e44ee443ffbd3a32f31e2db44087 MD5 | raw file
  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 Index
 18 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 */
 21
 22
 23/**
 24 * A Term represents a word from text.  This is the unit of search.  It is
 25 * composed of two elements, the text of the word, as a string, and the name of
 26 * the field that the text occured in, an interned string.
 27 *
 28 * Note that terms may represent more than words from text fields, but also
 29 * things like dates, email addresses, urls, etc.
 30 *
 31 * @category   Zend
 32 * @package    Zend_Search_Lucene
 33 * @subpackage Index
 34 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 36 */
 37class Zend_Search_Lucene_Index_Term
 38{
 39    /**
 40     * Field name or field number (depending from context)
 41     *
 42     * @var mixed
 43     */
 44    public $field;
 45
 46    /**
 47     * Term value
 48     *
 49     * @var string
 50     */
 51    public $text;
 52
 53
 54    /**
 55     * Object constructor
 56     */
 57    public function __construct($text, $field = null)
 58    {
 59        $this->field = ($field === null)?  Zend_Search_Lucene::getDefaultSearchField() : $field;
 60        $this->text  = $text;
 61    }
 62
 63
 64    /**
 65     * Returns term key
 66     *
 67     * @return string
 68     */
 69    public function key()
 70    {
 71        return $this->field . chr(0) . $this->text;
 72    }
 73
 74    /**
 75     * Get term prefix
 76     *
 77     * @param string $str
 78     * @param integer $length
 79     * @return string
 80     */
 81    public static function getPrefix($str, $length)
 82    {
 83        $prefixBytes = 0;
 84        $prefixChars = 0;
 85        while ($prefixBytes < strlen($str)  &&  $prefixChars < $length) {
 86            $charBytes = 1;
 87            if ((ord($str[$prefixBytes]) & 0xC0) == 0xC0) {
 88                $charBytes++;
 89                if (ord($str[$prefixBytes]) & 0x20 ) {
 90                    $charBytes++;
 91                    if (ord($str[$prefixBytes]) & 0x10 ) {
 92                        $charBytes++;
 93                    }
 94                }
 95            }
 96
 97            if ($prefixBytes + $charBytes > strlen($str)) {
 98                // wrong character
 99                break;
100            }
101
102            $prefixChars++;
103            $prefixBytes += $charBytes;
104        }
105
106        return substr($str, 0, $prefixBytes);
107    }
108
109    /**
110     * Get UTF-8 string length
111     *
112     * @param string $str
113     * @return string
114     */
115    public static function getLength($str)
116    {
117        $bytes = 0;
118        $chars = 0;
119        while ($bytes < strlen($str)) {
120            $charBytes = 1;
121            if ((ord($str[$bytes]) & 0xC0) == 0xC0) {
122                $charBytes++;
123                if (ord($str[$bytes]) & 0x20 ) {
124                    $charBytes++;
125                    if (ord($str[$bytes]) & 0x10 ) {
126                        $charBytes++;
127                    }
128                }
129            }
130
131            if ($bytes + $charBytes > strlen($str)) {
132                // wrong character
133                break;
134            }
135
136            $chars++;
137            $bytes += $charBytes;
138        }
139
140        return $chars;
141    }
142}
143