Completed
Push — 1.x ( 747b4c...b6bf38 )
by Adrian
04:52 queued 11s
created

PercolateDocsResultSet::getTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
4
namespace Manticoresearch\Results;
5
6
use Manticoresearch\Response;
7
8
class PercolateDocsResultSet implements \Iterator, \Countable
9
{
10
11
    /** @var int The position of the iterator through the result set */
12
    protected $position = 0;
13
14
    /** @var Response */
15
    protected $response;
16
17
    protected $array = [];
18
19
    /** @var int|mixed Total number of results */
20
    protected $total = 0;
21
22
    protected $took;
23
24
    /** @var mixed Did the query time out? */
25
    protected $timed_out;
26
27
    protected $profile;
28
29
    public function __construct($responseObj, $docs)
30
    {
31
32
        foreach ($docs as $doc) {
33
            $this->array[] = ['doc' => $doc, 'queries' => []];
34
        }
35
        $this->response = $responseObj;
36
        $response = $responseObj->getResponse();
37
        if (isset($response['hits']['hits'])) {
38
            $hits = $response['hits']['hits'];
39
            foreach ($hits as $query) {
40
                if (isset($query['fields'], $query['fields']['_percolator_document_slot'])) {
41
                    foreach ($query['fields']['_percolator_document_slot'] as $d) {
42
                        if (isset($this->array[$d-1])) {
43
                            $this->array[$d-1]['queries'][] =$query;
44
                        }
45
                    }
46
                }
47
            }
48
        }
49
    }
50
51
    public function rewind()
52
    {
53
        $this->position = 0;
54
    }
55
56
    public function current()
57
    {
58
        return new PercolateResultDoc($this->array[$this->position]);
59
    }
60
61
    public function next()
62
    {
63
        $this->position++;
64
    }
65
66
    public function valid()
67
    {
68
        return isset($this->array[$this->position]);
69
    }
70
71
    public function key()
72
    {
73
        return $this->position;
74
    }
75
76
    public function getTotal()
77
    {
78
        return $this->total;
79
    }
80
81
    public function getTime()
82
    {
83
        return $this->took;
84
    }
85
86
    public function hasTimedout()
87
    {
88
        return $this->timed_out;
89
    }
90
91
    /**
92
     * @return Response
93
     */
94
    public function getResponse()
95
    {
96
        return $this->response;
97
    }
98
99
    public function count()
100
    {
101
        return count($this->array);
102
    }
103
104
    public function getProfile()
105
    {
106
        return $this->profile;
107
    }
108
}
109