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\LegacyTransportWrapper;
28: use OpenSearch\Transport;
29: use OpenSearch\TransportInterface;
30:
31: abstract class AbstractNamespace
32: {
33: /**
34: * @var \OpenSearch\Transport
35: *
36: * @deprecated in 2.4.0 and will be removed in 3.0.0. Use $httpTransport property instead.
37: */
38: protected $transport;
39:
40: protected TransportInterface $httpTransport;
41:
42: protected EndpointFactoryInterface $endpointFactory;
43:
44: /**
45: * @var callable
46: *
47: * @deprecated in 2.4.0 and will be removed in 3.0.0. Use $endpointFactory property instead.
48: */
49: protected $endpoints;
50:
51: /**
52: * @phpstan-ignore parameter.deprecatedClass
53: */
54: public function __construct(TransportInterface|Transport $transport, callable|EndpointFactoryInterface $endpointFactory)
55: {
56: if (!$transport instanceof TransportInterface) {
57: @trigger_error('Passing an instance of \OpenSearch\Transport to ' . __METHOD__ . '() is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\TransportInterface instead.', E_USER_DEPRECATED);
58: // @phpstan-ignore property.deprecated
59: $this->transport = $transport;
60: // @phpstan-ignore new.deprecated
61: $this->httpTransport = new LegacyTransportWrapper($transport);
62: } else {
63: $this->httpTransport = $transport;
64: }
65: if (is_callable($endpointFactory)) {
66: @trigger_error('Passing a callable as $endpointFactory param to ' . __METHOD__ . '() is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
67: $endpoints = $endpointFactory;
68: // @phpstan-ignore new.deprecated
69: $endpointFactory = new LegacyEndpointFactory($endpointFactory);
70: } else {
71: $endpoints = function ($c) use ($endpointFactory) {
72: @trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
73: return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
74: };
75: }
76: // @phpstan-ignore property.deprecated
77: $this->endpoints = $endpoints;
78: $this->endpointFactory = $endpointFactory;
79: }
80:
81: /**
82: * @return null|mixed
83: */
84: public function extractArgument(array &$params, string $arg)
85: {
86: if (array_key_exists($arg, $params) === true) {
87: $val = $params[$arg];
88: unset($params[$arg]);
89: return $val;
90: } else {
91: return null;
92: }
93: }
94:
95: protected function performRequest(AbstractEndpoint $endpoint)
96: {
97: return $this->httpTransport->sendRequest(
98: $endpoint->getMethod(),
99: $endpoint->getURI(),
100: $endpoint->getParams(),
101: $endpoint->getBody(),
102: $endpoint->getOptions()
103: );
104:
105: }
106: }
107: