1: <?php
2:
3: namespace vierbergenlars\Forage\SearchQuery;
4:
5: use vierbergenlars\Forage\Transport\TransportInterface;
6:
7: /**
8: * A search query
9: */
10: class Query
11: {
12: /**
13: * The search query
14: * @var string
15: */
16: public $query = '';
17:
18: /**
19: * The fields to search in
20: * @var array
21: */
22: public $searchFields = array();
23:
24: /**
25: * The fields to facet on
26: * @var array
27: */
28: public $facetFields = array();
29:
30: /**
31: * The fields to filter the search result on
32: * @var array
33: */
34: public $searchFilters = array();
35:
36: /**
37: * The offset to the start of the result list
38: * @var int
39: */
40: public $offset = 0;
41:
42: /**
43: * The number of records to fetch
44: * @var int
45: */
46: public $limit = 10;
47:
48: /**
49: * The weights of each column
50: * @var array
51: */
52: public $weights = array();
53:
54: /**
55: * The class that parses the results
56: * @var string
57: */
58: protected $searchResultClass = '\vierbergenlars\Forage\SearchResult\SearchResult';
59:
60: /**
61: * The transport to use
62: * @var \vierbergenlars\Forage\Transport\TransportInterface
63: */
64: protected $transport;
65:
66: /**
67: * Creates a new search query
68: * @param \vierbergenlars\Forage\Transport\TransportInterface $transport
69: * @param string $query
70: */
71: public function __construct(TransportInterface $transport, $query = '') {
72: $this->transport = $transport;
73: $this->query = $query;
74: }
75:
76: /**
77: * Sets the search query
78: *
79: * @param string $query
80: * @return \vierbergenlars\Forage\SearchQuery\Query
81: */
82: public function setQuery($query)
83: {
84: $this->query = $query;
85: return $this;
86: }
87:
88: /**
89: * Sets the fields to search in
90: *
91: * @param array $searchFields
92: * @return \vierbergenlars\Forage\SearchQuery\Query
93: */
94: public function setSearchFields(array $searchFields)
95: {
96: $this->searchFields = $searchFields;
97: return $this;
98: }
99:
100: /**
101: * Sets the fields to facet on
102: *
103: * @param array $facetFields
104: * @return \vierbergenlars\Forage\SearchQuery\Query
105: */
106: public function setFacetFields(array $facetFields)
107: {
108: $this->facetFields = $facetFields;
109: return $this;
110: }
111:
112: /**
113: * Sets filters for search fields
114: * @param array $searchFilters
115: * @return \vierbergenlars\Forage\SearchQuery\Query
116: */
117: public function setSearchFilters(array $searchFilters)
118: {
119: $this->searchFilters = $searchFilters;
120: return $this;
121: }
122:
123: /**
124: * Sets the offset to the start of the result list
125: * @param int $offset
126: * @return \vierbergenlars\Forage\SearchQuery\Query
127: */
128: public function setOffset($offset)
129: {
130: $this->offset = $offset;
131: return $this;
132: }
133:
134: /**
135: * Sets the number of records to fetch
136: * @param int $limit
137: * @return \vierbergenlars\Forage\SearchQuery\Query
138: */
139: public function setLimit($limit)
140: {
141: $this->limit = $limit;
142: return $this;
143: }
144:
145: /**
146: * Sets the weights of each column
147: * @param array $weights
148: * @return \vierbergenlars\Forage\SearchQuery\Query
149: */
150: public function setWeights(array $weights)
151: {
152: $this->weights = $weights;
153: return $this;
154: }
155:
156: /**
157: * Executes the query
158: * @return \vierbergenlars\Forage\SearchResult\SearchResult
159: */
160: public function execute()
161: {
162: $result = $this->transport->search(
163: $this->query,
164: $this->searchFields,
165: $this->facetFields,
166: $this->searchFilters,
167: $this->offset,
168: $this->limit,
169: $this->weights
170: );
171:
172: $resultClass = $this->searchResultClass;
173:
174: return new $resultClass($result);
175: }
176: }
177: