1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 
<?php


namespace Manticoresearch;

/**
 * Manticore result set
 *  List hits returned by a search
 *  Implements iterator and countable
 * @category ManticoreSearch
 * @package ManticoreSearch
 * @author Adrian Nuta <adrian.nuta@manticoresearch.com>
 * @link https://manticoresearch.com
 * @see \Iterator
 */
class ResultSet implements \Iterator, \Countable
{
    private $_position = 0;
    private $_response;
    private $_array = [];
    private $_total = 0;
    private $_took;
    private $_timed_out;
    private $_profile;

    public function __construct($responseObj)
    {
        $this->_response = $responseObj;
        $response = $responseObj->getResponse();
        if (isset($response['hits']['hits'])) {
            $this->_array = $response['hits']['hits'];
            $this->_total = $response['hits']['total'];
        } else {
            $this->_total = 0;
        }
        $this->_took = $response['took'];
        $this->_timed_out = $response['timed_out'];
        if (isset($response['profile'])) {
            $this->_profile = $response['profile'];
        }

    }

    public function rewind()
    {
        $this->_position = 0;
    }

    public function current()
    {
        return new ResultHit($this->_array[$this->_position]);
    }

    public function next()
    {
        $this->_position++;
    }

    public function valid()
    {
        return isset($this->_array[$this->_position]);
    }

    public function key()
    {
        return $this->_position;
    }

    public function getTotal()
    {
        return $this->_total;
    }

    public function getTime()
    {
        return $this->_took;
    }

    public function hasTimedout()
    {
        return $this->_timed_out;
    }

    public function getResponse()
    {
        return $this->_response;
    }

    public function count()
    {
        return count($this->_array);
    }

    public function getProfile()
    {
        return $this->_profile;
    }

}