| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Maslosoft\Manganel\Adapters\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Maslosoft\Manganel\Decorators\IndexDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Maslosoft\Manganel\Decorators\MaxScoreDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Maslosoft\Manganel\Decorators\ScoreDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Maslosoft\Manganel\QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Maslosoft\Manganel\SearchCriteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * ElasticSearchCursor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author Piotr Maselkowski <pmaselkowski at gmail.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ElasticSearchCursor implements FinderCursorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * Whether query was sent to search server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	private $isExecuted = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @var QueryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	private $qb = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private $data = []; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 30 |  | 	public function __construct(QueryBuilder $qb) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 30 |  | 		$this->qb = $qb; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 30 |  | 		$criteria = $qb->getCriteria(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 30 |  | 		if (empty($criteria)) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 			$this->qb->setCriteria(new SearchCriteria()); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 30 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 19 |  | 	public function limit($num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 19 |  | 		$this->qb->getCriteria()->limit($num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 19 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 14 |  | 	public function skip($num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 14 |  | 		$this->qb->getCriteria()->offset($num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 14 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 20 |  | 	public function sort(array $fields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 20 |  | 		$this->qb->getCriteria()->setSort($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 20 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function fields(array $fields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->qb->getCriteria()->select($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | // <editor-fold defaultstate="collapsed" desc="Countable impl"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  | 	public function count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  | 		return count($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | // </editor-fold> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | // <editor-fold defaultstate="collapsed" desc="Iterator impl"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 25 |  | 	public function current() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 25 |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 25 |  | 		return current($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	public function key() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return key($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 20 |  | 	public function next() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 20 |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 20 |  | 		next($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 20 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 20 |  | 	public function rewind() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 20 |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 20 |  | 		return reset($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 20 |  | 	public function valid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 20 |  | 		$this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 20 |  | 		$key = key($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 20 |  | 		return ($key !== null && $key !== false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 30 |  | 	private function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 30 |  | 		if (!$this->isExecuted) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 30 |  | 			$this->isExecuted = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 30 |  | 			$results = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 30 |  | 			$data = $this->qb->search(null, $results); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 30 |  | 			$maxScore = $results['hits']['max_score']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 30 |  | 			foreach ($data as $result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 30 |  | 				$document = $result['_source']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 				/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				 * TODO Refactor it into plugable interface, with params: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 				 * $document, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 				 * $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 				 * $results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 				 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 30 |  | 				$document[IndexDecorator::Key] = $result['_index']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 30 |  | 				$document[ScoreDecorator::Key] = $result['_score']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 30 |  | 				$document[MaxScoreDecorator::Key] = $maxScore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 30 |  | 				$this->data[] = $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 30 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | // </editor-fold> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |