Completed
Push — master ( c218d3...33ae5d )
by Maxence
02:12
created

IndexDocument::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 15
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
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
	public $id;
36
37
	/** @var string */
38
	public $providerId;
39
40
	/** @var DocumentAccess */
41
	public $access;
42
43
	/** @var Index */
44
	public $index;
45
46
	/** @var int */
47
	public $modifiedTime = 0;
48
49
	/** @var array */
50
	public $tags = [];
51
52
	// TODO: do we really need it in the base class ?
53
	/** @var string */
54
	public $source = '';
55
56
	/** @var string */
57
	public $title = '';
58
59
	/** @var string */
60
	public $content = null;
61
62
	/** @var array */
63
	public $parts = [];
64
65
	/** @var string */
66
	public $link = '';
67
68
	/** @var array */
69
	public $more = [];
70
71
	/** @var array */
72
	public $excerpts = [];
73
74
	/** @var string */
75
	public $score;
76
77
	/** @var array */
78
	public $info = [];
79
80
	/** @var int */
81
	public $contentEncoded;
82
83
84
	public function __construct($providerId, $id) {
85
		$this->providerId = $providerId;
86
		$this->id = $id;
87
	}
88
89
90
	/**
91
	 * @param string|integer $id
92
	 *
93
	 * @return $this
94
	 */
