PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Solarium/QueryType/Select/Query/FilterQuery.php

http://github.com/basdenooijer/solarium
PHP | 221 lines | 82 code | 22 blank | 117 comment | 4 complexity | 69e9dff7a04ff2e34e4e6a132290fa5b 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;
  39. use Solarium\Core\Configurable;
  40. use Solarium\Core\Query\Helper;
  41. /**
  42. * Filterquery
  43. *
  44. * @link http://wiki.apache.org/solr/CommonQueryParameters#fq
  45. */
  46. class FilterQuery extends Configurable
  47. {
  48. /**
  49. * Tags for this filterquery
  50. *
  51. * @var array
  52. */
  53. protected $tags = array();
  54. /**
  55. * Query
  56. *
  57. * @var string
  58. */
  59. protected $query;
  60. /**
  61. * Initialize options
  62. *
  63. * @return void
  64. */
  65. protected function init()
  66. {
  67. foreach ($this->options as $name => $value) {
  68. switch ($name) {
  69. case 'tag':
  70. if (!is_array($value)) {
  71. $value = array($value);
  72. }
  73. $this->addTags($value);
  74. break;
  75. case 'key':
  76. $this->setKey($value);
  77. break;
  78. case 'query':
  79. $this->setQuery($value);
  80. break;
  81. }
  82. }
  83. }
  84. /**
  85. * Get key value
  86. *
  87. * @return string
  88. */
  89. public function getKey()
  90. {
  91. return $this->getOption('key');
  92. }
  93. /**
  94. * Set key value
  95. *
  96. * @param string $value
  97. * @return self Provides fluent interface
  98. */
  99. public function setKey($value)
  100. {
  101. return $this->setOption('key', $value);
  102. }
  103. /**
  104. * Set the query string
  105. *
  106. * This overwrites the current value
  107. *
  108. * @param string $query
  109. * @param array $bind Bind values for placeholders in the query string
  110. * @return self Provides fluent interface
  111. */
  112. public function setQuery($query, $bind = null)
  113. {
  114. if (!is_null($bind)) {
  115. $helper = new Helper;
  116. $query = $helper->assemble($query, $bind);
  117. }
  118. $this->query = trim($query);
  119. return $this;
  120. }
  121. /**
  122. * Get the query string
  123. *
  124. * @return string
  125. */
  126. public function getQuery()
  127. {
  128. return $this->query;
  129. }
  130. /**
  131. * Add a tag
  132. *
  133. * @param string $tag
  134. * @return self Provides fluent interface
  135. */
  136. public function addTag($tag)
  137. {
  138. $this->tags[$tag] = true;
  139. return $this;
  140. }
  141. /**
  142. * Add tags
  143. *
  144. * @param array $tags
  145. * @return self Provides fluent interface
  146. */
  147. public function addTags($tags)
  148. {
  149. foreach ($tags as $tag) {
  150. $this->addTag($tag);
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Get all tagss
  156. *
  157. * @return array
  158. */
  159. public function getTags()
  160. {
  161. return array_keys($this->tags);
  162. }
  163. /**
  164. * Remove a tag
  165. *
  166. * @param string $tag
  167. * @return self Provides fluent interface
  168. */
  169. public function removeTag($tag)
  170. {
  171. if (isset($this->tags[$tag])) {
  172. unset($this->tags[$tag]);
  173. }
  174. return $this;
  175. }
  176. /**
  177. * Remove all tags
  178. *
  179. * @return self Provides fluent interface
  180. */
  181. public function clearTags()
  182. {
  183. $this->tags = array();
  184. return $this;
  185. }
  186. /**
  187. * Set multiple tags
  188. *
  189. * This overwrites any existing tags
  190. *
  191. * @param array $tags
  192. * @return self Provides fluent interface
  193. */
  194. public function setTags($tags)
  195. {
  196. $this->clearTags();
  197. return $this->addTags($tags);
  198. }
  199. }