AbstractEndpoint.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Elasticsearch\Endpoints;
  3. use Elasticsearch\Common\Exceptions\UnexpectedValueException;
  4. use Elasticsearch\Serializers\SerializerInterface;
  5. use Elasticsearch\Transport;
  6. use Exception;
  7. use GuzzleHttp\Ring\Future\FutureArrayInterface;
  8. /**
  9. * Class AbstractEndpoint
  10. *
  11. * @category Elasticsearch
  12. * @package Elasticsearch\Endpoints
  13. * @author Zachary Tong <zach@elastic.co>
  14. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  15. * @link http://elastic.co
  16. */
  17. abstract class AbstractEndpoint
  18. {
  19. /** @var array */
  20. protected $params = array();
  21. /** @var string */
  22. protected $index = null;
  23. /** @var string */
  24. protected $type = null;
  25. /** @var string|int */
  26. protected $id = null;
  27. /** @var string */
  28. protected $method = null;
  29. /** @var array */
  30. protected $body = null;
  31. /** @var array */
  32. private $options = [];
  33. /** @var SerializerInterface */
  34. protected $serializer;
  35. /**
  36. * @return string[]
  37. */
  38. abstract public function getParamWhitelist();
  39. /**
  40. * @return string
  41. */
  42. abstract public function getURI();
  43. /**
  44. * @return string
  45. */
  46. abstract public function getMethod();
  47. /**
  48. * Set the parameters for this endpoint
  49. *
  50. * @param string[] $params Array of parameters
  51. * @return $this
  52. */
  53. public function setParams($params)
  54. {
  55. if (is_object($params) === true) {
  56. $params = (array) $params;
  57. }
  58. $this->checkUserParams($params);
  59. $params = $this->convertCustom($params);
  60. $this->extractOptions($params);
  61. $this->params = $this->convertArraysToStrings($params);
  62. return $this;
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function getParams()
  68. {
  69. return $this->params;
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getOptions()
  75. {
  76. return $this->options;
  77. }
  78. /**
  79. * @return string|null
  80. */
  81. public function getIndex()
  82. {
  83. return $this->index;
  84. }
  85. /**
  86. * @param string $index
  87. *
  88. * @return $this
  89. */
  90. public function setIndex($index)
  91. {
  92. if ($index === null) {
  93. return $this;
  94. }
  95. if (is_array($index) === true) {
  96. $index = array_map('trim', $index);
  97. $index = implode(",", $index);
  98. }
  99. $this->index = urlencode($index);
  100. return $this;
  101. }
  102. /**
  103. * @return string|null
  104. */
  105. public function getType()
  106. {
  107. return $this->type;
  108. }
  109. /**
  110. * @param string $type
  111. *
  112. * @return $this
  113. */
  114. public function setType($type)
  115. {
  116. if ($type === null) {
  117. return $this;
  118. }
  119. if (is_array($type) === true) {
  120. $type = array_map('trim', $type);
  121. $type = implode(",", $type);
  122. }
  123. $this->type = urlencode($type);
  124. return $this;
  125. }
  126. /**
  127. * @param int|string $docID
  128. *
  129. * @return $this
  130. */
  131. public function setID($docID)
  132. {
  133. if ($docID === null) {
  134. return $this;
  135. }
  136. $this->id = urlencode($docID);
  137. return $this;
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function getBody()
  143. {
  144. return $this->body;
  145. }
  146. /**
  147. * @param string $endpoint
  148. *
  149. * @return string
  150. */
  151. protected function getOptionalURI($endpoint)
  152. {
  153. $uri = array();
  154. $uri[] = $this->getOptionalIndex();
  155. $uri[] = $this->getOptionalType();
  156. $uri[] = $endpoint;
  157. $uri = array_filter($uri);
  158. return '/' . implode('/', $uri);
  159. }
  160. /**
  161. * @return string
  162. */
  163. private function getOptionalIndex()
  164. {
  165. if (isset($this->index) === true) {
  166. return $this->index;
  167. } else {
  168. return '_all';
  169. }
  170. }
  171. /**
  172. * @return string
  173. */
  174. private function getOptionalType()
  175. {
  176. if (isset($this->type) === true) {
  177. return $this->type;
  178. } else {
  179. return '';
  180. }
  181. }
  182. /**
  183. * @param array $params
  184. *
  185. * @throws \Elasticsearch\Common\Exceptions\UnexpectedValueException
  186. */
  187. private function checkUserParams($params)
  188. {
  189. if (isset($params) !== true) {
  190. return; //no params, just return.
  191. }
  192. $whitelist = array_merge($this->getParamWhitelist(), array('client', 'custom', 'filter_path', 'human'));
  193. $invalid = array_diff(array_keys($params), $whitelist);
  194. if (count($invalid) > 0) {
  195. sort($invalid);
  196. sort($whitelist);
  197. throw new UnexpectedValueException(sprintf(
  198. (count($invalid) > 1 ? '"%s" are not valid parameters.' : '"%s" is not a valid parameter.').' Allowed parameters are "%s"',
  199. implode('", "', $invalid),
  200. implode('", "', $whitelist)
  201. ));
  202. }
  203. }
  204. /**
  205. * @param $params Note: this is passed by-reference!
  206. */
  207. private function extractOptions(&$params)
  208. {
  209. // Extract out client options, then start transforming
  210. if (isset($params['client']) === true) {
  211. $this->options['client'] = $params['client'];
  212. unset($params['client']);
  213. }
  214. $ignore = isset($this->options['client']['ignore']) ? $this->options['client']['ignore'] : null;
  215. if (isset($ignore) === true) {
  216. if (is_string($ignore)) {
  217. $this->options['client']['ignore'] = explode(",", $ignore);
  218. } elseif (is_array($ignore)) {
  219. $this->options['client']['ignore'] = $ignore;
  220. } else {
  221. $this->options['client']['ignore'] = [$ignore];
  222. }
  223. }
  224. }
  225. private function convertCustom($params)
  226. {
  227. if (isset($params['custom']) === true) {
  228. foreach ($params['custom'] as $k => $v) {
  229. $params[$k] = $v;
  230. }
  231. unset($params['custom']);
  232. }
  233. return $params;
  234. }
  235. private function convertArraysToStrings($params)
  236. {
  237. foreach ($params as $key => &$value) {
  238. if (!($key === 'client' || $key == 'custom') && is_array($value) === true) {
  239. if ($this->isNestedArray($value) !== true) {
  240. $value = implode(",", $value);
  241. }
  242. }
  243. }
  244. return $params;
  245. }
  246. private function isNestedArray($a)
  247. {
  248. foreach ($a as $v) {
  249. if (is_array($v)) {
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. }