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\Helper\Iterators;
23:
24: use Iterator;
25:
26: // @phpstan-ignore classConstant.deprecatedClass
27: @trigger_error(SearchHitIterator::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
28:
29: /**
30: * @deprecated in 2.4.0 and will be removed in 3.0.0.
31: */
32: class SearchHitIterator implements Iterator, \Countable
33: {
34: /**
35: * @var SearchResponseIterator
36: */
37: private $search_responses;
38:
39: /**
40: * @var int
41: */
42: protected $current_key;
43:
44: /**
45: * @var int
46: */
47: protected $current_hit_index;
48:
49: /**
50: * @var array|null
51: */
52: protected $current_hit_data;
53:
54: /**
55: * @var int
56: */
57: protected $count = 0;
58:
59: /**
60: * Constructor
61: *
62: * @param SearchResponseIterator $search_responses
63: */
64: public function __construct(SearchResponseIterator $search_responses)
65: {
66: $this->search_responses = $search_responses;
67: }
68:
69: /**
70: * Rewinds the internal SearchResponseIterator and itself
71: *
72: * @return void
73: * @see Iterator::rewind()
74: */
75: public function rewind(): void
76: {
77: $this->current_key = 0;
78: $this->search_responses->rewind();
79:
80: // The first page may be empty. In that case, the next page is fetched.
81: $current_page = $this->search_responses->current();
82: if ($this->search_responses->valid() && empty($current_page['hits']['hits'])) {
83: $this->search_responses->next();
84: }
85:
86: $this->count = 0;
87: if (isset($current_page['hits']) && isset($current_page['hits']['total'])) {
88: $this->count = $current_page['hits']['total'];
89: }
90:
91: $this->readPageData();
92: }
93:
94: /**
95: * Advances pointer of the current hit to the next one in the current page. If there
96: * isn't a next hit in the current page, then it advances the current page and moves the
97: * pointer to the first hit in the page.
98: *
99: * @return void
100: * @see Iterator::next()
101: */
102: public function next(): void
103: {
104: $this->current_key++;
105: $this->current_hit_index++;
106: $current_page = $this->search_responses->current();
107: if (isset($current_page['hits']['hits'][$this->current_hit_index])) {
108: $this->current_hit_data = $current_page['hits']['hits'][$this->current_hit_index];
109: } else {
110: $this->search_responses->next();
111: $this->readPageData();
112: }
113: }
114:
115: /**
116: * Returns a boolean indicating whether or not the current pointer has valid data
117: *
118: * @return bool
119: * @see Iterator::valid()
120: */
121: public function valid(): bool
122: {
123: return is_array($this->current_hit_data);
124: }
125:
126: /**
127: * Returns the current hit
128: *
129: * @return array
130: * @see Iterator::current()
131: */
132: public function current(): array
133: {
134: return $this->current_hit_data;
135: }
136:
137: /**
138: * Returns the current hit index. The hit index spans all pages.
139: *
140: * @return int
141: * @see Iterator::key()
142: */
143: public function key(): int
144: {
145: return $this->current_key;
146: }
147:
148: /**
149: * Advances the internal SearchResponseIterator and resets the current_hit_index to 0
150: *
151: * @internal
152: */
153: private function readPageData(): void
154: {
155: if ($this->search_responses->valid()) {
156: $current_page = $this->search_responses->current();
157: $this->current_hit_index = 0;
158: $this->current_hit_data = $current_page['hits']['hits'][$this->current_hit_index];
159: } else {
160: $this->current_hit_data = null;
161: }
162: }
163:
164: /**
165: * {@inheritDoc}
166: */
167: public function count(): int
168: {
169: return $this->count;
170: }
171: }
172: