88namespace FestivalsApi ;
99
1010
11- use FestivalsApi \Result \EventSearchResult ;
1211use GuzzleHttp \Exception \GuzzleException ;
13- use IteratorAggregate ;
14- use LogicException ;
15- use Traversable ;
16- use function get_class ;
17- use function is_array ;
1812
19- class EventSearchIterator implements IteratorAggregate
13+ class EventSearchIterator extends AbstractSearchIterator
2014{
2115
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-
62- /**
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
67- */
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- }
74-
7516 /**
7617 * Execute the query and return the events
7718 *
@@ -80,20 +21,7 @@ public function setQuery(array $query, int $page_size = 100): void
8021 */
8122 protected function makeApiCall (): array
8223 {
83- $ this ->request_count ++;
84- $ this ->last_result = $ this ->client ->searchEvents ($ this ->query );
85-
86- return $ this ->last_result ->getEvents ();
87- }
88-
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- }
24+ return $ this ->client ->searchEvents ($ this ->query )->getEvents ();
9725 }
9826
9927}
0 commit comments