@@ -50,942 +50,942 @@ |
||
| 50 | 50 | class IndexDocument implements IIndexDocument, JsonSerializable { |
| 51 | 51 | |
| 52 | 52 | |
| 53 | - /** @var string */ |
|
| 54 | - protected $id = ''; |
|
| 53 | + /** @var string */ |
|
| 54 | + protected $id = ''; |
|
| 55 | 55 | |
| 56 | - /** @var string */ |
|
| 57 | - protected $providerId = ''; |
|
| 56 | + /** @var string */ |
|
| 57 | + protected $providerId = ''; |
|
| 58 | 58 | |
| 59 | - /** @var DocumentAccess */ |
|
| 60 | - protected $access; |
|
| 61 | - |
|
| 62 | - /** @var IIndex */ |
|
| 63 | - protected $index; |
|
| 64 | - |
|
| 65 | - /** @var int */ |
|
| 66 | - protected $modifiedTime = 0; |
|
| 67 | - |
|
| 68 | - /** @var string */ |
|
| 69 | - protected $source = ''; |
|
| 70 | - |
|
| 71 | - /** @var array */ |
|
| 72 | - protected $tags = []; |
|
| 73 | - |
|
| 74 | - /** @var array */ |
|
| 75 | - protected $metaTags = []; |
|
| 76 | - |
|
| 77 | - /** @var array */ |
|
| 78 | - protected $subTags = []; |
|
| 79 | - |
|
| 80 | - /** @var string */ |
|
| 81 | - protected $title = ''; |
|
| 82 | - |
|
| 83 | - /** @var string */ |
|
| 84 | - protected $content = ''; |
|
| 85 | - |
|
| 86 | - /** @var string */ |
|
| 87 | - protected $hash = ''; |
|
| 88 | - |
|
| 89 | - /** @var array */ |
|
| 90 | - protected $parts = []; |
|
| 91 | - |
|
| 92 | - /** @var string */ |
|
| 93 | - protected $link = ''; |
|
| 94 | - |
|
| 95 | - /** @var array */ |
|
| 96 | - protected $more = []; |
|
| 97 | - |
|
| 98 | - /** @var array */ |
|
| 99 | - protected $excerpts = []; |
|
| 100 | - |
|
| 101 | - /** @var string */ |
|
| 102 | - protected $score = ''; |
|
| 103 | - |
|
| 104 | - /** @var array */ |
|
| 105 | - protected $info = []; |
|
| 106 | - |
|
| 107 | - /** @var int */ |
|
| 108 | - protected $contentEncoded = 0; |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * IIndexDocument constructor. |
|
| 113 | - * |
|
| 114 | - * On creation, we assure the uniqueness of the object using the providerId |
|
| 115 | - * and the Id of the original document. |
|
| 116 | - * |
|
| 117 | - * @since 15.0.0 |
|
| 118 | - * |
|
| 119 | - * @param string $providerId |
|
| 120 | - * @param string $documentId |
|
| 121 | - */ |
|
| 122 | - public function __construct(string $providerId, string $documentId) { |
|
| 123 | - $this->providerId = $providerId; |
|
| 124 | - $this->id = $documentId; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Returns the Id of the original document. |
|
| 130 | - * |
|
| 131 | - * @since 15.0.0 |
|
| 132 | - * |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - final public function getId(): string { |
|
| 136 | - return $this->id; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Returns the Id of the provider. |
|
| 142 | - * |
|
| 143 | - * @since 15.0.0 |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - final public function getProviderId(): string { |
|
| 148 | - return $this->providerId; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Set the Index related to the IIndexDocument. |
|
| 154 | - * |
|
| 155 | - * @see IIndex |
|
| 156 | - * |
|
| 157 | - * @since 15.0.0 |
|
| 158 | - * |
|
| 159 | - * @param IIndex $index |
|
| 160 | - * |
|
| 161 | - * @return IIndexDocument |
|
| 162 | - */ |
|
| 163 | - final public function setIndex(IIndex $index): IIndexDocument { |
|
| 164 | - $this->index = $index; |
|
| 165 | - |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get the Index. |
|
| 171 | - * |
|
| 172 | - * @since 15.0.0 |
|
| 173 | - * |
|
| 174 | - * @return IIndex |
|
| 175 | - */ |
|
| 176 | - final public function getIndex(): IIndex { |
|
| 177 | - return $this->index; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * return if Index is defined. |
|
| 182 | - * |
|
| 183 | - * @since 16.0.0 |
|
| 184 | - * |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - final public function hasIndex(): bool { |
|
| 188 | - return ($this->index !== null); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Set the modified time of the original document. |
|
| 194 | - * |
|
| 195 | - * @since 15.0.0 |
|
| 196 | - * |
|
| 197 | - * @param int $modifiedTime |
|
| 198 | - * |
|
| 199 | - * @return IIndexDocument |
|
| 200 | - */ |
|
| 201 | - final public function setModifiedTime(int $modifiedTime): IIndexDocument { |
|
| 202 | - $this->modifiedTime = $modifiedTime; |
|
| 203 | - |
|
| 204 | - return $this; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Get the modified time of the original document. |
|
| 209 | - * |
|
| 210 | - * @since 15.0.0 |
|
| 211 | - * |
|
| 212 | - * @return int |
|
| 213 | - */ |
|
| 214 | - final public function getModifiedTime(): int { |
|
| 215 | - return $this->modifiedTime; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Check if the original document of the IIndexDocument is older than $time. |
|
| 220 | - * |
|
| 221 | - * @since 15.0.0 |
|
| 222 | - * |
|
| 223 | - * @param int $time |
|
| 224 | - * |
|
| 225 | - * @return bool |
|
| 226 | - */ |
|
| 227 | - final public function isOlderThan(int $time): bool { |
|
| 228 | - return ($this->modifiedTime < $time); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Set the read rights of the original document using a IDocumentAccess. |
|
| 234 | - * |
|
| 235 | - * @see IDocumentAccess |
|
| 236 | - * |
|
| 237 | - * @since 15.0.0 |
|
| 238 | - * |
|
| 239 | - * @param IDocumentAccess $access |
|
| 240 | - * |
|
| 241 | - * @return $this |
|
| 242 | - */ |
|
| 243 | - final public function setAccess(IDocumentAccess $access): IIndexDocument { |
|
| 244 | - $this->access = $access; |
|
| 245 | - |
|
| 246 | - return $this; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Get the IDocumentAccess related to the original document. |
|
| 251 | - * |
|
| 252 | - * @since 15.0.0 |
|
| 253 | - * |
|
| 254 | - * @return IDocumentAccess |
|
| 255 | - */ |
|
| 256 | - final public function getAccess(): IDocumentAccess { |
|
| 257 | - return $this->access; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Add a tag to the list. |
|
| 263 | - * |
|
| 264 | - * @since 15.0.0 |
|
| 265 | - * |
|
| 266 | - * @param string $tag |
|
| 267 | - * |
|
| 268 | - * @return IIndexDocument |
|
| 269 | - */ |
|
| 270 | - final public function addTag(string $tag): IIndexDocument { |
|
| 271 | - $this->tags[] = $tag; |
|
| 272 | - |
|
| 273 | - return $this; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Set the list of tags assigned to the original document. |
|
| 278 | - * |
|
| 279 | - * @since 15.0.0 |
|
| 280 | - * |
|
| 281 | - * @param array $tags |
|
| 282 | - * |
|
| 283 | - * @return IIndexDocument |
|
| 284 | - */ |
|
| 285 | - final public function setTags(array $tags): IIndexDocument { |
|
| 286 | - $this->tags = $tags; |
|
| 287 | - |
|
| 288 | - return $this; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Get the list of tags assigned to the original document. |
|
| 293 | - * |
|
| 294 | - * @since 15.0.0 |
|
| 295 | - * |
|
| 296 | - * @return array |
|
| 297 | - */ |
|
| 298 | - final public function getTags(): array { |
|
| 299 | - return $this->tags; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Add a meta tag to the list. |
|
| 305 | - * |
|
| 306 | - * @since 15.0.0 |
|
| 307 | - * |
|
| 308 | - * @param string $tag |
|
| 309 | - * |
|
| 310 | - * @return IIndexDocument |
|
| 311 | - */ |
|
| 312 | - final public function addMetaTag(string $tag): IIndexDocument { |
|
| 313 | - $this->metaTags[] = $tag; |
|
| 314 | - |
|
| 315 | - return $this; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Set the list of meta tags assigned to the original document. |
|
| 320 | - * |
|
| 321 | - * @since 15.0.0 |
|
| 322 | - * |
|
| 323 | - * @param array $tags |
|
| 324 | - * |
|
| 325 | - * @return IIndexDocument |
|
| 326 | - */ |
|
| 327 | - final public function setMetaTags(array $tags): IIndexDocument { |
|
| 328 | - $this->metaTags = $tags; |
|
| 329 | - |
|
| 330 | - return $this; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Get the list of meta tags assigned to the original document. |
|
| 335 | - * |
|
| 336 | - * @since 15.0.0 |
|
| 337 | - * |
|
| 338 | - * @return array |
|
| 339 | - */ |
|
| 340 | - final public function getMetaTags(): array { |
|
| 341 | - return $this->metaTags; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Add a sub tag to the list. |
|
| 347 | - * |
|
| 348 | - * @since 15.0.0 |
|
| 349 | - * |
|
| 350 | - * @param string $sub |
|
| 351 | - * @param string $tag |
|
| 352 | - * |
|
| 353 | - * @return IIndexDocument |
|
| 354 | - */ |
|
| 355 | - final public function addSubTag(string $sub, string $tag): IIndexDocument { |
|
| 356 | - if (!array_key_exists($sub, $this->subTags)) { |
|
| 357 | - $this->subTags[$sub] = []; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $this->subTags[$sub][] = $tag; |
|
| 361 | - |
|
| 362 | - return $this; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Set the list of sub tags assigned to the original document. |
|
| 368 | - * |
|
| 369 | - * @since 15.0.0 |
|
| 370 | - * |
|
| 371 | - * @param array $tags |
|
| 372 | - * |
|
| 373 | - * @return IIndexDocument |
|
| 374 | - */ |
|
| 375 | - final public function setSubTags(array $tags): IIndexDocument { |
|
| 376 | - $this->subTags = $tags; |
|
| 377 | - |
|
| 378 | - return $this; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get the list of sub tags assigned to the original document. |
|
| 383 | - * If $formatted is true, the result will be formatted in a one |
|
| 384 | - * dimensional array. |
|
| 385 | - * |
|
| 386 | - * @since 15.0.0 |
|
| 387 | - * |
|
| 388 | - * @param bool $formatted |
|
| 389 | - * |
|
| 390 | - * @return array |
|
| 391 | - */ |
|
| 392 | - final public function getSubTags(bool $formatted = false): array { |
|
| 393 | - if ($formatted === false) { |
|
| 394 | - return $this->subTags; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - $subTags = []; |
|
| 398 | - $ak = array_keys($this->subTags); |
|
| 399 | - foreach ($ak as $source) { |
|
| 400 | - $tags = $this->subTags[$source]; |
|
| 401 | - foreach ($tags as $tag) { |
|
| 402 | - $subTags[] = $source . '_' . $tag; |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $subTags; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Set the source of the original document. |
|
| 412 | - * |
|
| 413 | - * @since 15.0.0 |
|
| 414 | - * |
|
| 415 | - * @param string $source |
|
| 416 | - * |
|
| 417 | - * @return IIndexDocument |
|
| 418 | - */ |
|
| 419 | - final public function setSource(string $source): IIndexDocument { |
|
| 420 | - $this->source = $source; |
|
| 421 | - |
|
| 422 | - return $this; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Get the source of the original document. |
|
| 427 | - * |
|
| 428 | - * @since 15.0.0 |
|
| 429 | - * |
|
| 430 | - * @return string |
|
| 431 | - */ |
|
| 432 | - final public function getSource(): string { |
|
| 433 | - return $this->source; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Set the title of the original document. |
|
| 439 | - * |
|
| 440 | - * @since 15.0.0 |
|
| 441 | - * |
|
| 442 | - * @param string $title |
|
| 443 | - * |
|
| 444 | - * @return IIndexDocument |
|
| 445 | - */ |
|
| 446 | - final public function setTitle(string $title): IIndexDocument { |
|
| 447 | - $this->title = $title; |
|
| 448 | - |
|
| 449 | - return $this; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Get the title of the original document. |
|
| 454 | - * |
|
| 455 | - * @since 15.0.0 |
|
| 456 | - * |
|
| 457 | - * @return string |
|
| 458 | - */ |
|
| 459 | - final public function getTitle(): string { |
|
| 460 | - return $this->title; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Set the content of the document. |
|
| 466 | - * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or |
|
| 467 | - * encoded in base64. |
|
| 468 | - * |
|
| 469 | - * @since 15.0.0 |
|
| 470 | - * |
|
| 471 | - * @param string $content |
|
| 472 | - * @param int $encoded |
|
| 473 | - * |
|
| 474 | - * @return IIndexDocument |
|
| 475 | - */ |
|
| 476 | - final public function setContent(string $content, int $encoded = 0): IIndexDocument { |
|
| 477 | - $this->content = $content; |
|
| 478 | - $this->contentEncoded = $encoded; |
|
| 479 | - |
|
| 480 | - return $this; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Get the content of the original document. |
|
| 485 | - * |
|
| 486 | - * @since 15.0.0 |
|
| 487 | - * |
|
| 488 | - * @return string |
|
| 489 | - */ |
|
| 490 | - final public function getContent(): string { |
|
| 491 | - return $this->content; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Returns the type of the encoding on the content. |
|
| 496 | - * |
|
| 497 | - * @since 15.0.0 |
|
| 498 | - * |
|
| 499 | - * @return int |
|
| 500 | - */ |
|
| 501 | - final public function isContentEncoded(): int { |
|
| 502 | - return $this->contentEncoded; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Return the size of the content. |
|
| 507 | - * |
|
| 508 | - * @since 15.0.0 |
|
| 509 | - * |
|
| 510 | - * @return int |
|
| 511 | - */ |
|
| 512 | - final public function getContentSize(): int { |
|
| 513 | - return strlen($this->getContent()); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Generate an hash, based on the content of the original document. |
|
| 519 | - * |
|
| 520 | - * @since 15.0.0 |
|
| 521 | - * |
|
| 522 | - * @return IIndexDocument |
|
| 523 | - */ |
|
| 524 | - final public function initHash(): IIndexDocument { |
|
| 525 | - if ($this->getContent() === '' || is_null($this->getContent())) { |
|
| 526 | - return $this; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $this->hash = hash("md5", $this->getContent()); |
|
| 530 | - |
|
| 531 | - return $this; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Set the hash of the original document. |
|
| 536 | - * |
|
| 537 | - * @since 15.0.0 |
|
| 538 | - * |
|
| 539 | - * @param string $hash |
|
| 540 | - * |
|
| 541 | - * @return IIndexDocument |
|
| 542 | - */ |
|
| 543 | - final public function setHash(string $hash): IIndexDocument { |
|
| 544 | - $this->hash = $hash; |
|
| 545 | - |
|
| 546 | - return $this; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Get the hash of the original document. |
|
| 551 | - * |
|
| 552 | - * @since 15.0.0 |
|
| 553 | - * |
|
| 554 | - * @return string |
|
| 555 | - */ |
|
| 556 | - final public function getHash(): string { |
|
| 557 | - return $this->hash; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Add a part, identified by a string, and its content. |
|
| 563 | - * |
|
| 564 | - * It is strongly advised to use alphanumerical chars with no space in the |
|
| 565 | - * $part string. |
|
| 566 | - * |
|
| 567 | - * @since 15.0.0 |
|
| 568 | - * |
|
| 569 | - * @param string $part |
|
| 570 | - * @param string $content |
|
| 571 | - * |
|
| 572 | - * @return IIndexDocument |
|
| 573 | - */ |
|
| 574 | - final public function addPart(string $part, string $content): IIndexDocument { |
|
| 575 | - $this->parts[$part] = $content; |
|
| 576 | - |
|
| 577 | - return $this; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Set all parts and their content. |
|
| 582 | - * |
|
| 583 | - * @since 15.0.0 |
|
| 584 | - * |
|
| 585 | - * @param array $parts |
|
| 586 | - * |
|
| 587 | - * @return IIndexDocument |
|
| 588 | - */ |
|
| 589 | - final public function setParts(array $parts): IIndexDocument { |
|
| 590 | - $this->parts = $parts; |
|
| 591 | - |
|
| 592 | - return $this; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Get all parts of the IIndexDocument. |
|
| 597 | - * |
|
| 598 | - * @since 15.0.0 |
|
| 599 | - * |
|
| 600 | - * @return array |
|
| 601 | - */ |
|
| 602 | - final public function getParts(): array { |
|
| 603 | - return $this->parts; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Add a link, usable by the frontend. |
|
| 609 | - * |
|
| 610 | - * @since 15.0.0 |
|
| 611 | - * |
|
| 612 | - * @param string $link |
|
| 613 | - * |
|
| 614 | - * @return IIndexDocument |
|
| 615 | - */ |
|
| 616 | - final public function setLink(string $link): IIndexDocument { |
|
| 617 | - $this->link = $link; |
|
| 618 | - |
|
| 619 | - return $this; |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Get the link. |
|
| 624 | - * |
|
| 625 | - * @since 15.0.0 |
|
| 626 | - * |
|
| 627 | - * @return string |
|
| 628 | - */ |
|
| 629 | - final public function getLink(): string { |
|
| 630 | - return $this->link; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Set more information that couldn't be set using other method. |
|
| 636 | - * |
|
| 637 | - * @since 15.0.0 |
|
| 638 | - * |
|
| 639 | - * @param array $more |
|
| 640 | - * |
|
| 641 | - * @return IIndexDocument |
|
| 642 | - */ |
|
| 643 | - final public function setMore(array $more): IIndexDocument { |
|
| 644 | - $this->more = $more; |
|
| 645 | - |
|
| 646 | - return $this; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Get more information. |
|
| 651 | - * |
|
| 652 | - * @since 15.0.0 |
|
| 653 | - * |
|
| 654 | - * @return array |
|
| 655 | - */ |
|
| 656 | - final public function getMore(): array { |
|
| 657 | - return $this->more; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - |
|
| 661 | - /** |
|
| 662 | - * Add some excerpt of the content of the original document, usually based |
|
| 663 | - * on the search request. |
|
| 664 | - * |
|
| 665 | - * @since 16.0.0 |
|
| 666 | - * |
|
| 667 | - * @param string $source |
|
| 668 | - * @param string $excerpt |
|
| 669 | - * |
|
| 670 | - * @return IIndexDocument |
|
| 671 | - */ |
|
| 672 | - final public function addExcerpt(string $source, string $excerpt): IIndexDocument { |
|
| 673 | - $this->excerpts[] = |
|
| 674 | - [ |
|
| 675 | - 'source' => $source, |
|
| 676 | - 'excerpt' => $this->cleanExcerpt($excerpt) |
|
| 677 | - ]; |
|
| 678 | - |
|
| 679 | - return $this; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Set all excerpts of the content of the original document. |
|
| 685 | - * |
|
| 686 | - * @since 16.0.0 |
|
| 687 | - * |
|
| 688 | - * @param array $excerpts |
|
| 689 | - * |
|
| 690 | - * @return IIndexDocument |
|
| 691 | - */ |
|
| 692 | - final public function setExcerpts(array $excerpts): IIndexDocument { |
|
| 693 | - $new = []; |
|
| 694 | - foreach ($excerpts as $entry) { |
|
| 695 | - $new[] = [ |
|
| 696 | - 'source' => $entry['source'], |
|
| 697 | - 'excerpt' => $this->cleanExcerpt($entry['excerpt']) |
|
| 698 | - ]; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - $this->excerpts = $new; |
|
| 702 | - |
|
| 703 | - return $this; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Get all excerpts of the content of the original document. |
|
| 708 | - * |
|
| 709 | - * @since 15.0.0 |
|
| 710 | - * |
|
| 711 | - * @return array |
|
| 712 | - */ |
|
| 713 | - final public function getExcerpts(): array { |
|
| 714 | - return $this->excerpts; |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - /** |
|
| 718 | - * Clean excerpt. |
|
| 719 | - * |
|
| 720 | - * @since 16.0.0 |
|
| 721 | - * |
|
| 722 | - * @param string $excerpt |
|
| 723 | - * @return string |
|
| 724 | - */ |
|
| 725 | - private function cleanExcerpt(string $excerpt): string { |
|
| 726 | - $excerpt = str_replace("\\n", ' ', $excerpt); |
|
| 727 | - $excerpt = str_replace("\\r", ' ', $excerpt); |
|
| 728 | - $excerpt = str_replace("\\t", ' ', $excerpt); |
|
| 729 | - $excerpt = str_replace("\n", ' ', $excerpt); |
|
| 730 | - $excerpt = str_replace("\r", ' ', $excerpt); |
|
| 731 | - $excerpt = str_replace("\t", ' ', $excerpt); |
|
| 732 | - |
|
| 733 | - return $excerpt; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Set the score to the result assigned to this document during a search |
|
| 739 | - * request. |
|
| 740 | - * |
|
| 741 | - * @since 15.0.0 |
|
| 742 | - * |
|
| 743 | - * @param string $score |
|
| 744 | - * |
|
| 745 | - * @return IIndexDocument |
|
| 746 | - */ |
|
| 747 | - final public function setScore(string $score): IIndexDocument { |
|
| 748 | - $this->score = $score; |
|
| 749 | - |
|
| 750 | - return $this; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Get the score. |
|
| 755 | - * |
|
| 756 | - * @since 15.0.0 |
|
| 757 | - * |
|
| 758 | - * @return string |
|
| 759 | - */ |
|
| 760 | - final public function getScore(): string { |
|
| 761 | - return $this->score; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Set some information about the original document that will be available |
|
| 767 | - * to the front-end when displaying search result. (as string) |
|
| 768 | - * Because this information will not be indexed, this method can also be |
|
| 769 | - * used to manage some data while filling the IIndexDocument before its |
|
| 770 | - * indexing. |
|
| 771 | - * |
|
| 772 | - * @since 15.0.0 |
|
| 773 | - * |
|
| 774 | - * @param string $info |
|
| 775 | - * @param string $value |
|
| 776 | - * |
|
| 777 | - * @return IIndexDocument |
|
| 778 | - */ |
|
| 779 | - final public function setInfo(string $info, string $value): IIndexDocument { |
|
| 780 | - $this->info[$info] = $value; |
|
| 781 | - |
|
| 782 | - return $this; |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - /** |
|
| 786 | - * Get an information about a document. (string) |
|
| 787 | - * |
|
| 788 | - * @since 15.0.0 |
|
| 789 | - * |
|
| 790 | - * @param string $info |
|
| 791 | - * @param string $default |
|
| 792 | - * |
|
| 793 | - * @return string |
|
| 794 | - */ |
|
| 795 | - final public function getInfo(string $info, string $default = ''): string { |
|
| 796 | - if (!key_exists($info, $this->info)) { |
|
| 797 | - return $default; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - return $this->info[$info]; |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * Set some information about the original document that will be available |
|
| 805 | - * to the front-end when displaying search result. (as array) |
|
| 806 | - * Because this information will not be indexed, this method can also be |
|
| 807 | - * used to manage some data while filling the IIndexDocument before its |
|
| 808 | - * indexing. |
|
| 809 | - * |
|
| 810 | - * @since 15.0.0 |
|
| 811 | - * |
|
| 812 | - * @param string $info |
|
| 813 | - * @param array $value |
|
| 814 | - * |
|
| 815 | - * @return IIndexDocument |
|
| 816 | - */ |
|
| 817 | - final public function setInfoArray(string $info, array $value): IIndexDocument { |
|
| 818 | - $this->info[$info] = $value; |
|
| 819 | - |
|
| 820 | - return $this; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Get an information about a document. (array) |
|
| 825 | - * |
|
| 826 | - * @since 15.0.0 |
|
| 827 | - * |
|
| 828 | - * @param string $info |
|
| 829 | - * @param array $default |
|
| 830 | - * |
|
| 831 | - * @return array |
|
| 832 | - */ |
|
| 833 | - final public function getInfoArray(string $info, array $default = []): array { |
|
| 834 | - if (!key_exists($info, $this->info)) { |
|
| 835 | - return $default; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - return $this->info[$info]; |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - /** |
|
| 842 | - * Set some information about the original document that will be available |
|
| 843 | - * to the front-end when displaying search result. (as int) |
|
| 844 | - * Because this information will not be indexed, this method can also be |
|
| 845 | - * used to manage some data while filling the IIndexDocument before its |
|
| 846 | - * indexing. |
|
| 847 | - * |
|
| 848 | - * @since 15.0.0 |
|
| 849 | - * |
|
| 850 | - * @param string $info |
|
| 851 | - * @param int $value |
|
| 852 | - * |
|
| 853 | - * @return IIndexDocument |
|
| 854 | - */ |
|
| 855 | - final public function setInfoInt(string $info, int $value): IIndexDocument { |
|
| 856 | - $this->info[$info] = $value; |
|
| 857 | - |
|
| 858 | - return $this; |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - /** |
|
| 862 | - * Get an information about a document. (int) |
|
| 863 | - * |
|
| 864 | - * @since 15.0.0 |
|
| 865 | - * |
|
| 866 | - * @param string $info |
|
| 867 | - * @param int $default |
|
| 868 | - * |
|
| 869 | - * @return int |
|
| 870 | - */ |
|
| 871 | - final public function getInfoInt(string $info, int $default = 0): int { |
|
| 872 | - if (!key_exists($info, $this->info)) { |
|
| 873 | - return $default; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - return $this->info[$info]; |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * Set some information about the original document that will be available |
|
| 881 | - * to the front-end when displaying search result. (as bool) |
|
| 882 | - * Because this information will not be indexed, this method can also be |
|
| 883 | - * used to manage some data while filling the IIndexDocument before its |
|
| 884 | - * indexing. |
|
| 885 | - * |
|
| 886 | - * @since 15.0.0 |
|
| 887 | - * |
|
| 888 | - * @param string $info |
|
| 889 | - * @param bool $value |
|
| 890 | - * |
|
| 891 | - * @return IIndexDocument |
|
| 892 | - */ |
|
| 893 | - final public function setInfoBool(string $info, bool $value): IIndexDocument { |
|
| 894 | - $this->info[$info] = $value; |
|
| 895 | - |
|
| 896 | - return $this; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - /** |
|
| 900 | - * Get an information about a document. (bool) |
|
| 901 | - * |
|
| 902 | - * @since 15.0.0 |
|
| 903 | - * |
|
| 904 | - * @param string $info |
|
| 905 | - * @param bool $default |
|
| 906 | - * |
|
| 907 | - * @return bool |
|
| 908 | - */ |
|
| 909 | - final public function getInfoBool(string $info, bool $default = false): bool { |
|
| 910 | - if (!key_exists($info, $this->info)) { |
|
| 911 | - return $default; |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - return $this->info[$info]; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - /** |
|
| 918 | - * Get all info. |
|
| 919 | - * |
|
| 920 | - * @since 15.0.0 |
|
| 921 | - * |
|
| 922 | - * @return array |
|
| 923 | - */ |
|
| 924 | - final public function getInfoAll(): array { |
|
| 925 | - $info = []; |
|
| 926 | - foreach ($this->info as $k => $v) { |
|
| 927 | - if (substr($k, 0, 1) === '_') { |
|
| 928 | - continue; |
|
| 929 | - } |
|
| 930 | - |
|
| 931 | - $info[$k] = $v; |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - return $info; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * @since 15.0.0 |
|
| 940 | - * |
|
| 941 | - * On some version of PHP, it is better to force destruct the object. |
|
| 942 | - * And during the index, the number of generated IIndexDocument can be |
|
| 943 | - * _huge_. |
|
| 944 | - */ |
|
| 945 | - public function __destruct() { |
|
| 946 | - unset($this->id); |
|
| 947 | - unset($this->providerId); |
|
| 948 | - unset($this->access); |
|
| 949 | - unset($this->modifiedTime); |
|
| 950 | - unset($this->title); |
|
| 951 | - unset($this->content); |
|
| 952 | - unset($this->hash); |
|
| 953 | - unset($this->link); |
|
| 954 | - unset($this->source); |
|
| 955 | - unset($this->tags); |
|
| 956 | - unset($this->metaTags); |
|
| 957 | - unset($this->subTags); |
|
| 958 | - unset($this->more); |
|
| 959 | - unset($this->excerpts); |
|
| 960 | - unset($this->score); |
|
| 961 | - unset($this->info); |
|
| 962 | - unset($this->contentEncoded); |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - /** |
|
| 966 | - * @since 15.0.0 |
|
| 967 | - * |
|
| 968 | - * @return array |
|
| 969 | - */ |
|
| 970 | - public function jsonSerialize() { |
|
| 971 | - return [ |
|
| 972 | - 'id' => $this->getId(), |
|
| 973 | - 'providerId' => $this->getProviderId(), |
|
| 974 | - 'access' => $this->access, |
|
| 975 | - 'modifiedTime' => $this->getModifiedTime(), |
|
| 976 | - 'title' => $this->getTitle(), |
|
| 977 | - 'link' => $this->getLink(), |
|
| 978 | - 'index' => $this->index, |
|
| 979 | - 'source' => $this->getSource(), |
|
| 980 | - 'info' => $this->getInfoAll(), |
|
| 981 | - 'hash' => $this->getHash(), |
|
| 982 | - 'contentSize' => $this->getContentSize(), |
|
| 983 | - 'tags' => $this->getTags(), |
|
| 984 | - 'metatags' => $this->getMetaTags(), |
|
| 985 | - 'subtags' => $this->getSubTags(), |
|
| 986 | - 'more' => $this->getMore(), |
|
| 987 | - 'excerpts' => $this->getExcerpts(), |
|
| 988 | - 'score' => $this->getScore() |
|
| 989 | - ]; |
|
| 990 | - } |
|
| 59 | + /** @var DocumentAccess */ |
|
| 60 | + protected $access; |
|
| 61 | + |
|
| 62 | + /** @var IIndex */ |
|
| 63 | + protected $index; |
|
| 64 | + |
|
| 65 | + /** @var int */ |
|
| 66 | + protected $modifiedTime = 0; |
|
| 67 | + |
|
| 68 | + /** @var string */ |
|
| 69 | + protected $source = ''; |
|
| 70 | + |
|
| 71 | + /** @var array */ |
|
| 72 | + protected $tags = []; |
|
| 73 | + |
|
| 74 | + /** @var array */ |
|
| 75 | + protected $metaTags = []; |
|
| 76 | + |
|
| 77 | + /** @var array */ |
|
| 78 | + protected $subTags = []; |
|
| 79 | + |
|
| 80 | + /** @var string */ |
|
| 81 | + protected $title = ''; |
|
| 82 | + |
|
| 83 | + /** @var string */ |
|
| 84 | + protected $content = ''; |
|
| 85 | + |
|
| 86 | + /** @var string */ |
|
| 87 | + protected $hash = ''; |
|
| 88 | + |
|
| 89 | + /** @var array */ |
|
| 90 | + protected $parts = []; |
|
| 91 | + |
|
| 92 | + /** @var string */ |
|
| 93 | + protected $link = ''; |
|
| 94 | + |
|
| 95 | + /** @var array */ |
|
| 96 | + protected $more = []; |
|
| 97 | + |
|
| 98 | + /** @var array */ |
|
| 99 | + protected $excerpts = []; |
|
| 100 | + |
|
| 101 | + /** @var string */ |
|
| 102 | + protected $score = ''; |
|
| 103 | + |
|
| 104 | + /** @var array */ |
|
| 105 | + protected $info = []; |
|
| 106 | + |
|
| 107 | + /** @var int */ |
|
| 108 | + protected $contentEncoded = 0; |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * IIndexDocument constructor. |
|
| 113 | + * |
|
| 114 | + * On creation, we assure the uniqueness of the object using the providerId |
|
| 115 | + * and the Id of the original document. |
|
| 116 | + * |
|
| 117 | + * @since 15.0.0 |
|
| 118 | + * |
|
| 119 | + * @param string $providerId |
|
| 120 | + * @param string $documentId |
|
| 121 | + */ |
|
| 122 | + public function __construct(string $providerId, string $documentId) { |
|
| 123 | + $this->providerId = $providerId; |
|
| 124 | + $this->id = $documentId; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Returns the Id of the original document. |
|
| 130 | + * |
|
| 131 | + * @since 15.0.0 |
|
| 132 | + * |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + final public function getId(): string { |
|
| 136 | + return $this->id; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Returns the Id of the provider. |
|
| 142 | + * |
|
| 143 | + * @since 15.0.0 |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + final public function getProviderId(): string { |
|
| 148 | + return $this->providerId; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Set the Index related to the IIndexDocument. |
|
| 154 | + * |
|
| 155 | + * @see IIndex |
|
| 156 | + * |
|
| 157 | + * @since 15.0.0 |
|
| 158 | + * |
|
| 159 | + * @param IIndex $index |
|
| 160 | + * |
|
| 161 | + * @return IIndexDocument |
|
| 162 | + */ |
|
| 163 | + final public function setIndex(IIndex $index): IIndexDocument { |
|
| 164 | + $this->index = $index; |
|
| 165 | + |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get the Index. |
|
| 171 | + * |
|
| 172 | + * @since 15.0.0 |
|
| 173 | + * |
|
| 174 | + * @return IIndex |
|
| 175 | + */ |
|
| 176 | + final public function getIndex(): IIndex { |
|
| 177 | + return $this->index; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * return if Index is defined. |
|
| 182 | + * |
|
| 183 | + * @since 16.0.0 |
|
| 184 | + * |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + final public function hasIndex(): bool { |
|
| 188 | + return ($this->index !== null); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Set the modified time of the original document. |
|
| 194 | + * |
|
| 195 | + * @since 15.0.0 |
|
| 196 | + * |
|
| 197 | + * @param int $modifiedTime |
|
| 198 | + * |
|
| 199 | + * @return IIndexDocument |
|
| 200 | + */ |
|
| 201 | + final public function setModifiedTime(int $modifiedTime): IIndexDocument { |
|
| 202 | + $this->modifiedTime = $modifiedTime; |
|
| 203 | + |
|
| 204 | + return $this; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Get the modified time of the original document. |
|
| 209 | + * |
|
| 210 | + * @since 15.0.0 |
|
| 211 | + * |
|
| 212 | + * @return int |
|
| 213 | + */ |
|
| 214 | + final public function getModifiedTime(): int { |
|
| 215 | + return $this->modifiedTime; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Check if the original document of the IIndexDocument is older than $time. |
|
| 220 | + * |
|
| 221 | + * @since 15.0.0 |
|
| 222 | + * |
|
| 223 | + * @param int $time |
|
| 224 | + * |
|
| 225 | + * @return bool |
|
| 226 | + */ |
|
| 227 | + final public function isOlderThan(int $time): bool { |
|
| 228 | + return ($this->modifiedTime < $time); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Set the read rights of the original document using a IDocumentAccess. |
|
| 234 | + * |
|
| 235 | + * @see IDocumentAccess |
|
| 236 | + * |
|
| 237 | + * @since 15.0.0 |
|
| 238 | + * |
|
| 239 | + * @param IDocumentAccess $access |
|
| 240 | + * |
|
| 241 | + * @return $this |
|
| 242 | + */ |
|
| 243 | + final public function setAccess(IDocumentAccess $access): IIndexDocument { |
|
| 244 | + $this->access = $access; |
|
| 245 | + |
|
| 246 | + return $this; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Get the IDocumentAccess related to the original document. |
|
| 251 | + * |
|
| 252 | + * @since 15.0.0 |
|
| 253 | + * |
|
| 254 | + * @return IDocumentAccess |
|
| 255 | + */ |
|
| 256 | + final public function getAccess(): IDocumentAccess { |
|
| 257 | + return $this->access; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Add a tag to the list. |
|
| 263 | + * |
|
| 264 | + * @since 15.0.0 |
|
| 265 | + * |
|
| 266 | + * @param string $tag |
|
| 267 | + * |
|
| 268 | + * @return IIndexDocument |
|
| 269 | + */ |
|
| 270 | + final public function addTag(string $tag): IIndexDocument { |
|
| 271 | + $this->tags[] = $tag; |
|
| 272 | + |
|
| 273 | + return $this; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Set the list of tags assigned to the original document. |
|
| 278 | + * |
|
| 279 | + * @since 15.0.0 |
|
| 280 | + * |
|
| 281 | + * @param array $tags |
|
| 282 | + * |
|
| 283 | + * @return IIndexDocument |
|
| 284 | + */ |
|
| 285 | + final public function setTags(array $tags): IIndexDocument { |
|
| 286 | + $this->tags = $tags; |
|
| 287 | + |
|
| 288 | + return $this; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Get the list of tags assigned to the original document. |
|
| 293 | + * |
|
| 294 | + * @since 15.0.0 |
|
| 295 | + * |
|
| 296 | + * @return array |
|
| 297 | + */ |
|
| 298 | + final public function getTags(): array { |
|
| 299 | + return $this->tags; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Add a meta tag to the list. |
|
| 305 | + * |
|
| 306 | + * @since 15.0.0 |
|
| 307 | + * |
|
| 308 | + * @param string $tag |
|
| 309 | + * |
|
| 310 | + * @return IIndexDocument |
|
| 311 | + */ |
|
| 312 | + final public function addMetaTag(string $tag): IIndexDocument { |
|
| 313 | + $this->metaTags[] = $tag; |
|
| 314 | + |
|
| 315 | + return $this; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Set the list of meta tags assigned to the original document. |
|
| 320 | + * |
|
| 321 | + * @since 15.0.0 |
|
| 322 | + * |
|
| 323 | + * @param array $tags |
|
| 324 | + * |
|
| 325 | + * @return IIndexDocument |
|
| 326 | + */ |
|
| 327 | + final public function setMetaTags(array $tags): IIndexDocument { |
|
| 328 | + $this->metaTags = $tags; |
|
| 329 | + |
|
| 330 | + return $this; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Get the list of meta tags assigned to the original document. |
|
| 335 | + * |
|
| 336 | + * @since 15.0.0 |
|
| 337 | + * |
|
| 338 | + * @return array |
|
| 339 | + */ |
|
| 340 | + final public function getMetaTags(): array { |
|
| 341 | + return $this->metaTags; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Add a sub tag to the list. |
|
| 347 | + * |
|
| 348 | + * @since 15.0.0 |
|
| 349 | + * |
|
| 350 | + * @param string $sub |
|
| 351 | + * @param string $tag |
|
| 352 | + * |
|
| 353 | + * @return IIndexDocument |
|
| 354 | + */ |
|
| 355 | + final public function addSubTag(string $sub, string $tag): IIndexDocument { |
|
| 356 | + if (!array_key_exists($sub, $this->subTags)) { |
|
| 357 | + $this->subTags[$sub] = []; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $this->subTags[$sub][] = $tag; |
|
| 361 | + |
|
| 362 | + return $this; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Set the list of sub tags assigned to the original document. |
|
| 368 | + * |
|
| 369 | + * @since 15.0.0 |
|
| 370 | + * |
|
| 371 | + * @param array $tags |
|
| 372 | + * |
|
| 373 | + * @return IIndexDocument |
|
| 374 | + */ |
|
| 375 | + final public function setSubTags(array $tags): IIndexDocument { |
|
| 376 | + $this->subTags = $tags; |
|
| 377 | + |
|
| 378 | + return $this; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get the list of sub tags assigned to the original document. |
|
| 383 | + * If $formatted is true, the result will be formatted in a one |
|
| 384 | + * dimensional array. |
|
| 385 | + * |
|
| 386 | + * @since 15.0.0 |
|
| 387 | + * |
|
| 388 | + * @param bool $formatted |
|
| 389 | + * |
|
| 390 | + * @return array |
|
| 391 | + */ |
|
| 392 | + final public function getSubTags(bool $formatted = false): array { |
|
| 393 | + if ($formatted === false) { |
|
| 394 | + return $this->subTags; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + $subTags = []; |
|
| 398 | + $ak = array_keys($this->subTags); |
|
| 399 | + foreach ($ak as $source) { |
|
| 400 | + $tags = $this->subTags[$source]; |
|
| 401 | + foreach ($tags as $tag) { |
|
| 402 | + $subTags[] = $source . '_' . $tag; |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $subTags; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Set the source of the original document. |
|
| 412 | + * |
|
| 413 | + * @since 15.0.0 |
|
| 414 | + * |
|
| 415 | + * @param string $source |
|
| 416 | + * |
|
| 417 | + * @return IIndexDocument |
|
| 418 | + */ |
|
| 419 | + final public function setSource(string $source): IIndexDocument { |
|
| 420 | + $this->source = $source; |
|
| 421 | + |
|
| 422 | + return $this; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Get the source of the original document. |
|
| 427 | + * |
|
| 428 | + * @since 15.0.0 |
|
| 429 | + * |
|
| 430 | + * @return string |
|
| 431 | + */ |
|
| 432 | + final public function getSource(): string { |
|
| 433 | + return $this->source; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Set the title of the original document. |
|
| 439 | + * |
|
| 440 | + * @since 15.0.0 |
|
| 441 | + * |
|
| 442 | + * @param string $title |
|
| 443 | + * |
|
| 444 | + * @return IIndexDocument |
|
| 445 | + */ |
|
| 446 | + final public function setTitle(string $title): IIndexDocument { |
|
| 447 | + $this->title = $title; |
|
| 448 | + |
|
| 449 | + return $this; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Get the title of the original document. |
|
| 454 | + * |
|
| 455 | + * @since 15.0.0 |
|
| 456 | + * |
|
| 457 | + * @return string |
|
| 458 | + */ |
|
| 459 | + final public function getTitle(): string { |
|
| 460 | + return $this->title; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Set the content of the document. |
|
| 466 | + * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or |
|
| 467 | + * encoded in base64. |
|
| 468 | + * |
|
| 469 | + * @since 15.0.0 |
|
| 470 | + * |
|
| 471 | + * @param string $content |
|
| 472 | + * @param int $encoded |
|
| 473 | + * |
|
| 474 | + * @return IIndexDocument |
|
| 475 | + */ |
|
| 476 | + final public function setContent(string $content, int $encoded = 0): IIndexDocument { |
|
| 477 | + $this->content = $content; |
|
| 478 | + $this->contentEncoded = $encoded; |
|
| 479 | + |
|
| 480 | + return $this; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Get the content of the original document. |
|
| 485 | + * |
|
| 486 | + * @since 15.0.0 |
|
| 487 | + * |
|
| 488 | + * @return string |
|
| 489 | + */ |
|
| 490 | + final public function getContent(): string { |
|
| 491 | + return $this->content; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Returns the type of the encoding on the content. |
|
| 496 | + * |
|
| 497 | + * @since 15.0.0 |
|
| 498 | + * |
|
| 499 | + * @return int |
|
| 500 | + */ |
|
| 501 | + final public function isContentEncoded(): int { |
|
| 502 | + return $this->contentEncoded; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Return the size of the content. |
|
| 507 | + * |
|
| 508 | + * @since 15.0.0 |
|
| 509 | + * |
|
| 510 | + * @return int |
|
| 511 | + */ |
|
| 512 | + final public function getContentSize(): int { |
|
| 513 | + return strlen($this->getContent()); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Generate an hash, based on the content of the original document. |
|
| 519 | + * |
|
| 520 | + * @since 15.0.0 |
|
| 521 | + * |
|
| 522 | + * @return IIndexDocument |
|
| 523 | + */ |
|
| 524 | + final public function initHash(): IIndexDocument { |
|
| 525 | + if ($this->getContent() === '' || is_null($this->getContent())) { |
|
| 526 | + return $this; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $this->hash = hash("md5", $this->getContent()); |
|
| 530 | + |
|
| 531 | + return $this; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Set the hash of the original document. |
|
| 536 | + * |
|
| 537 | + * @since 15.0.0 |
|
| 538 | + * |
|
| 539 | + * @param string $hash |
|
| 540 | + * |
|
| 541 | + * @return IIndexDocument |
|
| 542 | + */ |
|
| 543 | + final public function setHash(string $hash): IIndexDocument { |
|
| 544 | + $this->hash = $hash; |
|
| 545 | + |
|
| 546 | + return $this; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Get the hash of the original document. |
|
| 551 | + * |
|
| 552 | + * @since 15.0.0 |
|
| 553 | + * |
|
| 554 | + * @return string |
|
| 555 | + */ |
|
| 556 | + final public function getHash(): string { |
|
| 557 | + return $this->hash; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Add a part, identified by a string, and its content. |
|
| 563 | + * |
|
| 564 | + * It is strongly advised to use alphanumerical chars with no space in the |
|
| 565 | + * $part string. |
|
| 566 | + * |
|
| 567 | + * @since 15.0.0 |
|
| 568 | + * |
|
| 569 | + * @param string $part |
|
| 570 | + * @param string $content |
|
| 571 | + * |
|
| 572 | + * @return IIndexDocument |
|
| 573 | + */ |
|
| 574 | + final public function addPart(string $part, string $content): IIndexDocument { |
|
| 575 | + $this->parts[$part] = $content; |
|
| 576 | + |
|
| 577 | + return $this; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Set all parts and their content. |
|
| 582 | + * |
|
| 583 | + * @since 15.0.0 |
|
| 584 | + * |
|
| 585 | + * @param array $parts |
|
| 586 | + * |
|
| 587 | + * @return IIndexDocument |
|
| 588 | + */ |
|
| 589 | + final public function setParts(array $parts): IIndexDocument { |
|
| 590 | + $this->parts = $parts; |
|
| 591 | + |
|
| 592 | + return $this; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Get all parts of the IIndexDocument. |
|
| 597 | + * |
|
| 598 | + * @since 15.0.0 |
|
| 599 | + * |
|
| 600 | + * @return array |
|
| 601 | + */ |
|
| 602 | + final public function getParts(): array { |
|
| 603 | + return $this->parts; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Add a link, usable by the frontend. |
|
| 609 | + * |
|
| 610 | + * @since 15.0.0 |
|
| 611 | + * |
|
| 612 | + * @param string $link |
|
| 613 | + * |
|
| 614 | + * @return IIndexDocument |
|
| 615 | + */ |
|
| 616 | + final public function setLink(string $link): IIndexDocument { |
|
| 617 | + $this->link = $link; |
|
| 618 | + |
|
| 619 | + return $this; |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Get the link. |
|
| 624 | + * |
|
| 625 | + * @since 15.0.0 |
|
| 626 | + * |
|
| 627 | + * @return string |
|
| 628 | + */ |
|
| 629 | + final public function getLink(): string { |
|
| 630 | + return $this->link; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Set more information that couldn't be set using other method. |
|
| 636 | + * |
|
| 637 | + * @since 15.0.0 |
|
| 638 | + * |
|
| 639 | + * @param array $more |
|
| 640 | + * |
|
| 641 | + * @return IIndexDocument |
|
| 642 | + */ |
|
| 643 | + final public function setMore(array $more): IIndexDocument { |
|
| 644 | + $this->more = $more; |
|
| 645 | + |
|
| 646 | + return $this; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Get more information. |
|
| 651 | + * |
|
| 652 | + * @since 15.0.0 |
|
| 653 | + * |
|
| 654 | + * @return array |
|
| 655 | + */ |
|
| 656 | + final public function getMore(): array { |
|
| 657 | + return $this->more; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + |
|
| 661 | + /** |
|
| 662 | + * Add some excerpt of the content of the original document, usually based |
|
| 663 | + * on the search request. |
|
| 664 | + * |
|
| 665 | + * @since 16.0.0 |
|
| 666 | + * |
|
| 667 | + * @param string $source |
|
| 668 | + * @param string $excerpt |
|
| 669 | + * |
|
| 670 | + * @return IIndexDocument |
|
| 671 | + */ |
|
| 672 | + final public function addExcerpt(string $source, string $excerpt): IIndexDocument { |
|
| 673 | + $this->excerpts[] = |
|
| 674 | + [ |
|
| 675 | + 'source' => $source, |
|
| 676 | + 'excerpt' => $this->cleanExcerpt($excerpt) |
|
| 677 | + ]; |
|
| 678 | + |
|
| 679 | + return $this; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Set all excerpts of the content of the original document. |
|
| 685 | + * |
|
| 686 | + * @since 16.0.0 |
|
| 687 | + * |
|
| 688 | + * @param array $excerpts |
|
| 689 | + * |
|
| 690 | + * @return IIndexDocument |
|
| 691 | + */ |
|
| 692 | + final public function setExcerpts(array $excerpts): IIndexDocument { |
|
| 693 | + $new = []; |
|
| 694 | + foreach ($excerpts as $entry) { |
|
| 695 | + $new[] = [ |
|
| 696 | + 'source' => $entry['source'], |
|
| 697 | + 'excerpt' => $this->cleanExcerpt($entry['excerpt']) |
|
| 698 | + ]; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + $this->excerpts = $new; |
|
| 702 | + |
|
| 703 | + return $this; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Get all excerpts of the content of the original document. |
|
| 708 | + * |
|
| 709 | + * @since 15.0.0 |
|
| 710 | + * |
|
| 711 | + * @return array |
|
| 712 | + */ |
|
| 713 | + final public function getExcerpts(): array { |
|
| 714 | + return $this->excerpts; |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + /** |
|
| 718 | + * Clean excerpt. |
|
| 719 | + * |
|
| 720 | + * @since 16.0.0 |
|
| 721 | + * |
|
| 722 | + * @param string $excerpt |
|
| 723 | + * @return string |
|
| 724 | + */ |
|
| 725 | + private function cleanExcerpt(string $excerpt): string { |
|
| 726 | + $excerpt = str_replace("\\n", ' ', $excerpt); |
|
| 727 | + $excerpt = str_replace("\\r", ' ', $excerpt); |
|
| 728 | + $excerpt = str_replace("\\t", ' ', $excerpt); |
|
| 729 | + $excerpt = str_replace("\n", ' ', $excerpt); |
|
| 730 | + $excerpt = str_replace("\r", ' ', $excerpt); |
|
| 731 | + $excerpt = str_replace("\t", ' ', $excerpt); |
|
| 732 | + |
|
| 733 | + return $excerpt; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Set the score to the result assigned to this document during a search |
|
| 739 | + * request. |
|
| 740 | + * |
|
| 741 | + * @since 15.0.0 |
|
| 742 | + * |
|
| 743 | + * @param string $score |
|
| 744 | + * |
|
| 745 | + * @return IIndexDocument |
|
| 746 | + */ |
|
| 747 | + final public function setScore(string $score): IIndexDocument { |
|
| 748 | + $this->score = $score; |
|
| 749 | + |
|
| 750 | + return $this; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Get the score. |
|
| 755 | + * |
|
| 756 | + * @since 15.0.0 |
|
| 757 | + * |
|
| 758 | + * @return string |
|
| 759 | + */ |
|
| 760 | + final public function getScore(): string { |
|
| 761 | + return $this->score; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Set some information about the original document that will be available |
|
| 767 | + * to the front-end when displaying search result. (as string) |
|
| 768 | + * Because this information will not be indexed, this method can also be |
|
| 769 | + * used to manage some data while filling the IIndexDocument before its |
|
| 770 | + * indexing. |
|
| 771 | + * |
|
| 772 | + * @since 15.0.0 |
|
| 773 | + * |
|
| 774 | + * @param string $info |
|
| 775 | + * @param string $value |
|
| 776 | + * |
|
| 777 | + * @return IIndexDocument |
|
| 778 | + */ |
|
| 779 | + final public function setInfo(string $info, string $value): IIndexDocument { |
|
| 780 | + $this->info[$info] = $value; |
|
| 781 | + |
|
| 782 | + return $this; |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + /** |
|
| 786 | + * Get an information about a document. (string) |
|
| 787 | + * |
|
| 788 | + * @since 15.0.0 |
|
| 789 | + * |
|
| 790 | + * @param string $info |
|
| 791 | + * @param string $default |
|
| 792 | + * |
|
| 793 | + * @return string |
|
| 794 | + */ |
|
| 795 | + final public function getInfo(string $info, string $default = ''): string { |
|
| 796 | + if (!key_exists($info, $this->info)) { |
|
| 797 | + return $default; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + return $this->info[$info]; |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + /** |
|
| 804 | + * Set some information about the original document that will be available |
|
| 805 | + * to the front-end when displaying search result. (as array) |
|
| 806 | + * Because this information will not be indexed, this method can also be |
|
| 807 | + * used to manage some data while filling the IIndexDocument before its |
|
| 808 | + * indexing. |
|
| 809 | + * |
|
| 810 | + * @since 15.0.0 |
|
| 811 | + * |
|
| 812 | + * @param string $info |
|
| 813 | + * @param array $value |
|
| 814 | + * |
|
| 815 | + * @return IIndexDocument |
|
| 816 | + */ |
|
| 817 | + final public function setInfoArray(string $info, array $value): IIndexDocument { |
|
| 818 | + $this->info[$info] = $value; |
|
| 819 | + |
|
| 820 | + return $this; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Get an information about a document. (array) |
|
| 825 | + * |
|
| 826 | + * @since 15.0.0 |
|
| 827 | + * |
|
| 828 | + * @param string $info |
|
| 829 | + * @param array $default |
|
| 830 | + * |
|
| 831 | + * @return array |
|
| 832 | + */ |
|
| 833 | + final public function getInfoArray(string $info, array $default = []): array { |
|
| 834 | + if (!key_exists($info, $this->info)) { |
|
| 835 | + return $default; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + return $this->info[$info]; |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + /** |
|
| 842 | + * Set some information about the original document that will be available |
|
| 843 | + * to the front-end when displaying search result. (as int) |
|
| 844 | + * Because this information will not be indexed, this method can also be |
|
| 845 | + * used to manage some data while filling the IIndexDocument before its |
|
| 846 | + * indexing. |
|
| 847 | + * |
|
| 848 | + * @since 15.0.0 |
|
| 849 | + * |
|
| 850 | + * @param string $info |
|
| 851 | + * @param int $value |
|
| 852 | + * |
|
| 853 | + * @return IIndexDocument |
|
| 854 | + */ |
|
| 855 | + final public function setInfoInt(string $info, int $value): IIndexDocument { |
|
| 856 | + $this->info[$info] = $value; |
|
| 857 | + |
|
| 858 | + return $this; |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + /** |
|
| 862 | + * Get an information about a document. (int) |
|
| 863 | + * |
|
| 864 | + * @since 15.0.0 |
|
| 865 | + * |
|
| 866 | + * @param string $info |
|
| 867 | + * @param int $default |
|
| 868 | + * |
|
| 869 | + * @return int |
|
| 870 | + */ |
|
| 871 | + final public function getInfoInt(string $info, int $default = 0): int { |
|
| 872 | + if (!key_exists($info, $this->info)) { |
|
| 873 | + return $default; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + return $this->info[$info]; |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * Set some information about the original document that will be available |
|
| 881 | + * to the front-end when displaying search result. (as bool) |
|
| 882 | + * Because this information will not be indexed, this method can also be |
|
| 883 | + * used to manage some data while filling the IIndexDocument before its |
|
| 884 | + * indexing. |
|
| 885 | + * |
|
| 886 | + * @since 15.0.0 |
|
| 887 | + * |
|
| 888 | + * @param string $info |
|
| 889 | + * @param bool $value |
|
| 890 | + * |
|
| 891 | + * @return IIndexDocument |
|
| 892 | + */ |
|
| 893 | + final public function setInfoBool(string $info, bool $value): IIndexDocument { |
|
| 894 | + $this->info[$info] = $value; |
|
| 895 | + |
|
| 896 | + return $this; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + /** |
|
| 900 | + * Get an information about a document. (bool) |
|
| 901 | + * |
|
| 902 | + * @since 15.0.0 |
|
| 903 | + * |
|
| 904 | + * @param string $info |
|
| 905 | + * @param bool $default |
|
| 906 | + * |
|
| 907 | + * @return bool |
|
| 908 | + */ |
|
| 909 | + final public function getInfoBool(string $info, bool $default = false): bool { |
|
| 910 | + if (!key_exists($info, $this->info)) { |
|
| 911 | + return $default; |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + return $this->info[$info]; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + /** |
|
| 918 | + * Get all info. |
|
| 919 | + * |
|
| 920 | + * @since 15.0.0 |
|
| 921 | + * |
|
| 922 | + * @return array |
|
| 923 | + */ |
|
| 924 | + final public function getInfoAll(): array { |
|
| 925 | + $info = []; |
|
| 926 | + foreach ($this->info as $k => $v) { |
|
| 927 | + if (substr($k, 0, 1) === '_') { |
|
| 928 | + continue; |
|
| 929 | + } |
|
| 930 | + |
|
| 931 | + $info[$k] = $v; |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + return $info; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * @since 15.0.0 |
|
| 940 | + * |
|
| 941 | + * On some version of PHP, it is better to force destruct the object. |
|
| 942 | + * And during the index, the number of generated IIndexDocument can be |
|
| 943 | + * _huge_. |
|
| 944 | + */ |
|
| 945 | + public function __destruct() { |
|
| 946 | + unset($this->id); |
|
| 947 | + unset($this->providerId); |
|
| 948 | + unset($this->access); |
|
| 949 | + unset($this->modifiedTime); |
|
| 950 | + unset($this->title); |
|
| 951 | + unset($this->content); |
|
| 952 | + unset($this->hash); |
|
| 953 | + unset($this->link); |
|
| 954 | + unset($this->source); |
|
| 955 | + unset($this->tags); |
|
| 956 | + unset($this->metaTags); |
|
| 957 | + unset($this->subTags); |
|
| 958 | + unset($this->more); |
|
| 959 | + unset($this->excerpts); |
|
| 960 | + unset($this->score); |
|
| 961 | + unset($this->info); |
|
| 962 | + unset($this->contentEncoded); |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + /** |
|
| 966 | + * @since 15.0.0 |
|
| 967 | + * |
|
| 968 | + * @return array |
|
| 969 | + */ |
|
| 970 | + public function jsonSerialize() { |
|
| 971 | + return [ |
|
| 972 | + 'id' => $this->getId(), |
|
| 973 | + 'providerId' => $this->getProviderId(), |
|
| 974 | + 'access' => $this->access, |
|
| 975 | + 'modifiedTime' => $this->getModifiedTime(), |
|
| 976 | + 'title' => $this->getTitle(), |
|
| 977 | + 'link' => $this->getLink(), |
|
| 978 | + 'index' => $this->index, |
|
| 979 | + 'source' => $this->getSource(), |
|
| 980 | + 'info' => $this->getInfoAll(), |
|
| 981 | + 'hash' => $this->getHash(), |
|
| 982 | + 'contentSize' => $this->getContentSize(), |
|
| 983 | + 'tags' => $this->getTags(), |
|
| 984 | + 'metatags' => $this->getMetaTags(), |
|
| 985 | + 'subtags' => $this->getSubTags(), |
|
| 986 | + 'more' => $this->getMore(), |
|
| 987 | + 'excerpts' => $this->getExcerpts(), |
|
| 988 | + 'score' => $this->getScore() |
|
| 989 | + ]; |
|
| 990 | + } |
|
| 991 | 991 | } |