Create.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Common\Exceptions;
  4. /**
  5. * Class Create
  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 Create extends AbstractEndpoint
  14. {
  15. /**
  16. * @param array $body
  17. *
  18. * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
  19. * @return $this
  20. */
  21. public function setBody($body)
  22. {
  23. if (isset($body) !== true) {
  24. return $this;
  25. }
  26. $this->body = $body;
  27. return $this;
  28. }
  29. /**
  30. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  31. * @return string
  32. */
  33. public function getURI()
  34. {
  35. if (isset($this->index) !== true) {
  36. throw new Exceptions\RuntimeException(
  37. 'index is required for Create'
  38. );
  39. }
  40. if (isset($this->type) !== true) {
  41. throw new Exceptions\RuntimeException(
  42. 'type is required for Create'
  43. );
  44. }
  45. if (isset($this->id) !== true) {
  46. throw new Exceptions\RuntimeException(
  47. 'id is required for Create'
  48. );
  49. }
  50. $id = $this->id;
  51. $index = $this->index;
  52. $type = $this->type;
  53. return "/$index/$type/$id/_create";
  54. }
  55. /**
  56. * @return string[]
  57. */
  58. public function getParamWhitelist()
  59. {
  60. return array(
  61. 'consistency',
  62. 'op_type',
  63. 'parent',
  64. 'percolate',
  65. 'refresh',
  66. 'replication',
  67. 'routing',
  68. 'timeout',
  69. 'timestamp',
  70. 'ttl',
  71. 'version',
  72. 'version_type',
  73. 'pipeline'
  74. );
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function getMethod()
  80. {
  81. return 'PUT';
  82. }
  83. /**
  84. * @return array
  85. * @throws \Elasticsearch\Common\Exceptions\RuntimeException
  86. */
  87. public function getBody()
  88. {
  89. if (isset($this->body) !== true) {
  90. throw new Exceptions\RuntimeException('Document body must be set for create request');
  91. } else {
  92. return $this->body;
  93. }
  94. }
  95. }