1010
1111use FestivalsApi \Result \EventSearchResult ;
1212use GuzzleHttp \Exception \GuzzleException ;
13- use IteratorAggregate ;
14- use LogicException ;
15- use Traversable ;
16- use function get_class ;
17- use function is_array ;
1813
19- class EventSearchIterator implements IteratorAggregate
14+ /**
15+ * @final since 2.5.0
16+ */
17+ class EventSearchIterator extends AbstractSearchIterator
2018{
2119
22- protected EventSearchResult $ last_result ;
23-
24- protected ?int $ page_size = NULL ;
25-
26- protected ?array $ query = NULL ;
27-
28- protected int $ request_count = 0 ;
29-
30- public function __construct (protected FestivalsApiClient $ client )
31- {
32- }
33-
34- /**
35- * @throws FestivalsApiClientException if API client encounters an error
36- * @throws GuzzleException if Guzzle encounters an error
37- * @throws LogicException if no query set
38- */
39- public function getIterator (): Traversable
40- {
41- $ this ->throwIfNoQuerySet ();
42-
43- $ this ->query ['size ' ] = $ this ->page_size ;
44- $ this ->query ['from ' ] = 0 ;
45- do {
46- $ events = $ this ->makeApiCall ();
47- foreach ($ events as $ event ) {
48- yield $ event ;
49- }
50- $ this ->query ['from ' ] += $ this ->page_size ;
51- } while (count ($ this ->last_result ->getEvents ()) === $ this ->page_size );
52- }
53-
54- /**
55- * Total number of calls to API made by this query
56- */
57- public function getNoOfRequestsMadeByQuery (): int
58- {
59- return $ this ->request_count ;
60- }
61-
6220 /**
63- * Sets the search query
64- *
65- * @param array $query the query parameters eg ['festival'=>'jazz', title='Blue']
66- * @param int $page_size the number of events to return per request, defaults to API max limit
21+ * @deprecated
6722 */
68- public function setQuery (array $ query , int $ page_size = 100 ): void
69- {
70- $ this ->query = $ query ;
71- $ this ->page_size = $ page_size ;
72- $ this ->request_count = 0 ;
73- }
23+ protected EventSearchResult $ last_result ;
7424
7525 /**
7626 * Execute the query and return the events
@@ -80,20 +30,10 @@ public function setQuery(array $query, int $page_size = 100): void
8030 */
8131 protected function makeApiCall (): array
8232 {
83- $ this ->request_count ++;
84- $ this ->last_result = $ this ->client ->searchEvents ($ this ->query );
85-
86- return $ this ->last_result ->getEvents ();
87- }
33+ $ result = $ this ->client ->searchEvents ($ this ->query );
34+ $ this ->last_result = $ result ;
8835
89- /**
90- * @throws LogicException if no query set
91- */
92- protected function throwIfNoQuerySet (): void
93- {
94- if ( ! is_array ($ this ->query )) {
95- throw new LogicException ("You must call " .get_class ($ this )."::setQuery() before iterating result " );
96- }
36+ return $ result ->getEvents ();
9737 }
9838
9939}
0 commit comments