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 | * @package Solarium |
---|
36 | * @subpackage Query |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Facet base class |
---|
41 | * |
---|
42 | * @link http://wiki.apache.org/solr/SimpleFacetParameters |
---|
43 | * |
---|
44 | * @package Solarium |
---|
45 | * @subpackage Query |
---|
46 | */ |
---|
47 | abstract class Solarium_Query_Select_Component_Facet extends Solarium_Configurable |
---|
48 | { |
---|
49 | |
---|
50 | /** |
---|
51 | * Exclude tags for this facet |
---|
52 | * |
---|
53 | * @var array |
---|
54 | */ |
---|
55 | protected $_excludes = array(); |
---|
56 | |
---|
57 | /** |
---|
58 | * Must be implemented by the facet types and return one of the constants |
---|
59 | * |
---|
60 | * @abstract |
---|
61 | * @return string |
---|
62 | */ |
---|
63 | abstract public function getType(); |
---|
64 | |
---|
65 | /** |
---|
66 | * Initialize options |
---|
67 | * |
---|
68 | * @return void |
---|
69 | */ |
---|
70 | protected function _init() |
---|
71 | { |
---|
72 | foreach ($this->_options AS $name => $value) { |
---|
73 | switch ($name) { |
---|
74 | case 'key': |
---|
75 | $this->setKey($value); |
---|
76 | break; |
---|
77 | case 'exclude': |
---|
78 | if(!is_array($value)) $value = array($value); |
---|
79 | $this->setExcludes($value); |
---|
80 | unset($this->_options['exclude']); |
---|
81 | break; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Get key value |
---|
88 | * |
---|
89 | * @return string |
---|
90 | */ |
---|
91 | public function getKey() |
---|
92 | { |
---|
93 | return $this->getOption('key'); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Set key value |
---|
98 | * |
---|
99 | * @param string $value |
---|
100 | * @return Solarium_Query_Select_Component_Facet Provides fluent interface |
---|
101 | */ |
---|
102 | public function setKey($value) |
---|
103 | { |
---|
104 | return $this->_setOption('key', $value); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Add an exclude tag |
---|
109 | * |
---|
110 | * @param string $tag |
---|
111 | * @return Solarium_Query_Select_Component_Facet Provides fluent interface |
---|
112 | */ |
---|
113 | public function addExclude($tag) |
---|
114 | { |
---|
115 | $this->_excludes[$tag] = true; |
---|
116 | return $this; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Add multiple exclude tags |
---|
121 | * |
---|
122 | * @param array $excludes |
---|
123 | * @return Solarium_Query_Select_Component_Facet Provides fluent interface |
---|
124 | */ |
---|
125 | public function addExcludes(array $excludes) |
---|
126 | { |
---|
127 | foreach ($excludes AS $exclude) { |
---|
128 | $this->addExclude($exclude); |
---|
129 | } |
---|
130 | |
---|
131 | return $this; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Get all excludes |
---|
136 | * |
---|
137 | * @return array |
---|
138 | */ |
---|
139 | public function getExcludes() |
---|
140 | { |
---|
141 | return array_keys($this->_excludes); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Remove a single exclude tag |
---|
146 | * |
---|
147 | * @param string $exclude |
---|
148 | * @return Solarium_Query_Select_Component_Facet Provides fluent interface |
---|
149 | */ |
---|
150 | public function removeExclude($exclude) |
---|
151 | { |
---|
152 | if (isset($this->_excludes[$exclude])) { |
---|
153 | unset($this->_excludes[$exclude]); |
---|
154 | } |
---|
155 | |
---|
156 | return $this; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Remove all excludes |
---|
161 | * |
---|
162 | * @return Solarium_Query_Select_Facet Provides fluent interface |
---|
163 | */ |
---|
164 | public function clearExcludes() |
---|
165 | { |
---|
166 | $this->_excludes = array(); |
---|
167 | return $this; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Set multiple excludes |
---|
172 | * |
---|
173 | * This overwrites any existing excludes |
---|
174 | * |
---|
175 | * @param array $excludes |
---|
176 | */ |
---|
177 | public function setExcludes($excludes) |
---|
178 | { |
---|
179 | $this->clearExcludes(); |
---|
180 | $this->addExcludes($excludes); |
---|
181 | } |
---|
182 | } |
---|