Passed
Push — 1.x ( 1fc25c...4e8027 )
by Adrian
05:24 queued 13s
created

ResultSet::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 8
nop 1
dl 0
loc 17
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
4
namespace Manticoresearch;
5
6
/**
7
 * Manticore result set
8
 *  List hits returned by a search
9
 *  Implements iterator and countable
10
 * @category ManticoreSearch
11
 * @package ManticoreSearch
12
 * @author Adrian Nuta <[email protected]>
13
 * @link https://manticoresearch.com
14
 * @see \Iterator
15
 */
16
class ResultSet implements \Iterator, \Countable
17
{
18
    /** @var int The position of the iterator through the result set */
19
    protected $position = 0;
20
21
    /** @var Response */
22
    protected $response;
23
24
    protected $array = [];
25
26
    /** @var int|mixed Total number of results */
27
    protected $total = 0;
28
29
    protected $took;
30
31
    /** @var mixed Did the query time out? */
32
    protected $timed_out;
33
34
    protected $profile;
35
36
    protected $facets;
37
38
    public function __construct($responseObj)
39
    {
40
        $this->response = $responseObj;
41
        $response = $responseObj->getResponse();
42
        if (isset($response['hits']['hits'])) {
43
            $this->array = $response['hits']['hits'];
44
            $this->total = $response['hits']['total'];
45
        } else {
46
            $this->total = 0;
47
        }
48
        $this->took = $response['took'];
49
        $this->timed_out = $response['timed_out'];
50
        if (isset($response['profile'])) {
51
            $this->profile = $response['profile'];
52
        }
53
        if (isset($response['aggregations'])) {
54
            $this->facets = $response['aggregations'];
55
        }
56
    }
57
58
    public function rewind()
59
    {
60
        $this->position = 0;
61
    }
62
63
    public function current()
64
    {
65
        return new ResultHit($this->array[$this->position]);
66
    }
67
68
    public function next()
69
    {
70
        $this->position++;
71
    }
72
73
    public function valid()
74
    {
75
        return isset($this->array[$this->position]);
76
    }
77
78
    public function key()
79
    {
80
        return $this->position;
81
    }
82
83
    public function getTotal()
84
    {
85
        return $this->total;
86
    }
87
88
    public function getTime()
89
    {
90
        return $this->took;
91
    }
92
93
    public function hasTimedout()
94
    {
95
        return $this->timed_out;
96
    }
97
98
    /**
99
     * @return Response
100
     */
101
    public function getResponse()
102
    {
103
        return $this->response;
104
    }
105
106
    public function count()
107
    {
108
        return count($this->array);
109
    }
110
111
    public function getProfile()
112
    {
113
        return $this->profile;
114
    }
115
116
    public function getFacets()
117
    {
118
        return $this->facets;
119
    }
120
}
121