PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Elastica/Facet/Range.php

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