Passed
Push — master ( 3f88db...32ded8 )
by Christoph
17:12 queued 26s
created

SearchResultEntry::addAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author John Molakvoæ (skjnldsv) <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCP\Search;
29
30
use JsonSerializable;
31
32
/**
33
 * Represents an entry in a list of results an app returns for a unified search
34
 * query.
35
 *
36
 * The app providing the results has to extend this class for customization. In
37
 * most cases apps do not have to add any additional code.
38
 *
39
 * @example ``class MailResultEntry extends SearchResultEntry {}`
40
 *
41
 * This approach was chosen over a final class as it allows Nextcloud to later
42
 * add new optional properties of an entry without having to break the usage of
43
 * this class in apps.
44
 *
45
 * @since 20.0.0
46
 */
47
class SearchResultEntry implements JsonSerializable {
48
49
	/**
50
	 * @var string
51
	 * @since 20.0.0
52
	 */
53
	protected $thumbnailUrl;
54
55
	/**
56
	 * @var string
57
	 * @since 20.0.0
58
	 */
59
	protected $title;
60
61
	/**
62
	 * @var string
63
	 * @since 20.0.0
64
	 */
65
	protected $subline;
66
67
	/**
68
	 * @var string
69
	 * @since 20.0.0
70
	 */
71
	protected $resourceUrl;
72
73
	/**
74
	 * @var string
75
	 * @since 20.0.0
76
	 */
77
	protected $icon;
78
79
	/**
80
	 * @var boolean
81
	 * @since 20.0.0
82
	 */
83
	protected $rounded;
84
85
	/**
86
	 * @var string[]
87
	 * @psalm-var array<string, string>
88
	 * @since 20.0.0
89
	 */
90
	protected $attributes = [];
91
92
	/**
93
	 * @param string $thumbnailUrl a relative or absolute URL to the thumbnail or icon of the entry
94
	 * @param string $title a main title of the entry
95
	 * @param string $subline the secondary line of the entry
96
	 * @param string $resourceUrl the URL where the user can find the detail, like a deep link inside the app
97
	 * @param string $icon the icon class or url to the icon
98
	 * @param boolean $rounded is the thumbnail rounded
99
	 *
100
	 * @since 20.0.0
101
	 */
102
	public function __construct(string $thumbnailUrl,
103
								string $title,
104
								string $subline,
105
								string $resourceUrl,
106
								string $icon = '',
107
								bool $rounded = false) {
108
		$this->thumbnailUrl = $thumbnailUrl;
109
		$this->title = $title;
110
		$this->subline = $subline;
111
		$this->resourceUrl = $resourceUrl;
112
		$this->icon = $icon;
113
		$this->rounded = $rounded;
114
	}
115
116
	/**
117
	 * Add optional attributes to the result entry, e.g. an ID or some other
118
	 * context information that can be read by the client application
119
	 *
120
	 * @param string $key
121
	 * @param string $value
122
	 *
123
	 * @since 20.0.0
124
	 */
125
	public function addAttribute(string $key, string $value): void {
126
		$this->attributes[$key] = $value;
127
	}
128
129
	/**
130
	 * @return array
131
	 *
132
	 * @since 20.0.0
133
	 */
134
	public function jsonSerialize(): array {
135
		return [
136
			'thumbnailUrl' => $this->thumbnailUrl,
137
			'title' => $this->title,
138
			'subline' => $this->subline,
139
			'resourceUrl' => $this->resourceUrl,
140
			'icon' => $this->icon,
141
			'rounded' => $this->rounded,
142
			'attributes' => $this->attributes,
143
		];
144
	}
145
}
146