1 | <?php |
||
17 | abstract class AbstractSearch implements |
||
18 | SearchInterface, |
||
19 | LoggerAwareInterface |
||
20 | { |
||
21 | use LoggerAwareTrait; |
||
22 | |||
23 | /** |
||
24 | * The (raw) search results. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $results; |
||
29 | |||
30 | /** |
||
31 | * Store the factory instance for the current class. |
||
32 | * |
||
33 | * @var FactoryInterface |
||
34 | */ |
||
35 | private $modelFactory; |
||
36 | |||
37 | /** |
||
38 | * Return a new search object. |
||
39 | * |
||
40 | * @param array|\ArrayAccess $data The class options and dependencies. |
||
41 | */ |
||
42 | public function __construct($data) |
||
47 | |||
48 | /** |
||
49 | * Set an object model factory. |
||
50 | * |
||
51 | * @param FactoryInterface $factory The model factory, to create objects. |
||
52 | * @return self |
||
53 | */ |
||
54 | protected function setModelFactory(FactoryInterface $factory) |
||
60 | |||
61 | /** |
||
62 | * Retrieve the object model factory. |
||
63 | * |
||
64 | * @throws RuntimeException If the model factory was not previously set. |
||
65 | * @return FactoryInterface |
||
66 | */ |
||
67 | public function modelFactory() |
||
77 | |||
78 | /** |
||
79 | * Process the search query. |
||
80 | * |
||
81 | * @param string $keyword The search term(s). |
||
82 | * @param array $searchOptions Additional options. |
||
83 | * @return array|\Traversable The results. |
||
84 | */ |
||
85 | abstract public function search($keyword, array $searchOptions = []); |
||
86 | |||
87 | /** |
||
88 | * Alias of {@see self::search()}. |
||
89 | * |
||
90 | * A search is always callable. |
||
91 | * |
||
92 | * @param string $keyword The search term(s). |
||
93 | * @param array $searchOptions Additional options. |
||
94 | * @return array The results. |
||
95 | */ |
||
96 | final public function __invoke($keyword, array $searchOptions = []) |
||
100 | |||
101 | /** |
||
102 | * Retrieve the results from the latest search. |
||
103 | * |
||
104 | * @param string $resultType The type of results to search. Can be only "raw" for now. |
||
105 | * @throws RuntimeException If this method is called before a search was executed. |
||
106 | * @return array The results from the last search operation. |
||
107 | */ |
||
108 | final public function lastResults($resultType = 'raw') |
||
122 | |||
123 | /** |
||
124 | * Determine if the latest search has any results. |
||
125 | * |
||
126 | * @return boolean |
||
127 | */ |
||
128 | final public function hasResults() |
||
132 | |||
133 | /** |
||
134 | * Set the results from a search. |
||
135 | * |
||
136 | * @param array $results The (raw) search results. |
||
137 | * @return void |
||
138 | */ |
||
139 | final protected function setResults(array $results) |
||
143 | } |
||
144 |