AbstractNamespace.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Elasticsearch\Namespaces;
  3. use Elasticsearch\Endpoints\AbstractEndpoint;
  4. use Elasticsearch\Transport;
  5. /**
  6. * Class AbstractNamespace
  7. *
  8. * @category Elasticsearch
  9. * @package Elasticsearch\Namespaces
  10. * @author Zachary Tong <zach@elastic.co>
  11. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  12. * @link http://elastic.co
  13. */
  14. abstract class AbstractNamespace
  15. {
  16. /** @var \Elasticsearch\Transport */
  17. protected $transport;
  18. /** @var callback */
  19. protected $endpoints;
  20. /**
  21. * Abstract constructor
  22. *
  23. * @param Transport $transport Transport object
  24. * @param $endpoints
  25. */
  26. public function __construct($transport, $endpoints)
  27. {
  28. $this->transport = $transport;
  29. $this->endpoints = $endpoints;
  30. }
  31. /**
  32. * @param array $params
  33. * @param string $arg
  34. *
  35. * @return null|mixed
  36. */
  37. public function extractArgument(&$params, $arg)
  38. {
  39. if (is_object($params) === true) {
  40. $params = (array) $params;
  41. }
  42. if (array_key_exists($arg, $params) === true) {
  43. $val = $params[$arg];
  44. unset($params[$arg]);
  45. return $val;
  46. } else {
  47. return null;
  48. }
  49. }
  50. /**
  51. * @param $endpoint AbstractEndpoint
  52. *
  53. * @throws \Exception
  54. * @return array
  55. */
  56. protected function performRequest(AbstractEndpoint $endpoint)
  57. {
  58. $response = $this->transport->performRequest(
  59. $endpoint->getMethod(),
  60. $endpoint->getURI(),
  61. $endpoint->getParams(),
  62. $endpoint->getBody(),
  63. $endpoint->getOptions()
  64. );
  65. return $this->transport->resultOrFuture($response, $endpoint->getOptions());
  66. }
  67. }