1: <?php
2:
3: namespace vierbergenlars\Forage\SearchQuery;
4:
5: use vierbergenlars\Forage\SearchQuery\Query;
6: 7: 8:
9: class QueryBuilder
10: {
11: 12: 13: 14:
15: protected $query;
16:
17: 18: 19: 20:
21: public function __construct(Query $query)
22: {
23: $this->query = $query;
24: }
25:
26: 27: 28: 29: 30:
31: public function setSearchQuery($query)
32: {
33: $this->query->setQuery($query);
34: return $this;
35: }
36:
37: 38: 39: 40: 41:
42: public function setOffset($offset)
43: {
44: $this->query->setOffset($offset);
45: return $this;
46: }
47:
48: 49: 50: 51: 52:
53: public function setLimit($limit)
54: {
55: $this->query->setLimit($limit);
56: return $this;
57: }
58:
59: 60: 61: 62: 63:
64: public function addSearchField($field)
65: {
66: $this->_addField('searchFields', $field);
67: return $this;
68: }
69:
70: 71: 72: 73: 74:
75: public function removeSearchField($field)
76: {
77: $this->_removeField('searchFields', $field);
78: return $this;
79: }
80:
81: 82: 83: 84: 85:
86: public function addFacet($facet)
87: {
88: $this->_addField('facetFields', $facet);
89: return $this;
90: }
91:
92: 93: 94: 95: 96:
97: public function removeFacet($facet)
98: {
99: $this->_removeField('facetFields', $facet);
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function addFilter($field, $value)
110: {
111: $this->_addFieldArray('searchFilters', $field, $value);
112: return $this;
113: }
114:
115: 116: 117: 118: 119: 120:
121: public function removeFilter($field, $value=null)
122: {
123: $this->_removeFieldArray('searchFilters', $field, $value);
124: return $this;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function addWeight($field, $value)
134: {
135: $this->_addFieldArray('weights', $field, $value);
136: return $this;
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function removeWeight($field, $value=null)
146: {
147: $this->_removeFieldArray('weights', $field, $value);
148: return $this;
149: }
150:
151: 152: 153: 154:
155: public function getQuery()
156: {
157: return clone $this->query;
158: }
159:
160: public function __clone()
161: {
162: $this->query = clone $this->query;
163: }
164:
165: private function _addField($type, $name)
166: {
167: if(!in_array($name, $this->query->$type))
168: $this->query->{$type}[] = $name;
169: }
170:
171: private function _removeField($type, $name)
172: {
173: $key = array_search($name, $this->query->$type);
174: unset($this->query->{$type}[$key]);
175:
176: $this->query->{$type} = array_values($this->query->{$type});
177:
178: }
179:
180: private function _addFieldArray($type, $name, $value)
181: {
182: if(!isset($this->query->{$type}[$name]))
183: $this->query->{$type}[$name] = array();
184:
185: if(is_array($value)) {
186: $this->query->{$type}[$name] = array_merge($this->query->{$type}[$name], $value);
187: } else {
188: $this->query->{$type}[$name][] = $value;
189: }
190: }
191:
192: private function _removeFieldArray($type, $name, $value)
193: {
194: if(!isset($this->query->{$type}[$name]))
195: return;
196:
197: if(is_null($value)) {
198: unset($this->query->{$type}[$name]);
199: } elseif(is_array($value)) {
200: foreach($value as $v)
201: $this->_removeFieldArray($type, $name, $v);
202: } else {
203: $key = array_search($value, $this->query->{$type}[$name]);
204: unset($this->query->{$type}[$name][$key]);
205:
206: $this->query->{$type}[$name] = array_values($this->query->{$type}[$name]);
207: }
208: }
209: }
210: