| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace Elasticsearch\Endpoints;
- use Elasticsearch\Common\Exceptions\UnexpectedValueException;
- use Elasticsearch\Serializers\SerializerInterface;
- use Elasticsearch\Transport;
- use Exception;
- use GuzzleHttp\Ring\Future\FutureArrayInterface;
- /**
- * Class AbstractEndpoint
- *
- * @category Elasticsearch
- * @package Elasticsearch\Endpoints
- * @author Zachary Tong <zach@elastic.co>
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
- * @link http://elastic.co
- */
- abstract class AbstractEndpoint
- {
- /** @var array */
- protected $params = array();
- /** @var string */
- protected $index = null;
- /** @var string */
- protected $type = null;
- /** @var string|int */
- protected $id = null;
- /** @var string */
- protected $method = null;
- /** @var array */
- protected $body = null;
- /** @var array */
- private $options = [];
- /** @var SerializerInterface */
- protected $serializer;
- /**
- * @return string[]
- */
- abstract public function getParamWhitelist();
- /**
- * @return string
- */
- abstract public function getURI();
- /**
- * @return string
- */
- abstract public function getMethod();
- /**
- * Set the parameters for this endpoint
- *
- * @param string[] $params Array of parameters
- * @return $this
- */
- public function setParams($params)
- {
- if (is_object($params) === true) {
- $params = (array) $params;
- }
- $this->checkUserParams($params);
- $params = $this->convertCustom($params);
- $this->extractOptions($params);
- $this->params = $this->convertArraysToStrings($params);
- return $this;
- }
- /**
- * @return array
- */
- public function getParams()
- {
- return $this->params;
- }
- /**
- * @return array
- */
- public function getOptions()
- {
- return $this->options;
- }
- /**
- * @return string|null
- */
- public function getIndex()
- {
- return $this->index;
- }
- /**
- * @param string $index
- *
- * @return $this
- */
- public function setIndex($index)
- {
- if ($index === null) {
- return $this;
- }
- if (is_array($index) === true) {
- $index = array_map('trim', $index);
- $index = implode(",", $index);
- }
- $this->index = urlencode($index);
- return $this;
- }
- /**
- * @return string|null
- */
- public function getType()
- {
- return $this->type;
- }
- /**
- * @param string $type
- *
- * @return $this
- */
- public function setType($type)
- {
- if ($type === null) {
- return $this;
- }
- if (is_array($type) === true) {
- $type = array_map('trim', $type);
- $type = implode(",", $type);
- }
- $this->type = urlencode($type);
- return $this;
- }
- /**
- * @param int|string $docID
- *
- * @return $this
- */
- public function setID($docID)
- {
- if ($docID === null) {
- return $this;
- }
- $this->id = urlencode($docID);
- return $this;
- }
- /**
- * @return array
- */
- public function getBody()
- {
- return $this->body;
- }
- /**
- * @param string $endpoint
- *
- * @return string
- */
- protected function getOptionalURI($endpoint)
- {
- $uri = array();
- $uri[] = $this->getOptionalIndex();
- $uri[] = $this->getOptionalType();
- $uri[] = $endpoint;
- $uri = array_filter($uri);
- return '/' . implode('/', $uri);
- }
- /**
- * @return string
- */
- private function getOptionalIndex()
- {
- if (isset($this->index) === true) {
- return $this->index;
- } else {
- return '_all';
- }
- }
- /**
- * @return string
- */
- private function getOptionalType()
- {
- if (isset($this->type) === true) {
- return $this->type;
- } else {
- return '';
- }
- }
- /**
- * @param array $params
- *
- * @throws \Elasticsearch\Common\Exceptions\UnexpectedValueException
- */
- private function checkUserParams($params)
- {
- if (isset($params) !== true) {
- return; //no params, just return.
- }
- $whitelist = array_merge($this->getParamWhitelist(), array('client', 'custom', 'filter_path', 'human'));
- $invalid = array_diff(array_keys($params), $whitelist);
- if (count($invalid) > 0) {
- sort($invalid);
- sort($whitelist);
- throw new UnexpectedValueException(sprintf(
- (count($invalid) > 1 ? '"%s" are not valid parameters.' : '"%s" is not a valid parameter.').' Allowed parameters are "%s"',
- implode('", "', $invalid),
- implode('", "', $whitelist)
- ));
- }
- }
- /**
- * @param $params Note: this is passed by-reference!
- */
- private function extractOptions(&$params)
- {
- // Extract out client options, then start transforming
- if (isset($params['client']) === true) {
- $this->options['client'] = $params['client'];
- unset($params['client']);
- }
- $ignore = isset($this->options['client']['ignore']) ? $this->options['client']['ignore'] : null;
- if (isset($ignore) === true) {
- if (is_string($ignore)) {
- $this->options['client']['ignore'] = explode(",", $ignore);
- } elseif (is_array($ignore)) {
- $this->options['client']['ignore'] = $ignore;
- } else {
- $this->options['client']['ignore'] = [$ignore];
- }
- }
- }
- private function convertCustom($params)
- {
- if (isset($params['custom']) === true) {
- foreach ($params['custom'] as $k => $v) {
- $params[$k] = $v;
- }
- unset($params['custom']);
- }
- return $params;
- }
- private function convertArraysToStrings($params)
- {
- foreach ($params as $key => &$value) {
- if (!($key === 'client' || $key == 'custom') && is_array($value) === true) {
- if ($this->isNestedArray($value) !== true) {
- $value = implode(",", $value);
- }
- }
- }
- return $params;
- }
- private function isNestedArray($a)
- {
- foreach ($a as $v) {
- if (is_array($v)) {
- return true;
- }
- }
- return false;
- }
- }
|