1 | <?php |
||
15 | abstract class SearchVariant |
||
16 | { |
||
17 | use Configurable; |
||
18 | |||
19 | /** |
||
20 | * Whether this variant is enabled |
||
21 | * |
||
22 | * @config |
||
23 | * @var boolean |
||
24 | */ |
||
25 | private static $enabled = true; |
||
26 | |||
27 | public function __construct() |
||
30 | |||
31 | /*** OVERRIDES start here */ |
||
32 | |||
33 | /** |
||
34 | * Variants can provide any functions they want, but they _must_ override these functions |
||
35 | * with specific ones |
||
36 | */ |
||
37 | |||
38 | /** |
||
39 | * Return false if there is something missing from the environment (probably a |
||
40 | * not installed module) that means this variant can't apply to any class |
||
41 | */ |
||
42 | public function appliesToEnvironment() |
||
46 | |||
47 | /** |
||
48 | * Return true if this variant applies to the passed class & subclass |
||
49 | */ |
||
50 | abstract public function appliesTo($class, $includeSubclasses); |
||
51 | |||
52 | /** |
||
53 | * Return the current state |
||
54 | */ |
||
55 | abstract public function currentState(); |
||
56 | /** |
||
57 | * Return all states to step through to reindex all items |
||
58 | */ |
||
59 | abstract public function reindexStates(); |
||
60 | /** |
||
61 | * Activate the passed state |
||
62 | */ |
||
63 | abstract public function activateState($state); |
||
64 | |||
65 | /** |
||
66 | * Apply this variant to a search query |
||
67 | * |
||
68 | * @param SearchQuery $query |
||
69 | * @param SearchIndex $index |
||
70 | */ |
||
71 | abstract public function alterQuery($query, $index); |
||
72 | |||
73 | /*** OVERRIDES end here*/ |
||
74 | |||
75 | /** Holds a cache of all variants */ |
||
76 | protected static $variants = null; |
||
77 | /** Holds a cache of the variants keyed by "class!" "1"? (1 = include subclasses) */ |
||
78 | protected static $class_variants = array(); |
||
79 | |||
80 | /** |
||
81 | * Returns an array of variants. |
||
82 | * |
||
83 | * With no arguments, returns all variants |
||
84 | * |
||
85 | * With a classname as the first argument, returns the variants that apply to that class |
||
86 | * (optionally including subclasses) |
||
87 | * |
||
88 | * @static |
||
89 | * @param string $class - The class name to get variants for |
||
90 | * @param bool $includeSubclasses - True if variants should be included if they apply to at least one subclass of $class |
||
91 | * @return array - An array of (string)$variantClassName => (Object)$variantInstance pairs |
||
92 | */ |
||
93 | public static function variants($class = null, $includeSubclasses = true) |
||
130 | |||
131 | /** |
||
132 | * Clear the cached variants |
||
133 | */ |
||
134 | public static function clear_variant_cache() |
||
138 | |||
139 | /** Holds a cache of SearchVariant_Caller instances, one for each class/includeSubclasses setting */ |
||
140 | protected static $call_instances = array(); |
||
141 | |||
142 | /** |
||
143 | * Lets you call any function on all variants that support it, in the same manner as "Object#extend" calls |
||
144 | * a method from extensions. |
||
145 | * |
||
146 | * Usage: SearchVariant::with(...)->call($method, $arg1, ...); |
||
147 | * |
||
148 | * @static |
||
149 | * |
||
150 | * @param string $class - (Optional) a classname. If passed, only variants that apply to that class will be checked / called |
||
151 | * |
||
152 | * @param bool $includeSubclasses - (Optional) If false, only variants that apply strictly to the passed class or its super-classes |
||
153 | * will be checked. If true (the default), variants that apply to any sub-class of the passed class with also be checked |
||
154 | * |
||
155 | * @return SearchVariant_Caller An object with one method, call() |
||
156 | */ |
||
157 | public static function with($class = null, $includeSubclasses = true) |
||
168 | |||
169 | /** |
||
170 | * A shortcut to with when calling without passing in a class, |
||
171 | * |
||
172 | * SearchVariant::call(...) ==== SearchVariant::with()->call(...); |
||
173 | */ |
||
174 | |||
175 | public static function call($method, &...$args) |
||
179 | |||
180 | /** |
||
181 | * Get the current state of every variant |
||
182 | * @static |
||
183 | * @return array |
||
184 | */ |
||
185 | public static function current_state($class = null, $includeSubclasses = true) |
||
193 | |||
194 | /** |
||
195 | * Activate all the states in the passed argument |
||
196 | * @static |
||
197 | * @param array $state A set of (string)$variantClass => (any)$state pairs , e.g. as returned by |
||
198 | * SearchVariant::current_state() |
||
199 | * @return void |
||
200 | */ |
||
201 | public static function activate_state($state) |
||
209 | |||
210 | /** |
||
211 | * Return an iterator that, when used in a for loop, activates one combination of reindex states per loop, and restores |
||
212 | * back to the original state at the end |
||
213 | * @static |
||
214 | * @param string $class - The class name to get variants for |
||
215 | * @param bool $includeSubclasses - True if variants should be included if they apply to at least one subclass of $class |
||
216 | * @return SearchVariant_ReindexStateIteratorRet - The iterator to foreach loop over |
||
217 | */ |
||
218 | public static function reindex_states($class = null, $includeSubclasses = true) |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Add new filter field to index safely. |
||
234 | * |
||
235 | * This method will respect existing filters with the same field name that |
||
236 | * correspond to multiple classes |
||
237 | * |
||
238 | * @param SearchIndex $index |
||
239 | * @param string $name Field name |
||
240 | * @param array $field Field spec |
||
241 | */ |
||
242 | protected function addFilterField($index, $name, $field) |
||
258 | |||
259 | /** |
||
260 | * Merge sets of (or individual) class names together for a search index field. |
||
261 | * |
||
262 | * If there is only one unique class name, then just return it as a string instead of array. |
||
263 | * |
||
264 | * @param array|string $left Left class(es) |
||
265 | * @param array|string $right Right class(es) |
||
266 | * @return array|string List of classes, or single class |
||
267 | */ |
||
268 | protected function mergeClasses($left, $right) |
||
285 | } |
||
286 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: