Get.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Common\Exceptions;
  4. /**
  5. * Class Get
  6. *
  7. * @category Elasticsearch
  8. * @package Elasticsearch\Endpoints
  9. * @author Zachary Tong <zach@elastic.co>
  10. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  11. * @link http://elastic.co
  12. */
  13. class Get extends AbstractEndpoint
  14. {
  15. /** @var bool */
  16. private $returnOnlySource = false;
  17. /** @var bool */
  18. private $checkOnlyExistance = false;
  19. /**
  20. * @return $this
  21. */
  22. public function returnOnlySource()
  23. {
  24. $this->returnOnlySource = true;
  25. return $this;
  26. }
  27. /**
  28. * @return $this
  29. */
  30. public function checkOnlyExistance()
  31. {
  32. $this->checkOnlyExistance = true;
  33. return $this;
  34. }
  35. /**
  36. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  37. * @return string
  38. */
  39. public function getURI()
  40. {
  41. if (isset($this->id) !== true) {
  42. throw new Exceptions\RuntimeException(
  43. 'id is required for Get'
  44. );
  45. }
  46. if (isset($this->index) !== true) {
  47. throw new Exceptions\RuntimeException(
  48. 'index is required for Get'
  49. );
  50. }
  51. if (isset($this->type) !== true) {
  52. throw new Exceptions\RuntimeException(
  53. 'type is required for Get'
  54. );
  55. }
  56. $id = $this->id;
  57. $index = $this->index;
  58. $type = $this->type;
  59. $uri = "/$index/$type/$id";
  60. if (isset($index) === true && isset($type) === true && isset($id) === true) {
  61. $uri = "/$index/$type/$id";
  62. }
  63. if ($this->returnOnlySource === true) {
  64. $uri .= '/_source';
  65. }
  66. return $uri;
  67. }
  68. /**
  69. * @return string[]
  70. */
  71. public function getParamWhitelist()
  72. {
  73. return array(
  74. 'fields',
  75. 'parent',
  76. 'preference',
  77. 'realtime',
  78. 'refresh',
  79. 'routing',
  80. '_source',
  81. '_source_exclude',
  82. '_source_include',
  83. 'version',
  84. 'version_type',
  85. 'stored_fields'
  86. );
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getMethod()
  92. {
  93. if ($this->checkOnlyExistance === true) {
  94. return 'HEAD';
  95. } else {
  96. return 'GET';
  97. }
  98. }
  99. }