| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types = 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BatchingIterator\Fetchers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BatchingIterator\BatchingFetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * A fetcher that sequentially fetches values from its child fetchers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Comparable to @see MultipleIterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Recursion might be used over the fetchers. So a big amount of fetchers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * can cause the stack to be blown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @since 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class MultipleBatchingFetcher implements BatchingFetcher { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @var BatchingFetcher[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $fetchers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param BatchingFetcher|BatchingFetcher[] $firstFetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @param BatchingFetcher ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @throws InvalidArgumentException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 7 |  | 	public function __construct( $firstFetcher ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 7 |  | 		if ( is_array( $firstFetcher ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 3 |  | 			if ( func_num_args() !== 1 ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  | 				throw new InvalidArgumentException( 'When providing an array of BatchingFetcher, no further arguments are accepted' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 2 |  | 			$this->attachFetchers( $firstFetcher ); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		else { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 4 |  | 			$this->attachFetchers( func_get_args() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 6 |  | 	private function attachFetchers( array $fetchers ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  | 		foreach ( $fetchers as $fetcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  | 			if ( !( $fetcher instanceof BatchingFetcher ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  | 				throw new InvalidArgumentException( 'All constructor arguments should implement BatchingFetcher' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  | 			$this->attachFetcher( $fetcher ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  | 	private function attachFetcher( BatchingFetcher $fetcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  | 		$this->fetchers[] = $fetcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @see BatchingFetcher::fetchNext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param int $maxFetchCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return mixed[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  | 	public function fetchNext( int $maxFetchCount ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  | 		if ( key( $this->fetchers ) === null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  | 			return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		 * @var BatchingFetcher $fetcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  | 		$fetcher = current( $this->fetchers ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  | 		$results = $fetcher->fetchNext( $maxFetchCount ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  | 		if ( !empty( $results ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  | 			return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  | 		next( $this->fetchers ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  | 		return $this->fetchNext( $maxFetchCount ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @see BatchingFetcher::rewind | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  | 	public function rewind() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  | 		foreach ( $this->fetchers as $fetcher ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  | 			$fetcher->rewind(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  | 		reset( $this->fetchers ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } |