1 | <?php |
||
19 | class Search |
||
20 | { |
||
21 | protected $index; |
||
22 | protected $query; |
||
23 | private $path; |
||
24 | private $limit = 25; |
||
25 | |||
26 | /** |
||
27 | * Search constructor. |
||
28 | * |
||
29 | * @param Index $index |
||
30 | * @param Query $query |
||
31 | */ |
||
32 | 6 | public function __construct(Index $index, Query $query) |
|
33 | { |
||
34 | 6 | $this->index = $index; |
|
35 | 6 | $this->query = $query; |
|
36 | 6 | } |
|
37 | |||
38 | /** |
||
39 | * @param $name |
||
40 | * @param $arguments |
||
41 | * @return $this |
||
42 | * @throws \BadMethodCallException |
||
43 | */ |
||
44 | 2 | public function __call($name, $arguments) |
|
45 | { |
||
46 | 2 | if (method_exists($this->query, $name)) { |
|
47 | 1 | $this->query->$name($arguments); |
|
48 | 1 | return $this; |
|
49 | } |
||
50 | |||
51 | 1 | throw new \BadMethodCallException; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param $limit |
||
56 | * @return $this |
||
57 | */ |
||
58 | 1 | public function limit($limit) |
|
59 | { |
||
60 | 1 | $this->limit = $limit; |
|
61 | 1 | return $this; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param bool $path |
||
66 | * @return $this |
||
67 | */ |
||
68 | 1 | public function path($path = false) |
|
69 | { |
||
70 | 1 | $this->path = $path; |
|
71 | 1 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $string |
||
76 | * @return $this |
||
77 | */ |
||
78 | 1 | public function raw($string) |
|
79 | { |
||
80 | 1 | $this->query->add(QueryParser::parse($string)); |
|
81 | 1 | return $this; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param $string |
||
86 | * @param bool $field |
||
87 | * @param null $offsets |
||
88 | * @return $this |
||
89 | * @return $this |
||
90 | */ |
||
91 | 1 | public function phrase($string, $field = false, $offsets = null) |
|
92 | { |
||
93 | 1 | $this->query->add(new Phrase(explode(' ', $string), $offsets, $field)); |
|
|
|||
94 | 1 | return $this; |
|
95 | |||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param $string |
||
100 | * @param bool $field |
||
101 | * @return $this |
||
102 | */ |
||
103 | 1 | public function fuzzy($string, $field = false) |
|
104 | { |
||
105 | 1 | $this->query->add(new Fuzzy($this->term($field, $string))); |
|
106 | 1 | return $this; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $string |
||
111 | * @param bool $field |
||
112 | * @return Term |
||
113 | */ |
||
114 | 1 | protected function term($string, $field = false) |
|
115 | { |
||
116 | 1 | return new Term(strtoupper($string), $field); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param $string |
||
121 | * @param bool $field |
||
122 | * @param array $options |
||
123 | */ |
||
124 | 1 | public function wildcard($string, $field = false, $options = [ ]) |
|
125 | { |
||
126 | 1 | $this->query->add(new Wildcard($this->term($field, $string), $options)); |
|
127 | 1 | return $this; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param $string |
||
132 | * @param bool|string $field |
||
133 | * @return $this|bool |
||
134 | * @todo Work out why the search only works if the string is uppercase... |
||
135 | */ |
||
136 | 1 | public function where($string, $field = false) |
|
137 | { |
||
138 | 1 | is_array($field) |
|
139 | 1 | ? $this->multiTerm($this->mapWhereArray($string, $field)) |
|
140 | 1 | : $this->query->add($this->singleTerm($string, $field)); |
|
141 | |||
142 | 1 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param array $terms |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function multiTerm(array $terms) |
||
150 | { |
||
151 | $multiTerm = new MultiTerm; |
||
152 | foreach ($terms as $field => $value) { |
||
153 | $multiTerm->addTerm($this->term($value, $field), $this->query->getSign()); |
||
154 | } |
||
155 | |||
156 | $this->query->add($multiTerm); |
||
157 | |||
158 | return $this; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param $string |
||
163 | * @param array $array |
||
164 | * @return mixed |
||
165 | * @todo abstract this out |
||
166 | */ |
||
167 | private function mapWhereArray($string, array $array) |
||
168 | { |
||
169 | return array_map( |
||
170 | function() use ($string) { |
||
171 | return $string; |
||
172 | }, array_flip($array) |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param $string |
||
178 | * @param $field |
||
179 | * @return QueryTerm |
||
180 | */ |
||
181 | 1 | public function singleTerm($string, $field = false) |
|
182 | { |
||
183 | 1 | return new QueryTerm($this->term($string, $field)); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function hits() |
||
190 | { |
||
191 | return $this->mapIds( |
||
192 | $this->index->limit($this->limit)->open($this->path)->get()->find($this->query->getBoolean()) |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param array $array |
||
198 | * @return mixed |
||
199 | * @todo abstract this out |
||
200 | */ |
||
201 | private function mapIds(array $array) |
||
209 | } |
||
210 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: