Passed
Push — master ( 1aad01...5decee )
by Morris
36:34 queued 17:35
created

IndexDocument   F

Complexity

Total Complexity 67

Size/Duplication

Total Lines 935
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 171
dl 0
loc 935
rs 3.04
c 0
b 0
f 0
wmc 67

55 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 2 1
A setIndex() 0 4 1
A setSubTags() 0 4 1
A isOlderThan() 0 2 1
A setInfoInt() 0 4 1
A __destruct() 0 18 1
A getContentSize() 0 2 1
A getInfoArray() 0 6 2
A getIndex() 0 2 1
A getHash() 0 2 1
A getExcerpts() 0 2 1
A hasIndex() 0 2 1
A getLink() 0 2 1
A getMetaTags() 0 2 1
A getSource() 0 2 1
A getId() 0 2 1
A setInfoBool() 0 4 1
A setScore() 0 4 1
A getSubTags() 0 15 4
A addSubTag() 0 4 1
A setTags() 0 4 1
A getInfo() 0 6 2
A setParts() 0 4 1
A setModifiedTime() 0 4 1
A jsonSerialize() 0 19 1
A setMetaTags() 0 4 1
A getTags() 0 2 1
A setTitle() 0 4 1
A getParts() 0 2 1
A setAccess() 0 4 1
A setLink() 0 4 1
A getInfoBool() 0 6 2
A getInfoAll() 0 12 3
A isContentEncoded() 0 2 1
A setInfo() 0 4 1
A setSource() 0 4 1
A setMore() 0 4 1
A getTitle() 0 2 1
A setInfoArray() 0 4 1
A getScore() 0 2 1
A setContent() 0 5 1
A getModifiedTime() 0 2 1
A addPart() 0 4 1
A addExcerpt() 0 8 1
A __construct() 0 3 1
A getAccess() 0 2 1
A cleanExcerpt() 0 9 1
A getMore() 0 2 1
A addMetaTag() 0 4 1
A addTag() 0 4 1
A getInfoInt() 0 6 2
A initHash() 0 8 3
A setHash() 0 4 1
A getProviderId() 0 2 1
A setExcerpts() 0 12 2

How to fix   Complexity   

Complex Class

Complex classes like IndexDocument often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use IndexDocument, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OC\FullTextSearch\Model;
32
33
34
use JsonSerializable;
35
use OCP\FullTextSearch\Model\IDocumentAccess;
36
use OCP\FullTextSearch\Model\IIndex;
37
use OCP\FullTextSearch\Model\IIndexDocument;
38
39
40
/**
41
 * Class IndexDocument
42
 *
43
 * This is one of the main class of the FullTextSearch, used as a data transfer
44
 * object. An IndexDocument is created to manage documents around FullTextSearch,
45
 * during an index and during a search.
46
 * The uniqueness of an IndexDocument is made by the Id of the Content Provider
47
 * and the Id of the original document within the Content Provider.
48
 *
49
 * We will call original document the source from which the IndexDocument is
50
 * generated. As an example, an original document can be a file, a mail, ...
51
 *
52
 * @since 15.0.0
53
 *
54
 * @package OC\FullTextSearch\Model
55
 */
56
class IndexDocument implements IIndexDocument, JsonSerializable {
57
58
59
	/** @var string */
60
	protected $id = '';
61
62
	/** @var string */
63
	protected $providerId = '';
64
65
	/** @var DocumentAccess */
66
	protected $access;
67
68
	/** @var IIndex */
69
	protected $index;
70
71
	/** @var int */
72
	protected $modifiedTime = 0;
73
74
	/** @var string */
75
	protected $source = '';
76
77
	/** @var array */
78
	protected $tags = [];
79
80
	/** @var array */
81
	protected $metaTags = [];
82
83
	/** @var array */
84
	protected $subTags = [];
85
86
	/** @var string */
87
	protected $title = '';
88
89
	/** @var string */
90
	protected $content = '';
91
92
	/** @var string */
93
	protected $hash = '';
94
95
	/** @var array */
96
	protected $parts = [];
97
98
	/** @var string */
99
	protected $link = '';
100
101
	/** @var array */
102
	protected $more = [];
103
104
	/** @var array */
105
	protected $excerpts = [];
106
107
	/** @var string */
108
	protected $score = '';
109
110
	/** @var array */
111
	protected $info = [];
112
113
	/** @var int */
114
	protected $contentEncoded = 0;
115
116
117
	/**
118
	 * IIndexDocument constructor.
119
	 *
120
	 * On creation, we assure the uniqueness of the object using the providerId
121
	 * and the Id of the original document.
122
	 *
123
	 * @since 15.0.0
124
	 *
125
	 * @param string $providerId
126
	 * @param string $documentId
127
	 */
128
	public function __construct(string $providerId, string $documentId) {
129
		$this->providerId = $providerId;
130
		$this->id = $documentId;
131
	}
132
133
134
	/**
135
	 * Returns the Id of the original document.
136
	 *
137
	 * @since 15.0.0
138
	 *
139
	 * @return string
140
	 */
141
	final public function getId(): string {
142
		return $this->id;
143
	}
144
145
146
	/**
147
	 * Returns the Id of the provider.
148
	 *
149
	 * @since 15.0.0
150
	 *
151
	 * @return string
152
	 */
153
	final public function getProviderId(): string {
154
		return $this->providerId;
155
	}
156
157
158
	/**
159
	 * Set the Index related to the IIndexDocument.
160
	 *
161
	 * @see IIndex
162
	 *
163
	 * @since 15.0.0
164
	 *
165
	 * @param IIndex $index
166
	 *
167
	 * @return IIndexDocument
168
	 */
169
	final public function setIndex(IIndex $index): IIndexDocument {
170
		$this->index = $index;
171
172
		return $this;
173
	}
174
175
	/**
176
	 * Get the Index.
177
	 *
178
	 * @since 15.0.0
179
	 *
180
	 * @return IIndex
181
	 */
182
	final public function getIndex(): IIndex {
183
		return $this->index;
184
	}
185
186
	/**
187
	 * return if Index is defined.
188
	 *
189
	 * @since 16.0.0
190
	 *
191
	 * @return bool
192
	 */
193
	final public function hasIndex(): bool {
194
		return ($this->index !== null);
195
	}
196
197
198
	/**
199
	 * Set the modified time of the original document.
200
	 *
201
	 * @since 15.0.0
202
	 *
203
	 * @param int $modifiedTime
204
	 *
205
	 * @return IIndexDocument
206
	 */
207
	final public function setModifiedTime(int $modifiedTime): IIndexDocument {
208
		$this->modifiedTime = $modifiedTime;
209
210
		return $this;
211
	}
212
213
	/**
214
	 * Get the modified time of the original document.
215
	 *
216
	 * @since 15.0.0
217
	 *
218
	 * @return int
219
	 */
220
	final public function getModifiedTime(): int {
221
		return $this->modifiedTime;
222
	}
223
224
	/**
225
	 * Check if the original document of the IIndexDocument is older than $time.
226
	 *
227
	 * @since 15.0.0
228
	 *
229
	 * @param int $time
230
	 *
231
	 * @return bool
232
	 */
233
	final public function isOlderThan(int $time): bool {
234
		return ($this->modifiedTime < $time);
235
	}
236
237
238
	/**
239
	 * Set the read rights of the original document using a IDocumentAccess.
240
	 *
241
	 * @see IDocumentAccess
242
	 *
243
	 * @since 15.0.0
244
	 *
245
	 * @param IDocumentAccess $access
246
	 *
247
	 * @return $this
248
	 */
249
	final public function setAccess(IDocumentAccess $access): IIndexDocument {
250
		$this->access = $access;
0 ignored issues
show
Documentation Bug introduced by
$access is of type OCP\FullTextSearch\Model\IDocumentAccess, but the property $access was declared to be of type OC\FullTextSearch\Model\DocumentAccess. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
251
252
		return $this;
253
	}
254
255
	/**
256
	 * Get the IDocumentAccess related to the original document.
257
	 *
258
	 * @since 15.0.0
259
	 *
260
	 * @return IDocumentAccess
261
	 */
262
	final public function getAccess(): IDocumentAccess {
263
		return $this->access;
264
	}
265
266
267
	/**
268
	 * Add a tag to the list.
269
	 *
270
	 * @since 15.0.0
271
	 *
272
	 * @param string $tag
273
	 *
274
	 * @return IIndexDocument
275
	 */
276
	final public function addTag(string $tag): IIndexDocument {
277
		$this->tags[] = $tag;
278
279
		return $this;
280
	}
281
282
	/**
283
	 * Set the list of tags assigned to the original document.
284
	 *
285
	 * @since 15.0.0
286
	 *
287
	 * @param array $tags
288
	 *
289
	 * @return IIndexDocument
290
	 */
291
	final public function setTags(array $tags): IIndexDocument {
292
		$this->tags = $tags;
293
294
		return $this;
295
	}
296
297
	/**
298
	 * Get the list of tags assigned to the original document.
299
	 *
300
	 * @since 15.0.0
301
	 *
302
	 * @return array
303
	 */
304
	final public function getTags(): array {
305
		return $this->tags;
306
	}
307
308
309
	/**
310
	 * Add a meta tag to the list.
311
	 *
312
	 * @since 15.0.0
313
	 *
314
	 * @param string $tag
315
	 *
316
	 * @return IIndexDocument
317
	 */
318
	final public function addMetaTag(string $tag): IIndexDocument {
319
		$this->metaTags[] = $tag;
320
321
		return $this;
322
	}
323
324
	/**
325
	 * Set the list of meta tags assigned to the original document.
326
	 *
327
	 * @since 15.0.0
328
	 *
329
	 * @param array $tags
330
	 *
331
	 * @return IIndexDocument
332
	 */
333
	final public function setMetaTags(array $tags): IIndexDocument {
334
		$this->metaTags = $tags;
335
336
		return $this;
337
	}
338
339
	/**
340
	 * Get the list of meta tags assigned to the original document.
341
	 *
342
	 * @since 15.0.0
343
	 *
344
	 * @return array
345
	 */
346
	final public function getMetaTags(): array {
347
		return $this->metaTags;
348
	}
349
350
351
	/**
352
	 * Add a sub tag to the list.
353
	 *
354
	 * @since 15.0.0
355
	 *
356
	 * @param string $sub
357
	 * @param string $tag
358
	 *
359
	 * @return IIndexDocument
360
	 */
361
	final public function addSubTag(string $sub, string $tag): IIndexDocument {
362
		$this->subTags[$sub] = $tag;
363
364
		return $this;
365
	}
366
367
	/**
368
	 * Set the list of sub tags assigned to the original document.
369
	 *
370
	 * @since 15.0.0
371
	 *
372
	 * @param array $tags
373
	 *
374
	 * @return IIndexDocument
375
	 */
376
	final public function setSubTags(array $tags): IIndexDocument {
377
		$this->subTags = $tags;
378
379
		return $this;
380
	}
381
382
	/**
383
	 * Get the list of sub tags assigned to the original document.
384
	 * If $formatted is true, the result will be formatted in a one
385
	 * dimensional array.
386
	 *
387
	 * @since 15.0.0
388
	 *
389
	 * @param bool $formatted
390
	 *
391
	 * @return array
392
	 */
393
	final public function getSubTags(bool $formatted = false): array {
394
		if ($formatted === false) {
395
			return $this->subTags;
396
		}
397
398
		$subTags = [];
399
		$ak = array_keys($this->subTags);
400
		foreach ($ak as $source) {
401
			$tags = $this->subTags[$source];
402
			foreach ($tags as $tag) {
403
				$subTags[] = $source . '_' . $tag;
404
			}
405
		}
406
407
		return $subTags;
408
	}
409
410
411
	/**
412
	 * Set the source of the original document.
413
	 *
414
	 * @since 15.0.0
415
	 *
416
	 * @param string $source
417
	 *
418
	 * @return IIndexDocument
419
	 */
420
	final public function setSource(string $source): IIndexDocument {
421
		$this->source = $source;
422
423
		return $this;
424
	}
425
426
	/**
427
	 * Get the source of the original document.
428
	 *
429
	 * @since 15.0.0
430
	 *
431
	 * @return string
432
	 */
433
	final public function getSource(): string {
434
		return $this->source;
435
	}
436
437
438
	/**
439
	 * Set the title of the original document.
440
	 *
441
	 * @since 15.0.0
442
	 *
443
	 * @param string $title
444
	 *
445
	 * @return IIndexDocument
446
	 */
447
	final public function setTitle(string $title): IIndexDocument {
448
		$this->title = $title;
449
450
		return $this;
451
	}
452
453
	/**
454
	 * Get the title of the original document.
455
	 *
456
	 * @since 15.0.0
457
	 *
458
	 * @return string
459
	 */
460
	final public function getTitle(): string {
461
		return $this->title;
462
	}
463
464
465
	/**
466
	 * Set the content of the document.
467
	 * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or
468
	 * encoded in base64.
469
	 *
470
	 * @since 15.0.0
471
	 *
472
	 * @param string $content
473
	 * @param int $encoded
474
	 *
475
	 * @return IIndexDocument
476
	 */
477
	final public function setContent(string $content, int $encoded = 0): IIndexDocument {
478
		$this->content = $content;
479
		$this->contentEncoded = $encoded;
480
481
		return $this;
482
	}
483
484
	/**
485
	 * Get the content of the original document.
486
	 *
487
	 * @since 15.0.0
488
	 *
489
	 * @return string
490
	 */
491
	final public function getContent(): string {
492
		return $this->content;
493
	}
494
495
	/**
496
	 * Returns the type of the encoding on the content.
497
	 *
498
	 * @since 15.0.0
499
	 *
500
	 * @return int
501
	 */
502
	final public function isContentEncoded(): int {
503
		return $this->contentEncoded;
504
	}
505
506
	/**
507
	 * Return the size of the content.
508
	 *
509
	 * @since 15.0.0
510
	 *
511
	 * @return int
512
	 */
513
	final public function getContentSize(): int {
514
		return strlen($this->getContent());
515
	}
516
517
518
	/**
519
	 * Generate an hash, based on the content of the original document.
520
	 *
521
	 * @since 15.0.0
522
	 *
523
	 * @return IIndexDocument
524
	 */
525
	final public function initHash(): IIndexDocument {
526
		if ($this->getContent() === '' || is_null($this->getContent())) {
527
			return $this;
528
		}
529
530
		$this->hash = hash("md5", $this->getContent());
531
532
		return $this;
533
	}
534
535
	/**
536
	 * Set the hash of the original document.
537
	 *
538
	 * @since 15.0.0
539
	 *
540
	 * @param string $hash
541
	 *
542
	 * @return IIndexDocument
543
	 */
544
	final public function setHash(string $hash): IIndexDocument {
545
		$this->hash = $hash;
546
547
		return $this;
548
	}
549
550
	/**
551
	 * Get the hash of the original document.
552
	 *
553
	 * @since 15.0.0
554
	 *
555
	 * @return string
556
	 */
557
	final public function getHash(): string {
558
		return $this->hash;
559
	}
560
561
562
	/**
563
	 * Add a part, identified by a string, and its content.
564
	 *
565
	 * It is strongly advised to use alphanumerical chars with no space in the
566
	 * $part string.
567
	 *
568
	 * @since 15.0.0
569
	 *
570
	 * @param string $part
571
	 * @param string $content
572
	 *
573
	 * @return IIndexDocument
574
	 */
575
	final public function addPart(string $part, string $content): IIndexDocument {
576
		$this->parts[$part] = $content;
577
578
		return $this;
579
	}
580
581
	/**
582
	 * Set all parts and their content.
583
	 *
584
	 * @since 15.0.0
585
	 *
586
	 * @param array $parts
587
	 *
588
	 * @return IIndexDocument
589
	 */
590
	final public function setParts(array $parts): IIndexDocument {
591
		$this->parts = $parts;
592
593
		return $this;
594
	}
595
596
	/**
597
	 * Get all parts of the IIndexDocument.
598
	 *
599
	 * @since 15.0.0
600
	 *
601
	 * @return array
602
	 */
603
	final public function getParts(): array {
604
		return $this->parts;
605
	}
606
607
608
	/**
609
	 * Add a link, usable by the frontend.
610
	 *
611
	 * @since 15.0.0
612
	 *
613
	 * @param string $link
614
	 *
615
	 * @return IIndexDocument
616
	 */
617
	final public function setLink(string $link): IIndexDocument {
618
		$this->link = $link;
619
620
		return $this;
621
	}
622
623
	/**
624
	 * Get the link.
625
	 *
626
	 * @since 15.0.0
627
	 *
628
	 * @return string
629
	 */
630
	final public function getLink(): string {
631
		return $this->link;
632
	}
633
634
635
	/**
636
	 * Set more information that couldn't be set using other method.
637
	 *
638
	 * @since 15.0.0
639
	 *
640
	 * @param array $more
641
	 *
642
	 * @return IIndexDocument
643
	 */
644
	final public function setMore(array $more): IIndexDocument {
645
		$this->more = $more;
646
647
		return $this;
648
	}
649
650
	/**
651
	 * Get more information.
652
	 *
653
	 * @since 15.0.0
654
	 *
655
	 * @return array
656
	 */
657
	final public function getMore(): array {
658
		return $this->more;
659
	}
660
661
662
	/**
663
	 * Add some excerpt of the content of the original document, usually based
664
	 * on the search request.
665
	 *
666
	 * @since 16.0.0
667
	 *
668
	 * @param string $source
669
	 * @param string $excerpt
670
	 *
671
	 * @return IIndexDocument
672
	 */
673
	final public function addExcerpt(string $source, string $excerpt): IIndexDocument {
674
		$this->excerpts[] =
675
			[
676
				'source' => $source,
677
				'excerpt' => $this->cleanExcerpt($excerpt)
678
			];
679
680
		return $this;
681
	}
682
683
684
	/**
685
	 * Set all excerpts of the content of the original document.
686
	 *
687
	 * @since 16.0.0
688
	 *
689
	 * @param array $excerpts
690
	 *
691
	 * @return IIndexDocument
692
	 */
693
	final public function setExcerpts(array $excerpts): IIndexDocument {
694
		$new = [];
695
		foreach ($excerpts as $entry) {
696
			$new[] = [
697
				'source' => $entry['source'],
698
				'excerpt' => $this->cleanExcerpt($entry['excerpt'])
699
			];
700
		}
701
702
		$this->excerpts = $new;
703
704
		return $this;
705
	}
706
707
	/**
708
	 * Get all excerpts of the content of the original document.
709
	 *
710
	 * @since 15.0.0
711
	 *
712
	 * @return array
713
	 */
714
	final public function getExcerpts(): array {
715
		return $this->excerpts;
716
	}
717
718
	/**
719
	 * Clean excerpt.
720
	 *
721
	 * @since 16.0.0
722
	 *
723
	 * @param string $excerpt
724
	 * @return string
725
	 */
726
	final private function cleanExcerpt(string $excerpt): string {
727
		$excerpt = str_replace("\\n", ' ', $excerpt);
728
		$excerpt = str_replace("\\r", ' ', $excerpt);
729
		$excerpt = str_replace("\\t", ' ', $excerpt);
730
		$excerpt = str_replace("\n", ' ', $excerpt);
731
		$excerpt = str_replace("\r", ' ', $excerpt);
732
		$excerpt = str_replace("\t", ' ', $excerpt);
733
734
		return $excerpt;
735
	}
736
737
738
	/**
739
	 * Set the score to the result assigned to this document during a search
740
	 * request.
741
	 *
742
	 * @since 15.0.0
743
	 *
744
	 * @param string $score
745
	 *
746
	 * @return IIndexDocument
747
	 */
748
	final public function setScore(string $score): IIndexDocument {
749
		$this->score = $score;
750
751
		return $this;
752
	}
753
754
	/**
755
	 * Get the score.
756
	 *
757
	 * @since 15.0.0
758
	 *
759
	 * @return string
760
	 */
761
	final public function getScore(): string {
762
		return $this->score;
763
	}
764
765
766
	/**
767
	 * Set some information about the original document that will be available
768
	 * to the front-end when displaying search result. (as string)
769
	 * Because this information will not be indexed, this method can also be
770
	 * used to manage some data while filling the IIndexDocument before its
771
	 * indexing.
772
	 *
773
	 * @since 15.0.0
774
	 *
775
	 * @param string $info
776
	 * @param string $value
777
	 *
778
	 * @return IIndexDocument
779
	 */
780
	final public function setInfo(string $info, string $value): IIndexDocument {
781
		$this->info[$info] = $value;
782
783
		return $this;
784
	}
785
786
	/**
787
	 * Get an information about a document. (string)
788
	 *
789
	 * @since 15.0.0
790
	 *
791
	 * @param string $info
792
	 * @param string $default
793
	 *
794
	 * @return string
795
	 */
796
	final public function getInfo(string $info, string $default = ''): string {
797
		if (!key_exists($info, $this->info)) {
798
			return $default;
799
		}
800
801
		return $this->info[$info];
802
	}
803
804
	/**
805
	 * Set some information about the original document that will be available
806
	 * to the front-end when displaying search result. (as array)
807
	 * Because this information will not be indexed, this method can also be
808
	 * used to manage some data while filling the IIndexDocument before its
809
	 * indexing.
810
	 *
811
	 * @since 15.0.0
812
	 *
813
	 * @param string $info
814
	 * @param array $value
815
	 *
816
	 * @return IIndexDocument
817
	 */
818
	final public function setInfoArray(string $info, array $value): IIndexDocument {
819
		$this->info[$info] = $value;
820
821
		return $this;
822
	}
823
824
	/**
825
	 * Get an information about a document. (array)
826
	 *
827
	 * @since 15.0.0
828
	 *
829
	 * @param string $info
830
	 * @param array $default
831
	 *
832
	 * @return array
833
	 */
834
	final public function getInfoArray(string $info, array $default = []): array {
835
		if (!key_exists($info, $this->info)) {
836
			return $default;
837
		}
838
839
		return $this->info[$info];
840
	}
841
842
	/**
843
	 * Set some information about the original document that will be available
844
	 * to the front-end when displaying search result. (as int)
845
	 * Because this information will not be indexed, this method can also be
846
	 * used to manage some data while filling the IIndexDocument before its
847
	 * indexing.
848
	 *
849
	 * @since 15.0.0
850
	 *
851
	 * @param string $info
852
	 * @param int $value
853
	 *
854
	 * @return IIndexDocument
855
	 */
856
	final public function setInfoInt(string $info, int $value): IIndexDocument {
857
		$this->info[$info] = $value;
858
859
		return $this;
860
	}
861
862
	/**
863
	 * Get an information about a document. (int)
864
	 *
865
	 * @since 15.0.0
866
	 *
867
	 * @param string $info
868
	 * @param int $default
869
	 *
870
	 * @return int
871
	 */
872
	final public function getInfoInt(string $info, int $default = 0): int {
873
		if (!key_exists($info, $this->info)) {
874
			return $default;
875
		}
876
877
		return $this->info[$info];
878
	}
879
880
	/**
881
	 * Set some information about the original document that will be available
882
	 * to the front-end when displaying search result. (as bool)
883
	 * Because this information will not be indexed, this method can also be
884
	 * used to manage some data while filling the IIndexDocument before its
885
	 * indexing.
886
	 *
887
	 * @since 15.0.0
888
	 *
889
	 * @param string $info
890
	 * @param bool $value
891
	 *
892
	 * @return IIndexDocument
893
	 */
894
	final public function setInfoBool(string $info, bool $value): IIndexDocument {
895
		$this->info[$info] = $value;
896
897
		return $this;
898
	}
899
900
	/**
901
	 * Get an information about a document. (bool)
902
	 *
903
	 * @since 15.0.0
904
	 *
905
	 * @param string $info
906
	 * @param bool $default
907
	 *
908
	 * @return bool
909
	 */
910
	final public function getInfoBool(string $info, bool $default = false): bool {
911
		if (!key_exists($info, $this->info)) {
912
			return $default;
913
		}
914
915
		return $this->info[$info];
916
	}
917
918
	/**
919
	 * Get all info.
920
	 *
921
	 * @since 15.0.0
922
	 *
923
	 * @return array
924
	 */
925
	final public function getInfoAll(): array {
926
927
		$info = [];
928
		foreach ($this->info as $k => $v) {
929
			if (substr($k, 0, 1) === '_') {
930
				continue;
931
			}
932
933
			$info[$k] = $v;
934
		}
935
936
		return $info;
937
	}
938
939
940
	/**
941
	 * @since 15.0.0
942
	 *
943
	 * On some version of PHP, it is better to force destruct the object.
944
	 * And during the index, the number of generated IIndexDocument can be
945
	 * _huge_.
946
	 */
947
	public function __destruct() {
948
		unset($this->id);
949
		unset($this->providerId);
950
		unset($this->access);
951
		unset($this->modifiedTime);
952
		unset($this->title);
953
		unset($this->content);
954
		unset($this->hash);
955
		unset($this->link);
956
		unset($this->source);
957
		unset($this->tags);
958
		unset($this->metaTags);
959
		unset($this->subTags);
960
		unset($this->more);
961
		unset($this->excerpts);
962
		unset($this->score);
963
		unset($this->info);
964
		unset($this->contentEncoded);
965
	}
966
967
	/**
968
	 * @since 15.0.0
969
	 *
970
	 * @return array
971
	 */
972
	public function jsonSerialize() {
973
		return [
974
			'id' => $this->getId(),
975
			'providerId' => $this->getProviderId(),
976
			'access' => $this->access,
977
			'modifiedTime' => $this->getModifiedTime(),
978
			'title' => $this->getTitle(),
979
			'link' => $this->getLink(),
980
			'index' => $this->index,
981
			'source' => $this->getSource(),
982
			'info' => $this->getInfoAll(),
983
			'hash' => $this->getHash(),
984
			'contentSize' => $this->getContentSize(),
985
			'tags' => $this->getTags(),
986
			'metatags' => $this->getMetaTags(),
987
			'subtags' => $this->getSubTags(),
988
			'more' => $this->getMore(),
989
			'excerpts' => $this->getExcerpts(),
990
			'score' => $this->getScore()
991
		];
992
	}
993
994
}
995
996