Document.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Document
  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. /** Zend_Search_Lucene_Field */
  22. require_once 'Zend/Search/Lucene/Field.php';
  23. /**
  24. * A Document is a set of fields. Each field has a name and a textual value.
  25. *
  26. * @category Zend
  27. * @package Zend_Search_Lucene
  28. * @subpackage Document
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Search_Lucene_Document
  33. {
  34. /**
  35. * Associative array Zend_Search_Lucene_Field objects where the keys to the
  36. * array are the names of the fields.
  37. *
  38. * @var array
  39. */
  40. protected $_fields = array();
  41. /**
  42. * Field boost factor
  43. * It's not stored directly in the index, but affects on normalization factor
  44. *
  45. * @var float
  46. */
  47. public $boost = 1.0;
  48. /**
  49. * Proxy method for getFieldValue(), provides more convenient access to
  50. * the string value of a field.
  51. *
  52. * @param $offset
  53. * @return string
  54. */
  55. public function __get($offset)
  56. {
  57. return $this->getFieldValue($offset);
  58. }
  59. /**
  60. * Add a field object to this document.
  61. *
  62. * @param Zend_Search_Lucene_Field $field
  63. * @return Zend_Search_Lucene_Document
  64. */
  65. public function addField(Zend_Search_Lucene_Field $field)
  66. {
  67. $this->_fields[$field->name] = $field;
  68. return $this;
  69. }
  70. /**
  71. * Return an array with the names of the fields in this document.
  72. *
  73. * @return array
  74. */
  75. public function getFieldNames()
  76. {
  77. return array_keys($this->_fields);
  78. }
  79. /**
  80. * Returns Zend_Search_Lucene_Field object for a named field in this document.
  81. *
  82. * @param string $fieldName
  83. * @return Zend_Search_Lucene_Field
  84. */
  85. public function getField($fieldName)
  86. {
  87. if (!array_key_exists($fieldName, $this->_fields)) {
  88. throw new Zend_Search_Lucene_Exception("Field name \"$fieldName\" not found in document.");
  89. }
  90. return $this->_fields[$fieldName];
  91. }
  92. /**
  93. * Returns the string value of a named field in this document.
  94. *
  95. * @see __get()
  96. * @return string
  97. */
  98. public function getFieldValue($fieldName)
  99. {
  100. return $this->getField($fieldName)->value;
  101. }
  102. /**
  103. * Returns the string value of a named field in UTF-8 encoding.
  104. *
  105. * @see __get()
  106. * @return string
  107. */
  108. public function getFieldUtf8Value($fieldName)
  109. {
  110. return $this->getField($fieldName)->getUtf8Value();
  111. }
  112. }