95
	public function setId($id) {
96
		$this->id = $id;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * @return string|integer
103
	 */
104
	public function getId() {
105
		return $this->id;
106
	}
107
108
109
	/**
110
	 * @param string $providerId
111
	 *
112
	 * @return $this
113
	 */
114
	public function setProviderId($providerId) {
115
		$this->providerId = $providerId;
116
117
		return $this;
118
	}
119
120
	/**
121
	 * @return string
122
	 */
123
	public function getProviderId() {
124
		return $this->providerId;
125
	}
126
127
128
	/**
129
	 * @param Index $index
130
	 */
131
	public function setIndex(Index $index) {
132
		$this->index = $index;
133
	}
134
135
	/**
136
	 * @return Index
137
	 */
138
	public function getIndex() {
139
		return $this->index;
140
	}
141
142
143
	/**
144
	 * @param int $modifiedTime
145
	 *
146
	 * @return $this
147
	 */
148
	public function setModifiedTime($modifiedTime) {
149
		$this->modifiedTime = $modifiedTime;
150
151
		return $this;
152
	}
153
154
	/**
155
	 * @return int
156
	 */
157
	public function getModifiedTime() {
158
		return $this->modifiedTime;
159
	}
160
161
	/**
162
	 * @param int $time
163
	 *
164
	 * @return bool
165
	 */
166
	public function isOlderThan($time) {
167
		return ($this->modifiedTime < $time);
168
	}
169
170
171
	/**
172
	 * @param DocumentAccess $access
173
	 *
174
	 * @return $this
175
	 */
176
	public function setAccess(DocumentAccess $access) {
177
		$this->access = $access;
178
179
		return $this;
180
	}
181
182
	/**
183
	 * @return DocumentAccess
184
	 */
185
	public function getAccess() {
186
		return $this->access;
187
	}
188
189
190
	/**
191
	 * @param array $tags
192
	 *
193
	 * @return $this
194
	 */
195
	public function setTags($tags) {
196
		$this->tags = $tags;
197
198
		return $this;
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	public function getTags() {
205
		return $this->tags;
206
	}
207
208
	/**
209
	 * @param $tag
210
	 *
211
	 * @return $this
212
	 */
213
	public function addTag($tag) {
214
		$this->tags[] = $tag;
215
216
		return $this;
217
	}
218
219
	/**
220
	 * @return string
221
	 */
222
	public function getSource() {
223
		return $this->source;
224
	}
225
226
	/**
227
	 * @param string $source
228
	 *
229
	 * @return $this
230
	 */
231
	public function setSource($source) {
232
		$this->source = $source;
233
234
		return $this;
235
	}
236
237
238
	/**
239
	 * @param string $title
240
	 *
241
	 * @return $this
242
	 */
243
	public function setTitle($title) {
244
		$this->title = $title;
245
246
		return $this;
247
	}
248
249
	/**
250
	 * @return string
251
	 */
252
	public function getTitle() {
253
		return $this->title;
254
	}
255
256
257
	/**
258
	 * @param string $content
259
	 * @param int $encoded
260
	 *
261
	 * @return $this
262
	 */
263
	public function setContent($content, $encoded = 0) {
264
		$this->content = $content;
265
		$this->contentEncoded = $encoded;
266
267
		return $this;
268
	}
269
270
	/**
271
	 * @return string
272
	 */
273
	public function getContent() {
274
		return $this->content;
275
	}
276
277
278
	/**
279
	 * @param string $part
280
	 * @param string $content
281
	 *
282
	 * @return $this
283
	 */
284
	public function setPart($part, $content) {
285
		$this->parts[$part] = $content;
286
287
		return $this;
288
	}
289
290
	/**
291
	 * @param array $parts
292
	 *
293
	 * @return $this
294
	 */
295
	public function setParts($parts) {
296
		$this->parts = $parts;
297
298
		return $this;
299
	}
300
301
	/**
302
	 * @return array
303
	 */
304
	public function getParts() {
305
		return $this->parts;
306
	}
307
308
309
	/**
310
	 * @return int
311
	 */
312
	public function isContentEncoded() {
313
		return $this->contentEncoded;
314
	}
315
316
317
	/**
318
	 * @param string $link
319
	 *
320
	 * @return $this
321
	 */
322
	public function setLink($link) {
323
		$this->link = $link;
324
325
		return $this;
326
	}
327
328
	/**
329
	 * @return string
330
	 */
331
	public function getLink() {
332
		return $this->link;
333
	}
334
335
336
	/**
337
	 * @param array $more
338
	 *
339
	 * @return $this
340
	 */
341
	public function setMore($more) {
342
		$this->more = $more;
343
344
		return $this;
345
	}
346
347
	/**
348
	 * @return array
349
	 */
350
	public function getMore() {
351
		return $this->more;
352
	}
353
354
355
	/**
356
	 * @param array $excerpts
357
	 *
358
	 * @return $this
359
	 */
360
	public function setExcerpts($excerpts) {
361
		$excerpts = array_map([$this, 'cleanExcerpt'], $excerpts);
362
363
		$this->excerpts = $excerpts;
364
365
		return $this;
366
	}
367
368
	/**
369
	 * @return array
370
	 */
371
	public function getExcerpts() {
372
		return $this->excerpts;
373
	}
374
375
	/**
376
	 * @param string $excerpt
377
	 */
378
	public function addExcerpt($excerpt) {
379
		$excerpt = $this->cleanExcerpt($excerpt);
380
381
		$this->excerpts[] = $excerpt;
382
	}
383
384
	/**
385
	 * @param $excerpt
386
	 *
387
	 * @return mixed
388
	 */
389
	public function cleanExcerpt($excerpt) {
390
		$excerpt = str_replace("\\n", ' ', $excerpt);
391
		$excerpt = str_replace("\\r", ' ', $excerpt);
392
		$excerpt = str_replace("\\t", ' ', $excerpt);
393
		$excerpt = str_replace("\n", ' ', $excerpt);
394
		$excerpt = str_replace("\r", ' ', $excerpt);
395
		$excerpt = str_replace("\t", ' ', $excerpt);
396
397
		return $excerpt;
398
	}
399
400
	/**
401
	 * @param string $score
402
	 *
403
	 * @return $this
404
	 */
405
	public function setScore($score) {
406
		$this->score = $score;
407
408
		return $this;
409
	}
410
411
	/**
412
	 * @return string
413
	 */
414
	public function getScore() {
415
		return $this->score;
416
	}
417
418
419
	/**
420
	 * @param string $info
421
	 * @param mixed $value
422
	 *
423
	 * @return $this
424
	 */
425
	public function setInfo($info, $value) {
426
		$this->info[$info] = $value;
427
428
		return $this;
429
	}
430
431
	/**
432
	 * @param string $info
433
	 * @param mixed $default
434
	 *
435
	 * @return mixed
436
	 */
437
	public function getInfo($info, $default = '') {
438
		if (!key_exists($info, $this->info)) {
439
			return $default;
440
		}
441
442
		return $this->info[$info];
443
	}
444
445
446
	/**
447
	 * @return array
448
	 */
449
	public function getInfoAll() {
450
451
		$info = [];
452
		foreach ($this->info as $k => $v) {
453
			if (substr($k, 0, 1) === '_') {
454
				continue;
455
			}
456
457
			$info[$k] = $v;
458
		}
459
460
		return $info;
461
	}
462
463
464
	public function __destruct() {
465
		unset($this->id);
466
		unset($this->providerId);
467
		unset($this->access);
468
		unset($this->modifiedTime);
469
		unset($this->title);
470
		unset($this->content);
471
		unset($this->link);
472
		unset($this->source);
473
		unset($this->tags);
474
		unset($this->more);
475
		unset($this->excerpts);
476
		unset($this->score);
477
		unset($this->info);
478
		unset($this->contentEncoded);
479
	}
480
481
	/**
482
	 * @return array<string,string|integer|DocumentAccess|array>
483
	 */
484
	public function jsonSerialize() {
485
		return [
486
			'id'           => $this->getId(),
487
			'providerId'   => $this->getProviderId(),
488
			'access'       => $this->getAccess(),
489
			'modifiedTime' => $this->getModifiedTime(),
490
			'title'        => $this->getTitle(),
491
			'link'         => $this->getLink(),
492
			'source'       => $this->getSource(),
493
			'tags'         => $this->getTags(),
494
			'more'         => $this->getMore(),
495
			'excerpts'     => $this->getExcerpts(),
496
			'score'        => $this->getScore()
497
		];
498
	}
499
500
}