1: <?php
2:
3: namespace vierbergenlars\Forage\SearchResult;
4:
5: /**
6: * A search result
7: */
8: class SearchResult extends \ArrayObject
9: {
10: /**
11: * The total number of hits the search query generated
12: *
13: * @var int
14: */
15: protected $totalHits = 0;
16:
17: /**
18: * The facets for the search query
19: *
20: * @var array
21: */
22: protected $facets = array();
23:
24: /**
25: * The class that is instanciated for a hit
26: * @var string
27: */
28: protected $hitClass = '\vierbergenlars\Forage\SearchResult\Hit';
29:
30: /**
31: * The class that is instanciated for a facet
32: * @var string
33: */
34: protected $facetClass = '\vierbergenlars\Forage\SearchResult\Facet';
35:
36: /**
37: * Creates a new SearchResult object
38: *
39: * @private
40: * @param array $result_array The result array from the transport layer
41: */
42: public function __construct(array $result_array)
43: {
44: $this->totalHits = $result_array['totalHits'];
45: $facetClass = $this->facetClass;
46: $hitClass = $this->hitClass;
47: foreach($result_array['facets'] as $field => $results) {
48: if(count($results) > 1) // Don't add facets that have at most one result, it's pointless to facet on those.
49: $this->facets[] = new $facetClass($field, $results);
50: }
51: $hits = array();
52: foreach($result_array['hits'] as $hit) {
53: $hits[] = new $hitClass($hit);
54: }
55: parent::__construct($hits);
56: }
57:
58: /**
59: * Gets the facets for the search query
60: *
61: * @return array
62: */
63: public function getFacets()
64: {
65: return $this->facets;
66: }
67:
68: /**
69: * Gets the hits (results) for the search query
70: *
71: * @return array
72: */
73: public function getHits()
74: {
75: return $this->getArrayCopy();
76: }
77:
78: /**
79: * Gets the total number of hits for the search.
80: *
81: * This number may not be equal to the number of hits that are received by getHits()
82: * @return int
83: */
84: public function getTotalHits()
85: {
86: return $this->totalHits;
87: }
88: }
89: