Passed
Push — master ( 0882b6...993609 )
by Morris
19:54 queued 08:47
created
lib/public/FullTextSearch/Model/IndexDocument.php 1 patch
Indentation   +921 added lines, -921 removed lines patch added patch discarded remove patch
@@ -52,934 +52,934 @@
 block discarded – undo
52 52
 class IndexDocument implements JsonSerializable {
53 53
 
54 54
 
55
-	const NOT_ENCODED = 0;
56
-	const ENCODED_BASE64 = 1;
55
+    const NOT_ENCODED = 0;
56
+    const ENCODED_BASE64 = 1;
57 57
 
58 58
 
59
-	/** @var string */
60
-	protected $id = '';
59
+    /** @var string */
60
+    protected $id = '';
61 61
 
62
-	/** @var string */
63
-	protected $providerId = '';
62
+    /** @var string */
63
+    protected $providerId = '';
64 64
 
65
-	/** @var DocumentAccess */
66
-	protected $access;
65
+    /** @var DocumentAccess */
66
+    protected $access;
67 67
 
68
-	/** @var IIndex */
69
-	protected $index;
70
-
71
-	/** @var int */
72
-	protected $modifiedTime = 0;
68
+    /** @var IIndex */
69
+    protected $index;
70
+
71
+    /** @var int */
72
+    protected $modifiedTime = 0;
73 73
 
74
-	/** @var string */
75
-	protected $source = '';
74
+    /** @var string */
75
+    protected $source = '';
76 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
-	 * IndexDocument 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 IndexDocument.
160
-	 *
161
-	 * @see IIndex
162
-	 *
163
-	 * @since 15.0.0
164
-	 *
165
-	 * @param IIndex $index
166
-	 *
167
-	 * @return IndexDocument
168
-	 */
169
-	final public function setIndex(IIndex $index): IndexDocument {
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 IndexDocument
206
-	 */
207
-	final public function setModifiedTime(int $modifiedTime): IndexDocument {
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 IndexDocument 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 DocumentAccess.
240
-	 *
241
-	 * @see DocumentAccess
242
-	 *
243
-	 * @since 15.0.0
244
-	 *
245
-	 * @param DocumentAccess $access
246
-	 *
247
-	 * @return $this
248
-	 */
249
-	final public function setAccess(DocumentAccess $access) {
250
-		$this->access = $access;
251
-
252
-		return $this;
253
-	}
254
-
255
-	/**
256
-	 * Get the DocumentAccess related to the original document.
257
-	 *
258
-	 * @since 15.0.0
259
-	 *
260
-	 * @return DocumentAccess
261
-	 */
262
-	final public function getAccess(): DocumentAccess {
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 IndexDocument
275
-	 */
276
-	final public function addTag(string $tag): IndexDocument {
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 IndexDocument
290
-	 */
291
-	final public function setTags(array $tags): IndexDocument {
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 IndexDocument
317
-	 */
318
-	final public function addMetaTag(string $tag): IndexDocument {
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 IndexDocument
332
-	 */
333
-	final public function setMetaTags(array $tags): IndexDocument {
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 IndexDocument
360
-	 */
361
-	final public function addSubTag(string $sub, string $tag): IndexDocument {
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 IndexDocument
375
-	 */
376
-	final public function setSubTags(array $tags): IndexDocument {
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 IndexDocument
419
-	 */
420
-	final public function setSource(string $source): IndexDocument {
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 IndexDocument
446
-	 */
447
-	final public function setTitle(string $title): IndexDocument {
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 IndexDocument
476
-	 */
477
-	final public function setContent(string $content, int $encoded = 0): IndexDocument {
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 IndexDocument
524
-	 */
525
-	final public function initHash(): IndexDocument {
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 IndexDocument
543
-	 */
544
-	final public function setHash(string $hash): IndexDocument {
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 IndexDocument
574
-	 */
575
-	final public function addPart(string $part, string $content): IndexDocument {
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 IndexDocument
589
-	 */
590
-	final public function setParts(array $parts): IndexDocument {
591
-		$this->parts = $parts;
592
-
593
-		return $this;
594
-	}
595
-
596
-	/**
597
-	 * Get all parts of the IndexDocument.
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 IndexDocument
616
-	 */
617
-	final public function setLink(string $link): IndexDocument {
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 IndexDocument
643
-	 */
644
-	final public function setMore(array $more): IndexDocument {
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 15.0.0
667
-	 *
668
-	 * @param string $excerpt
669
-	 *
670
-	 * @return IndexDocument
671
-	 */
672
-	final public function addExcerpt(string $excerpt): IndexDocument {
673
-		$excerpt = $this->cleanExcerpt($excerpt);
674
-
675
-		$this->excerpts[] = $excerpt;
676
-
677
-		return $this;
678
-	}
679
-
680
-	/**
681
-	 * Set all excerpts of the content of the original document.
682
-	 *
683
-	 * @since 15.0.0
684
-	 *
685
-	 * @param array $excerpts
686
-	 *
687
-	 * @return IndexDocument
688
-	 */
689
-	final public function setExcerpts(array $excerpts): IndexDocument {
690
-		$excerpts = array_map([$this, 'cleanExcerpt'], $excerpts);
691
-
692
-		$this->excerpts = $excerpts;
693
-
694
-		return $this;
695
-	}
696
-
697
-	/**
698
-	 * Get all excerpts of the content of the original document.
699
-	 *
700
-	 * @since 15.0.0
701
-	 *
702
-	 * @return array
703
-	 */
704
-	final public function getExcerpts(): array {
705
-		return $this->excerpts;
706
-	}
707
-
708
-	/**
709
-	 * Clean excerpt.
710
-	 *
711
-	 * @since 15.0.0
712
-	 *
713
-	 * @param string $excerpt
714
-	 *
715
-	 * @return string
716
-	 */
717
-	final public function cleanExcerpt(string $excerpt): string {
718
-		$excerpt = str_replace("\\n", ' ', $excerpt);
719
-		$excerpt = str_replace("\\r", ' ', $excerpt);
720
-		$excerpt = str_replace("\\t", ' ', $excerpt);
721
-		$excerpt = str_replace("\n", ' ', $excerpt);
722
-		$excerpt = str_replace("\r", ' ', $excerpt);
723
-		$excerpt = str_replace("\t", ' ', $excerpt);
724
-
725
-		return $excerpt;
726
-	}
727
-
728
-	/**
729
-	 * Set the score to the result assigned to this document during a search
730
-	 * request.
731
-	 *
732
-	 * @since 15.0.0
733
-	 *
734
-	 * @param string $score
735
-	 *
736
-	 * @return IndexDocument
737
-	 */
738
-	final public function setScore(string $score): IndexDocument {
739
-		$this->score = $score;
740
-
741
-		return $this;
742
-	}
743
-
744
-	/**
745
-	 * Get the score.
746
-	 *
747
-	 * @since 15.0.0
748
-	 *
749
-	 * @return string
750
-	 */
751
-	final public function getScore(): string {
752
-		return $this->score;
753
-	}
754
-
755
-
756
-	/**
757
-	 * Set some information about the original document that will be available
758
-	 * to the front-end when displaying search result. (as string)
759
-	 * Because this information will not be indexed, this method can also be
760
-	 * used to manage some data while filling the IndexDocument before its
761
-	 * indexing.
762
-	 *
763
-	 * @since 15.0.0
764
-	 *
765
-	 * @param string $info
766
-	 * @param string $value
767
-	 *
768
-	 * @return IndexDocument
769
-	 */
770
-	final public function setInfo(string $info, string $value): IndexDocument {
771
-		$this->info[$info] = $value;
772
-
773
-		return $this;
774
-	}
775
-
776
-	/**
777
-	 * Get an information about a document. (string)
778
-	 *
779
-	 * @since 15.0.0
780
-	 *
781
-	 * @param string $info
782
-	 * @param string $default
783
-	 *
784
-	 * @return string
785
-	 */
786
-	final public function getInfo(string $info, string $default = ''): string {
787
-		if (!key_exists($info, $this->info)) {
788
-			return $default;
789
-		}
790
-
791
-		return $this->info[$info];
792
-	}
793
-
794
-	/**
795
-	 * Set some information about the original document that will be available
796
-	 * to the front-end when displaying search result. (as array)
797
-	 * Because this information will not be indexed, this method can also be
798
-	 * used to manage some data while filling the IndexDocument before its
799
-	 * indexing.
800
-	 *
801
-	 * @since 15.0.0
802
-	 *
803
-	 * @param string $info
804
-	 * @param array $value
805
-	 *
806
-	 * @return IndexDocument
807
-	 */
808
-	final public function setInfoArray(string $info, array $value): IndexDocument {
809
-		$this->info[$info] = $value;
810
-
811
-		return $this;
812
-	}
813
-
814
-	/**
815
-	 * Get an information about a document. (array)
816
-	 *
817
-	 * @since 15.0.0
818
-	 *
819
-	 * @param string $info
820
-	 * @param array $default
821
-	 *
822
-	 * @return array
823
-	 */
824
-	final public function getInfoArray(string $info, array $default = []): array {
825
-		if (!key_exists($info, $this->info)) {
826
-			return $default;
827
-		}
828
-
829
-		return $this->info[$info];
830
-	}
831
-
832
-	/**
833
-	 * Set some information about the original document that will be available
834
-	 * to the front-end when displaying search result. (as int)
835
-	 * Because this information will not be indexed, this method can also be
836
-	 * used to manage some data while filling the IndexDocument before its
837
-	 * indexing.
838
-	 *
839
-	 * @since 15.0.0
840
-	 *
841
-	 * @param string $info
842
-	 * @param int $value
843
-	 *
844
-	 * @return IndexDocument
845
-	 */
846
-	final public function setInfoInt(string $info, int $value): IndexDocument {
847
-		$this->info[$info] = $value;
848
-
849
-		return $this;
850
-	}
851
-
852
-	/**
853
-	 * Get an information about a document. (int)
854
-	 *
855
-	 * @since 15.0.0
856
-	 *
857
-	 * @param string $info
858
-	 * @param int $default
859
-	 *
860
-	 * @return int
861
-	 */
862
-	final public function getInfoInt(string $info, int $default = 0): int {
863
-		if (!key_exists($info, $this->info)) {
864
-			return $default;
865
-		}
866
-
867
-		return $this->info[$info];
868
-	}
869
-
870
-	/**
871
-	 * Set some information about the original document that will be available
872
-	 * to the front-end when displaying search result. (as bool)
873
-	 * Because this information will not be indexed, this method can also be
874
-	 * used to manage some data while filling the IndexDocument before its
875
-	 * indexing.
876
-	 *
877
-	 * @since 15.0.0
878
-	 *
879
-	 * @param string $info
880
-	 * @param bool $value
881
-	 *
882
-	 * @return IndexDocument
883
-	 */
884
-	final public function setInfoBool(string $info, bool $value): IndexDocument {
885
-		$this->info[$info] = $value;
886
-
887
-		return $this;
888
-	}
889
-
890
-	/**
891
-	 * Get an information about a document. (bool)
892
-	 *
893
-	 * @since 15.0.0
894
-	 *
895
-	 * @param string $info
896
-	 * @param bool $default
897
-	 *
898
-	 * @return bool
899
-	 */
900
-	final public function getInfoBool(string $info, bool $default = false): bool {
901
-		if (!key_exists($info, $this->info)) {
902
-			return $default;
903
-		}
904
-
905
-		return $this->info[$info];
906
-	}
907
-
908
-	/**
909
-	 * Get all info.
910
-	 *
911
-	 * @since 15.0.0
912
-	 *
913
-	 * @return array
914
-	 */
915
-	final public function getInfoAll(): array {
916
-
917
-		$info = [];
918
-		foreach ($this->info as $k => $v) {
919
-			if (substr($k, 0, 1) === '_') {
920
-				continue;
921
-			}
922
-
923
-			$info[$k] = $v;
924
-		}
925
-
926
-		return $info;
927
-	}
928
-
929
-
930
-	/**
931
-	 * @since 15.0.0
932
-	 *
933
-	 * On some version of PHP, it is better to force destruct the object.
934
-	 * And during the index, the number of generated IndexDocument can be
935
-	 * _huge_.
936
-	 */
937
-	public function __destruct() {
938
-		unset($this->id);
939
-		unset($this->providerId);
940
-		unset($this->access);
941
-		unset($this->modifiedTime);
942
-		unset($this->title);
943
-		unset($this->content);
944
-		unset($this->hash);
945
-		unset($this->link);
946
-		unset($this->source);
947
-		unset($this->tags);
948
-		unset($this->metaTags);
949
-		unset($this->subTags);
950
-		unset($this->more);
951
-		unset($this->excerpts);
952
-		unset($this->score);
953
-		unset($this->info);
954
-		unset($this->contentEncoded);
955
-	}
956
-
957
-	/**
958
-	 * @since 15.0.0
959
-	 *
960
-	 * @return array
961
-	 */
962
-	public function jsonSerialize() {
963
-		return [
964
-			'id' => $this->getId(),
965
-			'providerId' => $this->getProviderId(),
966
-			'access' => $this->access,
967
-			'modifiedTime' => $this->getModifiedTime(),
968
-			'title' => $this->getTitle(),
969
-			'link' => $this->getLink(),
970
-			'index' => $this->index,
971
-			'source' => $this->getSource(),
972
-			'info' => $this->getInfoAll(),
973
-			'hash' => $this->getHash(),
974
-			'contentSize' => $this->getContentSize(),
975
-			'tags' => $this->getTags(),
976
-			'metatags' => $this->getMetaTags(),
977
-			'subtags' => $this->getSubTags(),
978
-			'more' => $this->getMore(),
979
-			'excerpts' => $this->getExcerpts(),
980
-			'score' => $this->getScore()
981
-		];
982
-	}
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
+     * IndexDocument 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 IndexDocument.
160
+     *
161
+     * @see IIndex
162
+     *
163
+     * @since 15.0.0
164
+     *
165
+     * @param IIndex $index
166
+     *
167
+     * @return IndexDocument
168
+     */
169
+    final public function setIndex(IIndex $index): IndexDocument {
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 IndexDocument
206
+     */
207
+    final public function setModifiedTime(int $modifiedTime): IndexDocument {
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 IndexDocument 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 DocumentAccess.
240
+     *
241
+     * @see DocumentAccess
242
+     *
243
+     * @since 15.0.0
244
+     *
245
+     * @param DocumentAccess $access
246
+     *
247
+     * @return $this
248
+     */
249
+    final public function setAccess(DocumentAccess $access) {
250
+        $this->access = $access;
251
+
252
+        return $this;
253
+    }
254
+
255
+    /**
256
+     * Get the DocumentAccess related to the original document.
257
+     *
258
+     * @since 15.0.0
259
+     *
260
+     * @return DocumentAccess
261
+     */
262
+    final public function getAccess(): DocumentAccess {
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 IndexDocument
275
+     */
276
+    final public function addTag(string $tag): IndexDocument {
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 IndexDocument
290
+     */
291
+    final public function setTags(array $tags): IndexDocument {
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 IndexDocument
317
+     */
318
+    final public function addMetaTag(string $tag): IndexDocument {
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 IndexDocument
332
+     */
333
+    final public function setMetaTags(array $tags): IndexDocument {
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 IndexDocument
360
+     */
361
+    final public function addSubTag(string $sub, string $tag): IndexDocument {
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 IndexDocument
375
+     */
376
+    final public function setSubTags(array $tags): IndexDocument {
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 IndexDocument
419
+     */
420
+    final public function setSource(string $source): IndexDocument {
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 IndexDocument
446
+     */
447
+    final public function setTitle(string $title): IndexDocument {
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 IndexDocument
476
+     */
477
+    final public function setContent(string $content, int $encoded = 0): IndexDocument {
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 IndexDocument
524
+     */
525
+    final public function initHash(): IndexDocument {
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 IndexDocument
543
+     */
544
+    final public function setHash(string $hash): IndexDocument {
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 IndexDocument
574
+     */
575
+    final public function addPart(string $part, string $content): IndexDocument {
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 IndexDocument
589
+     */
590
+    final public function setParts(array $parts): IndexDocument {
591
+        $this->parts = $parts;
592
+
593
+        return $this;
594
+    }
595
+
596
+    /**
597
+     * Get all parts of the IndexDocument.
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 IndexDocument
616
+     */
617
+    final public function setLink(string $link): IndexDocument {
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 IndexDocument
643
+     */
644
+    final public function setMore(array $more): IndexDocument {
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 15.0.0
667
+     *
668
+     * @param string $excerpt
669
+     *
670
+     * @return IndexDocument
671
+     */
672
+    final public function addExcerpt(string $excerpt): IndexDocument {
673
+        $excerpt = $this->cleanExcerpt($excerpt);
674
+
675
+        $this->excerpts[] = $excerpt;
676
+
677
+        return $this;
678
+    }
679
+
680
+    /**
681
+     * Set all excerpts of the content of the original document.
682
+     *
683
+     * @since 15.0.0
684
+     *
685
+     * @param array $excerpts
686
+     *
687
+     * @return IndexDocument
688
+     */
689
+    final public function setExcerpts(array $excerpts): IndexDocument {
690
+        $excerpts = array_map([$this, 'cleanExcerpt'], $excerpts);
691
+
692
+        $this->excerpts = $excerpts;
693
+
694
+        return $this;
695
+    }
696
+
697
+    /**
698
+     * Get all excerpts of the content of the original document.
699
+     *
700
+     * @since 15.0.0
701
+     *
702
+     * @return array
703
+     */
704
+    final public function getExcerpts(): array {
705
+        return $this->excerpts;
706
+    }
707
+
708
+    /**
709
+     * Clean excerpt.
710
+     *
711
+     * @since 15.0.0
712
+     *
713
+     * @param string $excerpt
714
+     *
715
+     * @return string
716
+     */
717
+    final public function cleanExcerpt(string $excerpt): string {
718
+        $excerpt = str_replace("\\n", ' ', $excerpt);
719
+        $excerpt = str_replace("\\r", ' ', $excerpt);
720
+        $excerpt = str_replace("\\t", ' ', $excerpt);
721
+        $excerpt = str_replace("\n", ' ', $excerpt);
722
+        $excerpt = str_replace("\r", ' ', $excerpt);
723
+        $excerpt = str_replace("\t", ' ', $excerpt);
724
+
725
+        return $excerpt;
726
+    }
727
+
728
+    /**
729
+     * Set the score to the result assigned to this document during a search
730
+     * request.
731
+     *
732
+     * @since 15.0.0
733
+     *
734
+     * @param string $score
735
+     *
736
+     * @return IndexDocument
737
+     */
738
+    final public function setScore(string $score): IndexDocument {
739
+        $this->score = $score;
740
+
741
+        return $this;
742
+    }
743
+
744
+    /**
745
+     * Get the score.
746
+     *
747
+     * @since 15.0.0
748
+     *
749
+     * @return string
750
+     */
751
+    final public function getScore(): string {
752
+        return $this->score;
753
+    }
754
+
755
+
756
+    /**
757
+     * Set some information about the original document that will be available
758
+     * to the front-end when displaying search result. (as string)
759
+     * Because this information will not be indexed, this method can also be
760
+     * used to manage some data while filling the IndexDocument before its
761
+     * indexing.
762
+     *
763
+     * @since 15.0.0
764
+     *
765
+     * @param string $info
766
+     * @param string $value
767
+     *
768
+     * @return IndexDocument
769
+     */
770
+    final public function setInfo(string $info, string $value): IndexDocument {
771
+        $this->info[$info] = $value;
772
+
773
+        return $this;
774
+    }
775
+
776
+    /**
777
+     * Get an information about a document. (string)
778
+     *
779
+     * @since 15.0.0
780
+     *
781
+     * @param string $info
782
+     * @param string $default
783
+     *
784
+     * @return string
785
+     */
786
+    final public function getInfo(string $info, string $default = ''): string {
787
+        if (!key_exists($info, $this->info)) {
788
+            return $default;
789
+        }
790
+
791
+        return $this->info[$info];
792
+    }
793
+
794
+    /**
795
+     * Set some information about the original document that will be available
796
+     * to the front-end when displaying search result. (as array)
797
+     * Because this information will not be indexed, this method can also be
798
+     * used to manage some data while filling the IndexDocument before its
799
+     * indexing.
800
+     *
801
+     * @since 15.0.0
802
+     *
803
+     * @param string $info
804
+     * @param array $value
805
+     *
806
+     * @return IndexDocument
807
+     */
808
+    final public function setInfoArray(string $info, array $value): IndexDocument {
809
+        $this->info[$info] = $value;
810
+
811
+        return $this;
812
+    }
813
+
814
+    /**
815
+     * Get an information about a document. (array)
816
+     *
817
+     * @since 15.0.0
818
+     *
819
+     * @param string $info
820
+     * @param array $default
821
+     *
822
+     * @return array
823
+     */
824
+    final public function getInfoArray(string $info, array $default = []): array {
825
+        if (!key_exists($info, $this->info)) {
826
+            return $default;
827
+        }
828
+
829
+        return $this->info[$info];
830
+    }
831
+
832
+    /**
833
+     * Set some information about the original document that will be available
834
+     * to the front-end when displaying search result. (as int)
835
+     * Because this information will not be indexed, this method can also be
836
+     * used to manage some data while filling the IndexDocument before its
837
+     * indexing.
838
+     *
839
+     * @since 15.0.0
840
+     *
841
+     * @param string $info
842
+     * @param int $value
843
+     *
844
+     * @return IndexDocument
845
+     */
846
+    final public function setInfoInt(string $info, int $value): IndexDocument {
847
+        $this->info[$info] = $value;
848
+
849
+        return $this;
850
+    }
851
+
852
+    /**
853
+     * Get an information about a document. (int)
854
+     *
855
+     * @since 15.0.0
856
+     *
857
+     * @param string $info
858
+     * @param int $default
859
+     *
860
+     * @return int
861
+     */
862
+    final public function getInfoInt(string $info, int $default = 0): int {
863
+        if (!key_exists($info, $this->info)) {
864
+            return $default;
865
+        }
866
+
867
+        return $this->info[$info];
868
+    }
869
+
870
+    /**
871
+     * Set some information about the original document that will be available
872
+     * to the front-end when displaying search result. (as bool)
873
+     * Because this information will not be indexed, this method can also be
874
+     * used to manage some data while filling the IndexDocument before its
875
+     * indexing.
876
+     *
877
+     * @since 15.0.0
878
+     *
879
+     * @param string $info
880
+     * @param bool $value
881
+     *
882
+     * @return IndexDocument
883
+     */
884
+    final public function setInfoBool(string $info, bool $value): IndexDocument {
885
+        $this->info[$info] = $value;
886
+
887
+        return $this;
888
+    }
889
+
890
+    /**
891
+     * Get an information about a document. (bool)
892
+     *
893
+     * @since 15.0.0
894
+     *
895
+     * @param string $info
896
+     * @param bool $default
897
+     *
898
+     * @return bool
899
+     */
900
+    final public function getInfoBool(string $info, bool $default = false): bool {
901
+        if (!key_exists($info, $this->info)) {
902
+            return $default;
903
+        }
904
+
905
+        return $this->info[$info];
906
+    }
907
+
908
+    /**
909
+     * Get all info.
910
+     *
911
+     * @since 15.0.0
912
+     *
913
+     * @return array
914
+     */
915
+    final public function getInfoAll(): array {
916
+
917
+        $info = [];
918
+        foreach ($this->info as $k => $v) {
919
+            if (substr($k, 0, 1) === '_') {
920
+                continue;
921
+            }
922
+
923
+            $info[$k] = $v;
924
+        }
925
+
926
+        return $info;
927
+    }
928
+
929
+
930
+    /**
931
+     * @since 15.0.0
932
+     *
933
+     * On some version of PHP, it is better to force destruct the object.
934
+     * And during the index, the number of generated IndexDocument can be
935
+     * _huge_.
936
+     */
937
+    public function __destruct() {
938
+        unset($this->id);
939
+        unset($this->providerId);
940
+        unset($this->access);
941
+        unset($this->modifiedTime);
942
+        unset($this->title);
943
+        unset($this->content);
944
+        unset($this->hash);
945
+        unset($this->link);
946
+        unset($this->source);
947
+        unset($this->tags);
948
+        unset($this->metaTags);
949
+        unset($this->subTags);
950
+        unset($this->more);
951
+        unset($this->excerpts);
952
+        unset($this->score);
953
+        unset($this->info);
954
+        unset($this->contentEncoded);
955
+    }
956
+
957
+    /**
958
+     * @since 15.0.0
959
+     *
960
+     * @return array
961
+     */
962
+    public function jsonSerialize() {
963
+        return [
964
+            'id' => $this->getId(),
965
+            'providerId' => $this->getProviderId(),
966
+            'access' => $this->access,
967
+            'modifiedTime' => $this->getModifiedTime(),
968
+            'title' => $this->getTitle(),
969
+            'link' => $this->getLink(),
970
+            'index' => $this->index,
971
+            'source' => $this->getSource(),
972
+            'info' => $this->getInfoAll(),
973
+            'hash' => $this->getHash(),
974
+            'contentSize' => $this->getContentSize(),
975
+            'tags' => $this->getTags(),
976
+            'metatags' => $this->getMetaTags(),
977
+            'subtags' => $this->getSubTags(),
978
+            'more' => $this->getMore(),
979
+            'excerpts' => $this->getExcerpts(),
980
+            'score' => $this->getScore()
981
+        ];
982
+    }
983 983
 
984 984
 }
985 985
 
Please login to merge, or discard this patch.