Overview

Namespaces

  • PHP
  • vierbergenlars
    • Forage
      • ODM
        • HydrationSettings
      • QueryParser
      • SearchIndex
      • SearchQuery
      • SearchResult
      • Transport

Classes

  • Query
  • QueryBuilder
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace vierbergenlars\Forage\SearchQuery;
  4: 
  5: use vierbergenlars\Forage\SearchQuery\Query;
  6: /**
  7:  * Helps building search queries
  8:  */
  9: class QueryBuilder
 10: {
 11:     /**
 12:      * The query that is under construction
 13:      * @var \vierbergenlars\Forage\SearchQuery\Query
 14:      */
 15:     protected $query;
 16: 
 17:     /**
 18:      * Creates a new query builder
 19:      * @param \vierbergenlars\Forage\SearchQuery\Query $query Override the query object that is built
 20:      */
 21:     public function __construct(Query $query)
 22:     {
 23:             $this->query = $query;
 24:     }
 25: 
 26:     /**
 27:      * Sets the search query
 28:      * @param string $query
 29:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 30:      */
 31:     public function setSearchQuery($query)
 32:     {
 33:         $this->query->setQuery($query);
 34:         return $this;
 35:     }
 36: 
 37:     /**
 38:      * Sets the offset for the search query
 39:      * @param int $offset
 40:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 41:      */
 42:     public function setOffset($offset)
 43:     {
 44:         $this->query->setOffset($offset);
 45:         return $this;
 46:     }
 47: 
 48:     /**
 49:      * Sets the limit for the search query
 50:      * @param int $limit
 51:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 52:      */
 53:     public function setLimit($limit)
 54:     {
 55:         $this->query->setLimit($limit);
 56:         return $this;
 57:     }
 58: 
 59:     /**
 60:      * Adds a new field to search in
 61:      * @param string $field
 62:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 63:      */
 64:     public function addSearchField($field)
 65:     {
 66:         $this->_addField('searchFields', $field);
 67:         return $this;
 68:     }
 69: 
 70:     /**
 71:      * Removes a field from the list to search in
 72:      * @param string $field
 73:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 74:      */
 75:     public function removeSearchField($field)
 76:     {
 77:         $this->_removeField('searchFields', $field);
 78:         return $this;
 79:     }
 80: 
 81:     /**
 82:      * Adds a new facet
 83:      * @param string $facet
 84:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 85:      */
 86:     public function addFacet($facet)
 87:     {
 88:         $this->_addField('facetFields', $facet);
 89:         return $this;
 90:     }
 91: 
 92:     /**
 93:      * Removes a facet
 94:      * @param string $facet
 95:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
 96:      */
 97:     public function removeFacet($facet)
 98:     {
 99:         $this->_removeField('facetFields', $facet);
100:         return $this;
101:     }
102: 
103:     /**
104:      * Adds a filter on a field
105:      * @param string $field The field to add a filter to
106:      * @param string|array $value The value(s) to limit the field to
107:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
108:      */
109:     public function addFilter($field, $value)
110:     {
111:         $this->_addFieldArray('searchFilters', $field, $value);
112:         return $this;
113:     }
114: 
115:     /**
116:      * Removes a filter on a field
117:      * @param string $field The field to remove a filter from
118:      * @param string|array|null $value The values to remove from the filter. If `null`, remove all filters on the field.
119:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
120:      */
121:     public function removeFilter($field, $value=null)
122:     {
123:         $this->_removeFieldArray('searchFilters', $field, $value);
124:         return $this;
125:     }
126: 
127:     /**
128:      * Adds a weight to a field
129:      * @param string $field
130:      * @param int|array $value
131:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
132:      */
133:     public function addWeight($field, $value)
134:     {
135:         $this->_addFieldArray('weights', $field, $value);
136:         return $this;
137:     }
138: 
139:     /**
140:      * Removes a weight from a field
141:      * @param string $field
142:      * @param int|array $value
143:      * @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
144:      */
145:     public function removeWeight($field, $value=null)
146:     {
147:         $this->_removeFieldArray('weights', $field, $value);
148:         return $this;
149:     }
150: 
151:     /**
152:      * Gets the completed query
153:      * @return \vierbergenlars\Forage\SearchQuery\Query
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:         // Reset array to numeric order
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:             // Reset array keys to numeric order
206:             $this->query->{$type}[$name] = array_values($this->query->{$type}[$name]);
207:         }
208:     }
209: }
210: 
Forage-PHP-Client API documentation generated by ApiGen 2.8.0