1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | namespace OpenSearch\Helper\Iterators; |
23: | |
24: | use Iterator; |
25: | |
26: | |
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: | |
31: | |
32: | class SearchHitIterator implements Iterator, \Countable |
33: | { |
34: | |
35: | |
36: | |
37: | private $search_responses; |
38: | |
39: | |
40: | |
41: | |
42: | protected $current_key; |
43: | |
44: | |
45: | |
46: | |
47: | protected $current_hit_index; |
48: | |
49: | |
50: | |
51: | |
52: | protected $current_hit_data; |
53: | |
54: | |
55: | |
56: | |
57: | protected $count = 0; |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | public function __construct(SearchResponseIterator $search_responses) |
65: | { |
66: | $this->search_responses = $search_responses; |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | public function rewind(): void |
76: | { |
77: | $this->current_key = 0; |
78: | $this->search_responses->rewind(); |
79: | |
80: | |
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: | |
96: | |
97: | |
98: | |
99: | |
100: | |
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: | |
117: | |
118: | |
119: | |
120: | |
121: | public function valid(): bool |
122: | { |
123: | return is_array($this->current_hit_data); |
124: | } |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | public function current(): array |
133: | { |
134: | return $this->current_hit_data; |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | public function key(): int |
144: | { |
145: | return $this->current_key; |
146: | } |
147: | |
148: | |
149: | |
150: | |
151: | |
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: | |
166: | |
167: | public function count(): int |
168: | { |
169: | return $this->count; |
170: | } |
171: | } |
172: | |