1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\Namespaces;
23:
24: use OpenSearch\EndpointFactoryInterface;
25: use OpenSearch\Endpoints\AbstractEndpoint;
26: use OpenSearch\LegacyEndpointFactory;
27: use OpenSearch\Transport;
28:
29: abstract class AbstractNamespace
30: {
31: /**
32: * @var \OpenSearch\Transport
33: */
34: protected $transport;
35:
36: protected EndpointFactoryInterface $endpointFactory;
37:
38: /**
39: * @var callable
40: *
41: * @deprecated in 2.3.2 and will be removed in 3.0.0. Use $endpointFactory property instead.
42: */
43: protected $endpoints;
44:
45: public function __construct(Transport $transport, callable|EndpointFactoryInterface $endpointFactory)
46: {
47: $this->transport = $transport;
48: if (is_callable($endpointFactory)) {
49: @trigger_error('Passing a callable as $endpointFactory param to ' . __METHOD__ . '() is deprecated in 2.3.2 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
50: $endpoints = $endpointFactory;
51: $endpointFactory = new LegacyEndpointFactory($endpointFactory);
52: } else {
53: $endpoints = function ($c) use ($endpointFactory) {
54: @trigger_error('The $endpoints property is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);
55: return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
56: };
57: }
58: $this->endpoints = $endpoints;
59: $this->endpointFactory = $endpointFactory;
60: }
61:
62: /**
63: * @return null|mixed
64: */
65: public function extractArgument(array &$params, string $arg)
66: {
67: if (array_key_exists($arg, $params) === true) {
68: $val = $params[$arg];
69: unset($params[$arg]);
70: return $val;
71: } else {
72: return null;
73: }
74: }
75:
76: protected function performRequest(AbstractEndpoint $endpoint)
77: {
78: $response = $this->transport->performRequest(
79: $endpoint->getMethod(),
80: $endpoint->getURI(),
81: $endpoint->getParams(),
82: $endpoint->getBody(),
83: $endpoint->getOptions()
84: );
85:
86: return $this->transport->resultOrFuture($response, $endpoint->getOptions());
87: }
88: }
89: