PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Elastica/Facet/Range.php

https://github.com/philippfrenzel/Elastica
PHP | 126 lines | 50 code | 10 blank | 66 comment | 6 complexity | 63333d65ef0c3adf3ae5e502609490f2 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Implements the range facet.
  4. *
  5. * @category Xodoa
  6. * @package Elastica
  7. * @author Jasper van Wanrooy <jasper@vanwanrooy.net>
  8. * @link http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html
  9. */
  10. class Elastica_Facet_Range extends Elastica_Facet_Abstract
  11. {
  12. /**
  13. * Sets the field for the range.
  14. *
  15. * @param string $field The name of the field for range.
  16. * @return Elastica_Facet_Range
  17. */
  18. public function setField($field)
  19. {
  20. return $this->setParam('field', $field);
  21. }
  22. /**
  23. * Sets the fields by their separate key and value fields.
  24. *
  25. * @param string $keyField The key_field param for the range.
  26. * @param string $valueField The key_value param for the range.
  27. * @return Elastica_Facet_Range
  28. */
  29. public function setKeyValueFields($keyField, $valueField)
  30. {
  31. return $this->setParam('key_field', $keyField)
  32. ->setParam('value_field', $valueField);
  33. }
  34. /**
  35. * Sets the key and value for this facet by script.
  36. *
  37. * @param string $keyScript Script to check whether it falls into the range.
  38. * @param string $valueScript Script to use for statistical calculations.
  39. *
  40. * @return Elastica_Facet_Range
  41. */
  42. public function setKeyValueScripts($keyScript, $valueScript)
  43. {
  44. return $this->setParam('key_script', $keyScript)
  45. ->setParam('value_script', $valueScript);
  46. }
  47. /**
  48. * Sets the ranges for the facet all at once. Sample ranges:
  49. * array (
  50. * array('to' => 50),
  51. * array('from' => 20, 'to' 70),
  52. * array('from' => 70, 'to' => 120),
  53. * array('from' => 150)
  54. * )
  55. *
  56. * @param array $ranges Numerical array with range definitions.
  57. * @return Elastica_Facet_Range
  58. */
  59. public function setRanges(array $ranges)
  60. {
  61. return $this->setParam('ranges', $ranges);
  62. }
  63. /**
  64. * Adds a range to the range facet.
  65. *
  66. * @param mixed $from The from for the range.
  67. * @param mixed $to The to for the range.
  68. * @return Elastica_Facet_Range
  69. */
  70. public function addRange($from = null, $to = null)
  71. {
  72. if (!isset($this->_params['ranges']) || !is_array($this->_params['ranges'])) {
  73. $this->_params['ranges'] = array();
  74. }
  75. $this->_params['ranges'][] = array('from' => $from, 'to' => $to);
  76. return $this;
  77. }
  78. /**
  79. * Creates the full facet definition, which includes the basic
  80. * facet definition of the parent.
  81. *
  82. * @see Elastica_Facet_Abstract::toArray()
  83. * @throws Elastica_Exception_Invalid When the right fields haven't been set.
  84. * @return array
  85. */
  86. public function toArray()
  87. {
  88. /**
  89. * Check the facet for validity.
  90. * There are three ways to set the key and value field for the range:
  91. * - a single field for both key and value; or
  92. * - separate fields for key and value; or
  93. * - separate scripts for key and value.
  94. */
  95. $fieldTypesSet = 0;
  96. if (isset($this->_params['field'])) {
  97. $fieldTypesSet++;
  98. }
  99. if (isset($this->_params['key_field'])) {
  100. $fieldTypesSet++;
  101. }
  102. if (isset($this->_params['key_script'])) {
  103. $fieldTypesSet++;
  104. }
  105. if ($fieldTypesSet === 0) {
  106. throw new Elastica_Exception_Invalid('Neither field, key_field nor key_script is set.');
  107. } elseif ($fieldTypesSet > 1) {
  108. throw new Elastica_Exception_Invalid('Either field, key_field and key_value or key_script and value_script should be set.');
  109. }
  110. /**
  111. * Set the range in the abstract as param.
  112. */
  113. $this->_setFacetParam('range', $this->_params);
  114. return parent::toArray();
  115. }
  116. }