Document.php 3.3 KB

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