1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Search; |
4
|
|
|
|
5
|
|
|
use \DateTime; |
6
|
|
|
use \DateTimeInterface; |
7
|
|
|
use \InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
// Module `charcoal-core` dependencies |
10
|
|
|
use \Charcoal\Model\AbstractModel; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Search logs should be saved every time a client initiates a search request. |
14
|
|
|
*/ |
15
|
|
|
class SearchLog extends AbstractModel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The search identifier this specific search log belongs to. |
19
|
|
|
* @var string $searchIdent |
20
|
|
|
*/ |
21
|
|
|
private $searchIdent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The searched keyword. |
25
|
|
|
* @var string $keyword |
26
|
|
|
*/ |
27
|
|
|
private $keyword; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Number of search results. |
31
|
|
|
* @var array $numResults |
32
|
|
|
*/ |
33
|
|
|
private $numResults; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Detailed results, if available. |
37
|
|
|
* @var array $results |
38
|
|
|
*/ |
39
|
|
|
private $results; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Timestamp of the search (date-time when the search was performed). |
43
|
|
|
* @var DateTimeInterface |
44
|
|
|
*/ |
45
|
|
|
private $ts; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Client IP. |
49
|
|
|
* @var string $ip |
50
|
|
|
*/ |
51
|
|
|
private $ip; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Client session ID, if any. |
55
|
|
|
* @var string $sessionId |
56
|
|
|
*/ |
57
|
|
|
private $sessionId; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The language code. |
61
|
|
|
* @var string $lang |
62
|
|
|
*/ |
63
|
|
|
private $lang; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The search origin; an identifier representing where the search was executed from. |
67
|
|
|
* @var string $origin |
68
|
|
|
*/ |
69
|
|
|
private $origin; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $ident The search identifier. |
73
|
|
|
* @throws InvalidArgumentException If the search ident is not a string. |
74
|
|
|
* @return SearchLog Chainable |
75
|
|
|
*/ |
76
|
|
|
public function setSearchIdent($ident) |
77
|
|
|
{ |
78
|
|
|
if (!is_string($ident)) { |
79
|
|
|
throw new InvalidArgumentException( |
80
|
|
|
'Search ident must be a string.' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
$this->searchIdent = $ident; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function searchIdent() |
91
|
|
|
{ |
92
|
|
|
return $this->searchIdent; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $kw The searched term / keyword. |
97
|
|
|
* @throws InvalidArgumentException If the keyword is not a string. |
98
|
|
|
* @return SearchLog Chainable |
99
|
|
|
*/ |
100
|
|
|
public function setKeyword($kw) |
101
|
|
|
{ |
102
|
|
|
if (!is_string($kw)) { |
103
|
|
|
throw new InvalidArgumentException( |
104
|
|
|
'Keyword must be a string' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
$this->keyword = $kw; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function keyword() |
115
|
|
|
{ |
116
|
|
|
return $this->keyword; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param integer $num The number of results from search. |
121
|
|
|
* @return SearchLog Chainable |
122
|
|
|
*/ |
123
|
|
|
public function setNumResults($num) |
124
|
|
|
{ |
125
|
|
|
$this->numResults = (int)$num; |
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return integer |
131
|
|
|
*/ |
132
|
|
|
public function numResults() |
133
|
|
|
{ |
134
|
|
|
return $this->numResults; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param mixed $results The search results data, if available. |
139
|
|
|
* @throws InvalidArgumentException If the results is not an array or invalid JSON. |
140
|
|
|
* @return SearchLog Chainable |
141
|
|
|
*/ |
142
|
|
|
public function setResults($results) |
143
|
|
|
{ |
144
|
|
|
if ($results === null) { |
145
|
|
|
$this->results = null; |
|
|
|
|
146
|
|
|
} elseif (is_string($results)) { |
147
|
|
|
$this->results = json_decode($results, true); |
|
|
|
|
148
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
149
|
|
|
throw new InvalidArgumentException( |
150
|
|
|
sprintf('Invalid JSON for search results: "%s"', $results) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} elseif (is_array($results)) { |
154
|
|
|
$this->results = $results; |
155
|
|
|
} else { |
156
|
|
|
throw new InvalidArgumentException( |
157
|
|
|
'Invalid search results type. Must be a JSON string, an array or null.' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function results() |
167
|
|
|
{ |
168
|
|
|
return $this->results; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param DateTimeInterface|string|null $ts The timestamp (date-time the search occured). |
173
|
|
|
* @throws InvalidArgumentException If ts is not a valid date-time. |
174
|
|
|
* @return SearchLog Chainable |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
public function setTs($ts) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
if ($ts === null) { |
179
|
|
|
$this->ts = null; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
if (is_string($ts)) { |
183
|
|
|
$ts = new DateTime($ts); |
184
|
|
|
} |
185
|
|
|
if (!($ts instanceof DateTimeInterface)) { |
186
|
|
|
throw new InvalidArgumentException( |
187
|
|
|
'Invalid "ts" value. Must be a date/time string or a DateTime object.' |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
$this->ts = $ts; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return DateTime |
196
|
|
|
*/ |
197
|
|
|
public function ts() |
198
|
|
|
{ |
199
|
|
|
return $this->ts; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $ip The IP address of the client that searched. |
204
|
|
|
* @return SearchLog Chainable |
205
|
|
|
*/ |
206
|
|
|
public function setIp($ip) |
207
|
|
|
{ |
208
|
|
|
$this->ip = $ip; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function ip() |
216
|
|
|
{ |
217
|
|
|
return $this->ip; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $sessionId The session identifier. Typically, `session_id()`. |
222
|
|
|
* @throws InvalidArgumentException If the session id is not a string. |
223
|
|
|
* @return SearchLog Chainable |
224
|
|
|
*/ |
225
|
|
|
public function setSessionId($sessionId) |
226
|
|
|
{ |
227
|
|
|
if ($sessionId === null) { |
228
|
|
|
$this->sessionId = null; |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
if (!is_string($sessionId)) { |
232
|
|
|
throw new InvalidArgumentException( |
233
|
|
|
'Can not set search log\'s session Id: must be a string.' |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
$this->sessionId = $sessionId; |
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function sessionId() |
244
|
|
|
{ |
245
|
|
|
return $this->sessionId; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param string $lang The language code. |
250
|
|
|
* @return SearchLog Chainable |
251
|
|
|
*/ |
252
|
|
|
public function setLang($lang) |
253
|
|
|
{ |
254
|
|
|
$this->lang = $lang; |
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function lang() |
262
|
|
|
{ |
263
|
|
|
return $this->lang; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $origin The origin is an identifier representing where the search was executed. |
268
|
|
|
* @throws InvalidArgumentException If the origin is not a string. |
269
|
|
|
* @return SearchLog Chainable |
270
|
|
|
*/ |
271
|
|
|
public function setOrigin($origin) |
272
|
|
|
{ |
273
|
|
|
if (!is_string($origin) && $origin !== null) { |
274
|
|
|
throw new InvalidArgumentException( |
275
|
|
|
'Origin must be a string' |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$this->origin = $origin; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
public function origin() |
288
|
|
|
{ |
289
|
|
|
return $this->origin; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return boolean |
294
|
|
|
*/ |
295
|
|
|
public function preSave() |
296
|
|
|
{ |
297
|
|
|
parent::preSave(); |
298
|
|
|
|
299
|
|
|
$this->setIp(getenv('REMOTE_ADDR') ? getenv('REMOTE_ADDR') : ''); |
300
|
|
|
$this->setTs('now'); |
301
|
|
|
if (session_id()) { |
302
|
|
|
$this->setSessionId(session_id()); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (!isset($this->lang)) { |
306
|
|
|
$this->setLang(''); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return true; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..