1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 * @package   Zend_Search
9 */
10
11namespace ZendSearch\Lucene\Search;
12
13use ZendSearch\Lucene;
14use ZendSearch\Lucene\Document;
15
16/**
17 * @category   Zend
18 * @package    Zend_Search_Lucene
19 * @subpackage Search
20 */
21class QueryHit
22{
23    /**
24     * Object handle of the index
25     * @var \ZendSearch\Lucene\SearchIndexInterface
26     */
27    protected $_index = null;
28
29    /**
30     * Object handle of the document associated with this hit
31     * @var \ZendSearch\Lucene\Document
32     */
33    protected $_document = null;
34
35    /**
36     * Unique hit id
37     * @var integer
38     */
39    public $id;
40
41    /**
42     * Number of the document in the index
43     * @var integer
44     */
45    public $document_id;
46
47    /**
48     * Score of the hit
49     * @var float
50     */
51    public $score;
52
53
54    /**
55     * Constructor - pass object handle of Zend_Search_Lucene_Interface index that produced
56     * the hit so the document can be retrieved easily from the hit.
57     *
58     * @param \ZendSearch\Lucene\SearchIndexInterface $index
59     */
60
61    public function __construct(Lucene\SearchIndexInterface $index)
62    {
63        $this->_index = $index;
64    }
65
66    /**
67     * Magic method for checking the existence of a field
68     *
69     * @param string $offset
70     * @return boolean TRUE if the field exists else FALSE
71     */
72    public function __isset($offset)
73    {
74        return isset($this->getDocument()->$offset);
75    }
76
77
78    /**
79     * Convenience function for getting fields from the document
80     * associated with this hit.
81     *
82     * @param string $offset
83     * @return string
84     */
85    public function __get($offset)
86    {
87        return $this->getDocument()->getFieldValue($offset);
88    }
89
90
91    /**
92     * Return the document object for this hit
93     *
94     * @return \ZendSearch\Lucene\Document
95     */
96    public function getDocument()
97    {
98        if (!$this->_document instanceof Document) {
99            $this->_document = $this->_index->getDocument($this->document_id);
100        }
101
102        return $this->_document;
103    }
104
105
106    /**
107     * Return the index object for this hit
108     *
109     * @return \ZendSearch\Lucene\SearchIndexInterface
110     */
111    public function getIndex()
112    {
113        return $this->_index;
114    }
115}
116