Completed
Push — master ( 4541a4...2fe9dd )
by Maxence
01:49
created

IndexDocument::setHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Model;
28
29
class IndexDocument implements \JsonSerializable {
30
31
	const NOT_ENCODED = 0;
32
	const ENCODED_BASE64 = 1;
33
34
	/** @var string|int */
35
	protected $id;
36
37
	/** @var string */
38
	protected $providerId;
39
40
	/** @var DocumentAccess */
41
	protected $access;
42
43
	/** @var Index */
44
	protected $index;
45
46
	/** @var int */
47
	protected $modifiedTime = 0;
48
49
	/** @var string */
50
	protected $source = '';
51
52
	/** @var array */
53
	protected $tags = [];
54
55
	/** @var array */
56
	protected $metaTags = [];
57
58
	/** @var array */
59
	protected $subTags = [];
60
61
	/** @var string */
62
	protected $title = '';
63
64
	/** @var string */
65
	protected $content = null;
66
67
	/** @var string */
68
	protected $hash = '';
69
70
	/** @var array */
71
	protected $parts = [];
72
73
	/** @var string */
74
	protected $link = '';
75
76
	/** @var array */
77
	protected $more = [];
78
79
	/** @var array */
80
	protected $excerpts = [];
81
82
	/** @var string */
83
	protected $score;
84
85
	/** @var array */
86
	protected $info = [];
87
88
	/** @var int */
89
	protected $contentEncoded;
90
91
92
	public function __construct($providerId, $id) {
93
		$this->providerId = $providerId;
94
		$this->id = $id;
95
	}
96
97
98
//	/**
99
//	 * @param string|integer $id
100
//	 *
101
//	 * @return $this
102
//	 */
103
//	public function setId($id) {
104
//		$this->id = $id;
105
//
106
//		return $this;
107
//	}
108
109
	/**
110
	 * @return string|integer
111
	 */
112
	public function getId() {
113
		return $this->id;
114
	}
115
116
117
//	/**
118
//	 * @param string $providerId
119
//	 *
120
//	 * @return $this
121
//	 */
122
//	public function setProviderId($providerId) {
123
//		$this->providerId = $providerId;
124
//
125
//		return $this;
126
//	}
127
128
	/**
129
	 * @return string
130
	 */
131
	public function getProviderId() {
132
		return $this->providerId;
133
	}
134
135
136
	/**
137
	 * @param Index $index
138
	 */
139
	public function setIndex(Index $index) {
140
		$this->index = $index;
141
	}
142
143
	/**
144
	 * @return Index
145
	 */
146
	public function getIndex() {
147
		return $this->index;
148
	}
149
150
151
	/**
152
	 * @param int $modifiedTime
153
	 *
154
	 * @return $this
155
	 */
156
	public function setModifiedTime($modifiedTime) {
157
		$this->modifiedTime = $modifiedTime;
158
159
		return $this;
160
	}
161
162
	/**
163
	 * @return int
164
	 */
165
	public function getModifiedTime() {
166
		return $this->modifiedTime;
167
	}
168
169
	/**
170
	 * @param int $time
171
	 *
172
	 * @return bool
173
	 */
174
	public function isOlderThan($time) {
175
		return ($this->modifiedTime < $time);
176
	}
177
178
179
	/**
180
	 * @param DocumentAccess $access
181
	 *
182
	 * @return $this
183
	 */
184
	public function setAccess(DocumentAccess $access) {
185
		$this->access = $access;
186
187
		return $this;
188
	}
189
190
	/**
191
	 * @return DocumentAccess
192
	 */
193
	public function getAccess() {
194
		return $this->access;
195
	}
196
197
198
	/**
199
	 * @param array $tags
200
	 *
201
	 * @return $this
202
	 */
203
	public function setTags($tags) {
204
		$this->tags = $tags;
205
206
		return $this;
207
	}
208
209
	/**
210
	 * @return array
211
	 */
212
	public function getTags() {
213
		return $this->tags;
214
	}
215
216
	/**
217
	 * @param $tag
218
	 *
219
	 * @return $this
220
	 */
221
	public function addTag($tag) {
222
		$this->tags[] = $tag;
223
224
		return $this;
225
	}
226
227
228
	/**
229
	 * @param array $tags
230
	 *
231
	 * @return $this
232
	 */
233
	public function setMetaTags($tags) {
234
		$this->metaTags = $tags;
235
236
		return $this;
237
	}
238
239
	/**
240
	 * @return array
241
	 */
242
	public function getMetaTags() {
243
		return $this->metaTags;
244
	}
245
246
	/**
247
	 * @param $tags
248
	 *
249
	 * @return $this
250
	 */
251
	public function addMetaTag($tags) {
252
		$this->metaTags[] = $tags;
253
254
		return $this;
255
	}
256
257
258
	/**
259
	 * @param array $tags
260
	 *
261
	 * @return $this
262
	 */
263
	public function setSubTags($tags) {
264
		$this->subTags = $tags;
265
266
		return $this;
267
	}
268
269
	/**
270
	 * @param bool $formatted
271
	 *
272
	 * @return array
273
	 */
274 View Code Duplication
	public function getSubTags($formatted = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
		if ($formatted === false) {
276
			return $this->subTags;
277
		}
278
279
		$subTags = [];
280
		$ak = array_keys($this->subTags);
281
		foreach ($ak as $source) {
282
			$tags = $this->subTags[$source];
283
			foreach ($tags as $tag) {
284
				$subTags[] = $source . '_' . $tag;
285
			}
286
		}
287
288
		return $subTags;
289
	}
290
291
	/**
292
	 * @param string $k
293
	 * @param string $tag
294
	 *
295
	 * @return $this
296
	 */
297
	public function addSubTag($k, $tag) {
298
		$this->subTags[$k] = $tag;
299
300
		return $this;
301
	}
302
303
304
	/**
305
	 * @return string
306
	 */
307
	public function getSource() {
308
		return $this->source;
309
	}
310
311
	/**
312
	 * @param string $source
313
	 *
314
	 * @return $this
315
	 */
316
	public function setSource($source) {
317
		$this->source = $source;
318
319
		return $this;
320
	}
321
322
323
	/**
324
	 * @param string $title
325
	 *
326
	 * @return $this
327
	 */
328
	public function setTitle($title) {
329
		$this->title = $title;
330
331
		return $this;
332
	}
333
334
	/**
335
	 * @return string
336
	 */
337
	public function getTitle() {
338
		return $this->title;
339
	}
340
341
342
	/**
343
	 * @param string $content
344
	 * @param int $encoded
345
	 *
346
	 * @return $this
347
	 */
348
	public function setContent($content, $encoded = 0) {
349
		$this->content = $content;
350
		$this->contentEncoded = $encoded;
351
352
		return $this;
353
	}
354
355
	/**
356
	 * @return string
357
	 */
358
	public function getContent() {
359
		return $this->content;
360
	}
361
362
	/**
363
	 * @return int
364
	 */
365
	public function getContentSize() {
366
		return strlen($this->getContent());
367
	}
368
369
370
	/**
371
	 * @return $this
372
	 */
373
	public function initHash() {
374
		if ($this->getContent() === '' || is_null($this->getContent())) {
375
			return $this;
376
		}
377
378
		$this->hash = hash("md5", $this->getContent());
379
380
		return $this;
381
	}
382
383
	/**
384
	 * @param $hash
385
	 *
386
	 * @return $this
387
	 */
388
	public function setHash($hash) {
389
		$this->hash = $hash;
390
391
		return $this;
392
	}
393
394
	/**
395
	 * @return string
396
	 */
397
	public function getHash() {
398
		return $this->hash;
399
	}
400
401
402
	/**
403
	 * @param string $part
404
	 * @param string $content
405
	 *
406
	 * @return $this
407
	 */
408
	public function addPart($part, $content) {
409
		$this->parts[$part] = $content;
410
411
		return $this;
412
	}
413
414
	/**
415
	 * @param array $parts
416
	 *
417
	 * @return $this
418
	 */
419
	public function setParts($parts) {
420
		$this->parts = $parts;
421
422
		return $this;
423
	}
424
425
	/**
426
	 * @return array
427
	 */
428
	public function getParts() {
429
		return $this->parts;
430
	}
431
432
433
	/**
434
	 * @return int
435
	 */
436
	public function isContentEncoded() {
437
		return $this->contentEncoded;
438
	}
439
440
441
	/**
442
	 * @param string $link
443
	 *
444
	 * @return $this
445
	 */
446
	public function setLink($link) {
447
		$this->link = $link;
448
449
		return $this;
450
	}
451
452
	/**
453
	 * @return string
454
	 */
455
	public function getLink() {
456
		return $this->link;
457
	}
458
459
460
	/**
461
	 * @param array $more
462
	 *
463
	 * @return $this
464
	 */
465
	public function setMore($more) {
466
		$this->more = $more;
467
468
		return $this;
469
	}
470
471
	/**
472
	 * @return array
473
	 */
474
	public function getMore() {
475
		return $this->more;
476
	}
477
478
479
	/**
480
	 * @param array $excerpts
481
	 *
482
	 * @return $this
483
	 */
484
	public function setExcerpts($excerpts) {
485
		$excerpts = array_map([$this, 'cleanExcerpt'], $excerpts);
486
487
		$this->excerpts = $excerpts;
488
489
		return $this;
490
	}
491
492
	/**
493
	 * @return array
494
	 */
495
	public function getExcerpts() {
496
		return $this->excerpts;
497
	}
498
499
	/**
500
	 * @param string $excerpt
501
	 */
502
	public function addExcerpt($excerpt) {
503
		$excerpt = $this->cleanExcerpt($excerpt);
504
505
		$this->excerpts[] = $excerpt;
506
	}
507
508
	/**
509
	 * @param $excerpt
510
	 *
511
	 * @return mixed
512
	 */
513
	public function cleanExcerpt($excerpt) {
514
		$excerpt = str_replace("\\n", ' ', $excerpt);
515
		$excerpt = str_replace("\\r", ' ', $excerpt);
516
		$excerpt = str_replace("\\t", ' ', $excerpt);
517
		$excerpt = str_replace("\n", ' ', $excerpt);
518
		$excerpt = str_replace("\r", ' ', $excerpt);
519
		$excerpt = str_replace("\t", ' ', $excerpt);
520
521
		return $excerpt;
522
	}
523
524
	/**
525
	 * @param string $score
526
	 *
527
	 * @return $this
528
	 */
529
	public function setScore($score) {
530
		$this->score = $score;
531
532
		return $this;
533
	}
534
535
	/**
536
	 * @return string
537
	 */
538
	public function getScore() {
539
		return $this->score;
540
	}
541
542
543
	/**
544
	 * @param string $info
545
	 * @param mixed $value
546
	 *
547
	 * @return $this
548
	 */
549
	public function setInfo($info, $value) {
550
		$this->info[$info] = $value;
551
552
		return $this;
553
	}
554
555
	/**
556
	 * @param string $info
557
	 * @param mixed $default
558
	 *
559
	 * @return mixed
560
	 */
561
	public function getInfo($info, $default = '') {
562
		if (!key_exists($info, $this->info)) {
563
			return $default;
564
		}
565
566
		return $this->info[$info];
567
	}
568
569
570
	/**
571
	 * @return array
572
	 */
573
	public function getInfoAll() {
574
575
		$info = [];
576
		foreach ($this->info as $k => $v) {
577
			if (substr($k, 0, 1) === '_') {
578
				continue;
579
			}
580
581
			$info[$k] = $v;
582
		}
583
584
		return $info;
585
	}
586
587
588
	public function __destruct() {
589
		unset($this->id);
590
		unset($this->providerId);
591
		unset($this->access);
592
		unset($this->modifiedTime);
593
		unset($this->title);
594
		unset($this->content);
595
		unset($this->hash);
596
		unset($this->link);
597
		unset($this->source);
598
		unset($this->tags);
599
		unset($this->metaTags);
600
		unset($this->subTags);
601
		unset($this->more);
602
		unset($this->excerpts);
603
		unset($this->score);
604
		unset($this->info);
605
		unset($this->contentEncoded);
606
	}
607
608
	/**
609
	 * @return array<string,string|integer|DocumentAccess|array>
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string|inte...mentAccess|Index|array>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
610
	 */
611
	public function jsonSerialize() {
612
		return [
613
			'id'           => $this->getId(),
614
			'providerId'   => $this->getProviderId(),
615
			'access'       => $this->getAccess(),
616
			'modifiedTime' => $this->getModifiedTime(),
617
			'title'        => $this->getTitle(),
618
			'link'         => $this->getLink(),
619
			'index'        => $this->getIndex(),
620
			'source'       => $this->getSource(),
621
			'info'         => $this->getInfoAll(),
622
			'hash'         => $this->getHash(),
623
			'contentSize'  => $this->getContentSize(),
624
			'tags'         => $this->getTags(),
625
			'metatags'     => $this->getMetaTags(),
626
			'subtags'      => $this->getSubTags(),
627
			'more'         => $this->getMore(),
628
			'excerpts'     => $this->getExcerpts(),
629
			'score'        => $this->getScore()
630
		];
631
	}
632
633
}