PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Solarium/QueryType/Select/Query/Component/Stats/Stats.php

http://github.com/basdenooijer/solarium
PHP | 361 lines | 146 code | 40 blank | 175 comment | 17 complexity | 1f21556ea4a30d3d8999337a7066e54d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this listof conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation are
  28. * those of the authors and should not be interpreted as representing official
  29. * policies, either expressed or implied, of the copyright holder.
  30. *
  31. * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
  32. * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
  33. * @link http://www.solarium-project.org/
  34. */
  35. /**
  36. * @namespace
  37. */
  38. namespace Solarium\QueryType\Select\Query\Component\Stats;
  39. use Solarium\QueryType\Select\Query\Query as SelectQuery;
  40. use Solarium\QueryType\Select\Query\Component\Component;
  41. use Solarium\QueryType\Select\RequestBuilder\Component\Stats as RequestBuilder;
  42. use Solarium\QueryType\Select\ResponseParser\Component\Stats as ResponseParser;
  43. use Solarium\Exception\InvalidArgumentException;
  44. /**
  45. * Stats component
  46. *
  47. * @link http://wiki.apache.org/solr/StatsComponent
  48. */
  49. class Stats extends Component
  50. {
  51. /**
  52. * Stats facets for all fields
  53. *
  54. * @var array
  55. */
  56. protected $facets = array();
  57. /**
  58. * Fields
  59. *
  60. * @var array
  61. */
  62. protected $fields = array();
  63. /**
  64. * Get component type
  65. *
  66. * @return string
  67. */
  68. public function getType()
  69. {
  70. return SelectQuery::COMPONENT_STATS;
  71. }
  72. /**
  73. * Get a requestbuilder for this query
  74. *
  75. * @return RequestBuilder
  76. */
  77. public function getRequestBuilder()
  78. {
  79. return new RequestBuilder;
  80. }
  81. /**
  82. * Get a response parser for this query
  83. *
  84. * @return ResponseParser
  85. */
  86. public function getResponseParser()
  87. {
  88. return new ResponseParser;
  89. }
  90. /**
  91. * Initialize options
  92. *
  93. * Several options need some extra checks or setup work, for these options
  94. * the setters are called.
  95. *
  96. * @return void
  97. */
  98. protected function init()
  99. {
  100. foreach ($this->options as $name => $value) {
  101. switch ($name) {
  102. case 'field':
  103. $this->setFields($value);
  104. break;
  105. case 'facet':
  106. $this->setFacets($value);
  107. break;
  108. }
  109. }
  110. }
  111. /**
  112. * Create a field instance
  113. *
  114. * If you supply a string as the first arguments ($options) it will be used as the key for the field
  115. * and it will be added to this query component.
  116. * If you supply an options array/object that contains a key the field will also be added to the component.
  117. *
  118. * When no key is supplied the field cannot be added, in that case you will need to add it manually
  119. * after setting the key, by using the addField method.
  120. *
  121. * @param mixed $options
  122. * @return Field
  123. */
  124. public function createField($options = null)
  125. {
  126. if (is_string($options)) {
  127. $fq = new Field;
  128. $fq->setKey($options);
  129. } else {
  130. $fq = new Field($options);
  131. }
  132. if ($fq->getKey() !== null) {
  133. $this->addField($fq);
  134. }
  135. return $fq;
  136. }
  137. /**
  138. * Add a field
  139. *
  140. * Supports a field instance or a config array, in that case a new
  141. * field instance wil be created based on the options.
  142. *
  143. * @throws InvalidArgumentException
  144. * @param Field|array $field
  145. * @return self Provides fluent interface
  146. */
  147. public function addField($field)
  148. {
  149. if (is_array($field)) {
  150. $field = new Field($field);
  151. }
  152. $key = $field->getKey();
  153. if (0 === strlen($key)) {
  154. throw new InvalidArgumentException('A field must have a key value');
  155. }
  156. //double add calls for the same field are ignored, but non-unique keys cause an exception
  157. //@todo add trigger_error with a notice for double add calls?
  158. if (array_key_exists($key, $this->fields) && $this->fields[$key] !== $field) {
  159. throw new InvalidArgumentException('A field must have a unique key value');
  160. } else {
  161. $this->fields[$key] = $field;
  162. }
  163. return $this;
  164. }
  165. /**
  166. * Add multiple fields
  167. *
  168. * @param array $fields
  169. * @return self Provides fluent interface
  170. */
  171. public function addFields(array $fields)
  172. {
  173. foreach ($fields as $key => $field) {
  174. // in case of a config array: add key to config
  175. if (is_array($field) && !isset($field['key'])) {
  176. $field['key'] = $key;
  177. }
  178. $this->addField($field);
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Get a field
  184. *
  185. * @param string $key
  186. * @return string
  187. */
  188. public function getField($key)
  189. {
  190. if (isset($this->fields[$key])) {
  191. return $this->fields[$key];
  192. } else {
  193. return null;
  194. }
  195. }
  196. /**
  197. * Get all fields
  198. *
  199. * @return Field[]
  200. */
  201. public function getFields()
  202. {
  203. return $this->fields;
  204. }
  205. /**
  206. * Remove a single field
  207. *
  208. * You can remove a field by passing its key, or by passing the field instance
  209. *
  210. * @param string|Field $field
  211. * @return self Provides fluent interface
  212. */
  213. public function removeField($field)
  214. {
  215. if (is_object($field)) {
  216. $field = $field->getKey();
  217. }
  218. if (isset($this->fields[$field])) {
  219. unset($this->fields[$field]);
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Remove all fields
  225. *
  226. * @return self Provides fluent interface
  227. */
  228. public function clearFields()
  229. {
  230. $this->fields = array();
  231. return $this;
  232. }
  233. /**
  234. * Set multiple fields
  235. *
  236. * This overwrites any existing fields
  237. *
  238. * @param array $fields
  239. */
  240. public function setFields($fields)
  241. {
  242. $this->clearFields();
  243. $this->addFields($fields);
  244. }
  245. /**
  246. * Specify a facet to return in the resultset
  247. *
  248. * @param string $facet
  249. * @return self Provides fluent interface
  250. */
  251. public function addFacet($facet)
  252. {
  253. $this->facets[$facet] = true;
  254. return $this;
  255. }
  256. /**
  257. * Specify multiple facets to return in the resultset
  258. *
  259. * @param string|array $facets can be an array or string with comma
  260. * separated facetnames
  261. *
  262. * @return self Provides fluent interface
  263. */
  264. public function addFacets($facets)
  265. {
  266. if (is_string($facets)) {
  267. $facets = explode(',', $facets);
  268. $facets = array_map('trim', $facets);
  269. }
  270. foreach ($facets as $facet) {
  271. $this->addFacet($facet);
  272. }
  273. return $this;
  274. }
  275. /**
  276. * Remove a facet from the facet list
  277. *
  278. * @param string $facet
  279. * @return self Provides fluent interface
  280. */
  281. public function removeFacet($facet)
  282. {
  283. if (isset($this->facets[$facet])) {
  284. unset($this->facets[$facet]);
  285. }
  286. return $this;
  287. }
  288. /**
  289. * Remove all facets from the facet list.
  290. *
  291. * @return self Provides fluent interface
  292. */
  293. public function clearFacets()
  294. {
  295. $this->facets = array();
  296. return $this;
  297. }
  298. /**
  299. * Get the list of facets
  300. *
  301. * @return array
  302. */
  303. public function getFacets()
  304. {
  305. return array_keys($this->facets);
  306. }
  307. /**
  308. * Set multiple facets
  309. *
  310. * This overwrites any existing facets
  311. *
  312. * @param array $facets
  313. * @return self Provides fluent interface
  314. */
  315. public function setFacets($facets)
  316. {
  317. $this->clearFacets();
  318. $this->addFacets($facets);
  319. return $this;
  320. }
  321. }