Completed
Push — devel ( 3bd8be...9c998f )
by Alexey
02:35
created

SearchResult   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 104
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFirst() 0 8 2
A getResults() 0 4 1
A count() 0 4 1
A getTotalCount() 0 4 1
A isEmpty() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 6 1
A offsetUnset() 0 6 1
A getIterator() 0 4 1
1
<?php
2
3
namespace Bardex\Elastic;
4
5
6
/**
7
 * SearchResult
8
 * @package Bardex\Elastic
9
 * @author Andrey Volynov <[email protected]>
10
 * @author Alexey Sumin <[email protected]>
11
 */
12
class SearchResult implements \ArrayAccess, \Countable, \IteratorAggregate
13
{
14
    /**
15
     * @var  int $totalCount
16
     */
17
    protected $totalCount;
18
19
    /**
20
     * @var array|array[]|object[] $results
21
     */
22
    protected $results = [];
23
24
25
    public function __construct($results, $totalCount)
26
    {
27
        $results = (array) $results;
28
        $this->results = $results;
29
        $this->totalCount = $totalCount;
30
    }
31
32
    /**
33
     * Returns first entry from result set, or null,
34
     * if result set is empty.
35
     *
36
     * @return null|array|object|array[]|object[]
37
     */
38
    public function getFirst()
39
    {
40
        if ($this->isEmpty()) {
41
            return null;
42
        }
43
        $keys = array_keys($this->results);
44
        return $this->results[$keys[0]];
45
    }
46
47
    /**
48
     * Returns the complete result set.
49
     *
50
     * @return array
51
     */
52
    public function getResults()
53
    {
54
        return $this->results;
55
    }
56
57
    /**
58
     * Method returns count of returned query results.
59
     *
60
     * @return int
61
     */
62
    public function count()
63
    {
64
        return count($this->results);
65
    }
66
67
    /**
68
     * Method returns count of total query results in index.
69
     *
70
     * @return int
71
     */
72
    public function getTotalCount()
73
    {
74
        return $this->totalCount;
75
    }
76
77
    /**
78
     * Method determines, if result data set is empty.
79
     *
80
     * @return bool
81
     */
82
    public function isEmpty()
83
    {
84
        return $this->count() == 0;
85
    }
86
87
    public function offsetExists($offset)
88
    {
89
        return array_key_exists($offset, $this->results);
90
    }
91
92
    public function offsetGet($offset)
93
    {
94
        return $this->results[$offset];
95
    }
96
97
    public function offsetSet($offset, $value)
98
    {
99
        $this->results[$offset] = $value;
100
101
        return $this;
102
    }
103
104
    public function offsetUnset($offset)
105
    {
106
        unset($this->results[$offset]);
107
108
        return $this;
109
    }
110
111
    public function getIterator()
112
    {
113
        return new \ArrayIterator($this->results);
114
    }
115
}
116