| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Encpsulates the supported search techniques. |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
*/ |
| 19 |
class VBOChatSearch |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The pagination offset. |
| 23 |
* |
| 24 |
* @var int|null |
| 25 |
*/ |
| 26 |
protected $start = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* The pagination limit. |
| 30 |
* |
| 31 |
* @var int|null |
| 32 |
*/ |
| 33 |
protected $limit = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Searches the messages by context. |
| 37 |
* |
| 38 |
* @var VBOChatContext|null |
| 39 |
*/ |
| 40 |
protected $context = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Searches the messages by ID. |
| 44 |
* The object contains the "id" and "operator" properties. |
| 45 |
* |
| 46 |
* @var object|null |
| 47 |
*/ |
| 48 |
protected $message = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Searches the messages by sender ID. |
| 52 |
* The object contains the "id" and "operator" properties. |
| 53 |
* |
| 54 |
* @var object|null |
| 55 |
*/ |
| 56 |
protected $sender = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Searches the messages by date. |
| 60 |
* The object contains the "utc" and "operator" properties. |
| 61 |
* |
| 62 |
* @var object|null |
| 63 |
*/ |
| 64 |
protected $date = null; |
| 65 |
|
| 66 |
/** |
| 67 |
* The ID of the user that is requesting the search. |
| 68 |
* |
| 69 |
* @var int|null |
| 70 |
*/ |
| 71 |
protected $readerId = null; |
| 72 |
|
| 73 |
/** |
| 74 |
* Whether the system should return only the unread messages. |
| 75 |
* |
| 76 |
* @var bool |
| 77 |
*/ |
| 78 |
protected $unread = false; |
| 79 |
|
| 80 |
/** |
| 81 |
* Whether the results should be aggregated per message. |
| 82 |
* |
| 83 |
* @var bool |
| 84 |
*/ |
| 85 |
protected $aggregate = false; |
| 86 |
|
| 87 |
/** |
| 88 |
* Sets the specified pagination offset. |
| 89 |
* |
| 90 |
* @param int $start |
| 91 |
* |
| 92 |
* @return self |
| 93 |
*/ |
| 94 |
public function start(?int $start) |
| 95 |
{ |
| 96 |
$this->start = is_null($start) ? null : abs($start); |
| 97 |
|
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Checks whether a custom start position has been specified. |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public function hasStart() |
| 107 |
{ |
| 108 |
return $this->start !== null; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the specified pagination offset (0 if not provided). |
| 113 |
* |
| 114 |
* @return int |
| 115 |
*/ |
| 116 |
public function getStart() |
| 117 |
{ |
| 118 |
return $this->start ?: 0; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Sets the specified pagination limit. |
| 123 |
* |
| 124 |
* @param int|null $limit |
| 125 |
* |
| 126 |
* @return self |
| 127 |
*/ |
| 128 |
public function limit(?int $limit) |
| 129 |
{ |
| 130 |
$this->limit = is_null($limit) ? null : abs($limit); |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Checks whether a custom limit has been specified. |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function hasLimit() |
| 141 |
{ |
| 142 |
return $this->limit !== null; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the specified pagination limit (0 if not provided). |
| 147 |
* |
| 148 |
* @return int|null |
| 149 |
*/ |
| 150 |
public function getLimit() |
| 151 |
{ |
| 152 |
return $this->limit; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Sets the specified context parent. |
| 157 |
* |
| 158 |
* @param VBOChatContext $context |
| 159 |
* |
| 160 |
* @return self |
| 161 |
*/ |
| 162 |
public function withContext(?VBOChatContext $context) |
| 163 |
{ |
| 164 |
$this->context = $context; |
| 165 |
|
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Checks whether a custom context has been specified. |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
public function hasContext() |
| 175 |
{ |
| 176 |
return $this->context !== null; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the specified context parent, if any. |
| 181 |
* |
| 182 |
* @return VBOChatContext|null |
| 183 |
*/ |
| 184 |
public function getContext() |
| 185 |
{ |
| 186 |
return $this->context; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets the specified message ID. |
| 191 |
* |
| 192 |
* @param int $messageId The ID of the message. |
| 193 |
* @param string $operator Used to set the comparison operator. |
| 194 |
* |
| 195 |
* @return self |
| 196 |
*/ |
| 197 |
public function message(?int $messageId, string $operator = '=') |
| 198 |
{ |
| 199 |
if ($messageId !== null) { |
| 200 |
$this->message = new stdClass; |
| 201 |
$this->message->id = $messageId; |
| 202 |
$this->message->operator = $operator; |
| 203 |
} else { |
| 204 |
$this->message = null; |
| 205 |
} |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Checks whether a custom message has been specified. |
| 212 |
* |
| 213 |
* @return bool |
| 214 |
*/ |
| 215 |
public function hasMessage() |
| 216 |
{ |
| 217 |
return $this->message !== null; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the specified message, if any. |
| 222 |
* |
| 223 |
* @return object|null |
| 224 |
*/ |
| 225 |
public function getMessage() |
| 226 |
{ |
| 227 |
return $this->message; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Sets the specified sender ID. |
| 232 |
* |
| 233 |
* @param int $senderId The ID of the sender. |
| 234 |
* @param bool $equals Whether the search should match the ID. |
| 235 |
* |
| 236 |
* @return self |
| 237 |
*/ |
| 238 |
public function sender(?int $senderId, bool $equals = true) |
| 239 |
{ |
| 240 |
if ($senderId !== null) { |
| 241 |
$this->sender = new stdClass; |
| 242 |
$this->sender->id = $senderId; |
| 243 |
$this->sender->operator = $equals ? '=' : '<>'; |
| 244 |
} else { |
| 245 |
$this->sender = null; |
| 246 |
} |
| 247 |
|
| 248 |
return $this; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Checks whether a custom sender has been specified. |
| 253 |
* |
| 254 |
* @return bool |
| 255 |
*/ |
| 256 |
public function hasSender() |
| 257 |
{ |
| 258 |
return $this->sender !== null; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Returns the specified sender, if any. |
| 263 |
* |
| 264 |
* @return object|null |
| 265 |
*/ |
| 266 |
public function getSender() |
| 267 |
{ |
| 268 |
return $this->sender; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Sets the specified date. |
| 273 |
* |
| 274 |
* @param mixed $date The search date. |
| 275 |
* @param string $operator Used to set the comparison operator. |
| 276 |
* |
| 277 |
* @return self |
| 278 |
*/ |
| 279 |
public function date($date, string $operator = '=') |
| 280 |
{ |
| 281 |
if ($date !== null) { |
| 282 |
if (is_scalar($date)) { |
| 283 |
$date = JFactory::getDate($date)->toSql(); |
| 284 |
} |
| 285 |
|
| 286 |
$this->date = new stdClass; |
| 287 |
$this->date->utc = $date; |
| 288 |
$this->date->operator = $operator; |
| 289 |
} else { |
| 290 |
$this->date = null; |
| 291 |
} |
| 292 |
|
| 293 |
return $this; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Checks whether a custom date has been specified. |
| 298 |
* |
| 299 |
* @return bool |
| 300 |
*/ |
| 301 |
public function hasDate() |
| 302 |
{ |
| 303 |
return $this->date !== null; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Returns the specified date, if any. |
| 308 |
* |
| 309 |
* @return object|null |
| 310 |
*/ |
| 311 |
public function getDate() |
| 312 |
{ |
| 313 |
return $this->date; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Sets the specified reader ID. |
| 318 |
* |
| 319 |
* @param int|null $readerId |
| 320 |
* |
| 321 |
* @return self |
| 322 |
*/ |
| 323 |
public function reader(?int $readerId) |
| 324 |
{ |
| 325 |
$this->readerId = is_null($readerId) ? null : abs($readerId); |
| 326 |
|
| 327 |
return $this; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Checks whether a custom reader has been specified. |
| 332 |
* |
| 333 |
* @return bool |
| 334 |
*/ |
| 335 |
public function hasReader() |
| 336 |
{ |
| 337 |
return $this->readerId !== null; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Returns the specified reader. |
| 342 |
* |
| 343 |
* @return int|null |
| 344 |
*/ |
| 345 |
public function getReader() |
| 346 |
{ |
| 347 |
return $this->readerId; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Sets whether only unread messages should be returned. |
| 352 |
* |
| 353 |
* @param bool $unread |
| 354 |
* |
| 355 |
* @return self |
| 356 |
*/ |
| 357 |
public function unread(bool $unread = true) |
| 358 |
{ |
| 359 |
$this->unread = $unread; |
| 360 |
|
| 361 |
return $this; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Checks whether only unread messages should be returned. |
| 366 |
* |
| 367 |
* @return bool |
| 368 |
*/ |
| 369 |
public function hasUnread() |
| 370 |
{ |
| 371 |
return $this->unread; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Sets whether the messages should be aggregated. |
| 376 |
* |
| 377 |
* @param bool $aggregate |
| 378 |
* |
| 379 |
* @return self |
| 380 |
*/ |
| 381 |
public function aggregate(bool $aggregate = true) |
| 382 |
{ |
| 383 |
$this->aggregate = $aggregate; |
| 384 |
|
| 385 |
return $this; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Checks whether the messages should be aggregated. |
| 390 |
* |
| 391 |
* @return bool |
| 392 |
*/ |
| 393 |
public function hasAggregate() |
| 394 |
{ |
| 395 |
return $this->aggregate; |
| 396 |
} |
| 397 |
} |
| 398 |
|