]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - application/formatter/BookmarkFormatter.php
ShaarliParsedown: add PHPDoc/comments
[github/shaarli/Shaarli.git] / application / formatter / BookmarkFormatter.php
... / ...
CommitLineData
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTimeInterface;
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Config\ConfigManager;
8
9/**
10 * Class BookmarkFormatter
11 *
12 * Abstract class processing all bookmark attributes through methods designed to be overridden.
13 *
14 * List of available formatted fields:
15 * - id ID
16 * - shorturl Unique identifier, used in permalinks
17 * - url URL, can be altered in some way, e.g. passing through an HTTP reverse proxy
18 * - real_url (legacy) same as `url`
19 * - url_html URL to be displayed in HTML content (it can contain HTML tags)
20 * - title Title
21 * - title_html Title to be displayed in HTML content (it can contain HTML tags)
22 * - description Description content. It most likely contains HTML tags
23 * - thumbnail Thumbnail: path to local cache file, false if there is none, null if hasn't been retrieved
24 * - taglist List of tags (array)
25 * - taglist_urlencoded List of tags (array) URL encoded: it must be used to create a link to a URL containing a tag
26 * - taglist_html List of tags (array) to be displayed in HTML content (it can contain HTML tags)
27 * - tags Tags separated by a single whitespace
28 * - tags_urlencoded Tags separated by a single whitespace, URL encoded: must be used to create a link
29 * - sticky Is sticky (bool)
30 * - private Is private (bool)
31 * - class Additional CSS class
32 * - created Creation DateTime
33 * - updated Last edit DateTime
34 * - timestamp Creation timestamp
35 * - updated_timestamp Last edit timestamp
36 *
37 * @package Shaarli\Formatter
38 */
39abstract class BookmarkFormatter
40{
41 /**
42 * @var ConfigManager
43 */
44 protected $conf;
45
46 /** @var bool */
47 protected $isLoggedIn;
48
49 /**
50 * @var array Additional parameters than can be used for specific formatting
51 * e.g. index_url for Feed formatting
52 */
53 protected $contextData = [];
54
55 /**
56 * LinkDefaultFormatter constructor.
57 * @param ConfigManager $conf
58 */
59 public function __construct(ConfigManager $conf, bool $isLoggedIn)
60 {
61 $this->conf = $conf;
62 $this->isLoggedIn = $isLoggedIn;
63 }
64
65 /**
66 * Convert a Bookmark into an array usable by templates and plugins.
67 *
68 * All Bookmark attributes are formatted through a format method
69 * that can be overridden in a formatter extending this class.
70 *
71 * @param Bookmark $bookmark instance
72 *
73 * @return array formatted representation of a Bookmark
74 */
75 public function format($bookmark)
76 {
77 $out['id'] = $this->formatId($bookmark);
78 $out['shorturl'] = $this->formatShortUrl($bookmark);
79 $out['url'] = $this->formatUrl($bookmark);
80 $out['real_url'] = $this->formatRealUrl($bookmark);
81 $out['url_html'] = $this->formatUrlHtml($bookmark);
82 $out['title'] = $this->formatTitle($bookmark);
83 $out['title_html'] = $this->formatTitleHtml($bookmark);
84 $out['description'] = $this->formatDescription($bookmark);
85 $out['thumbnail'] = $this->formatThumbnail($bookmark);
86 $out['taglist'] = $this->formatTagList($bookmark);
87 $out['taglist_urlencoded'] = $this->formatTagListUrlEncoded($bookmark);
88 $out['taglist_html'] = $this->formatTagListHtml($bookmark);
89 $out['tags'] = $this->formatTagString($bookmark);
90 $out['tags_urlencoded'] = $this->formatTagStringUrlEncoded($bookmark);
91 $out['sticky'] = $bookmark->isSticky();
92 $out['private'] = $bookmark->isPrivate();
93 $out['class'] = $this->formatClass($bookmark);
94 $out['created'] = $this->formatCreated($bookmark);
95 $out['updated'] = $this->formatUpdated($bookmark);
96 $out['timestamp'] = $this->formatCreatedTimestamp($bookmark);
97 $out['updated_timestamp'] = $this->formatUpdatedTimestamp($bookmark);
98
99 return $out;
100 }
101
102 /**
103 * Add additional data available to formatters.
104 * This is used for example to add `index_url` in description's links.
105 *
106 * @param string $key Context data key
107 * @param string $value Context data value
108 */
109 public function addContextData($key, $value)
110 {
111 $this->contextData[$key] = $value;
112
113 return $this;
114 }
115
116 /**
117 * Format ID
118 *
119 * @param Bookmark $bookmark instance
120 *
121 * @return int formatted ID
122 */
123 protected function formatId($bookmark)
124 {
125 return $bookmark->getId();
126 }
127
128 /**
129 * Format ShortUrl
130 *
131 * @param Bookmark $bookmark instance
132 *
133 * @return string formatted ShortUrl
134 */
135 protected function formatShortUrl($bookmark)
136 {
137 return $bookmark->getShortUrl();
138 }
139
140 /**
141 * Format Url
142 *
143 * @param Bookmark $bookmark instance
144 *
145 * @return string formatted Url
146 */
147 protected function formatUrl($bookmark)
148 {
149 return $bookmark->getUrl();
150 }
151
152 /**
153 * Format RealUrl
154 * Legacy: identical to Url
155 *
156 * @param Bookmark $bookmark instance
157 *
158 * @return string formatted RealUrl
159 */
160 protected function formatRealUrl($bookmark)
161 {
162 return $this->formatUrl($bookmark);
163 }
164
165 /**
166 * Format Url Html: to be displayed in HTML content, it can contains HTML tags.
167 *
168 * @param Bookmark $bookmark instance
169 *
170 * @return string formatted Url HTML
171 */
172 protected function formatUrlHtml($bookmark)
173 {
174 return $this->formatUrl($bookmark);
175 }
176
177 /**
178 * Format Title
179 *
180 * @param Bookmark $bookmark instance
181 *
182 * @return string formatted Title
183 */
184 protected function formatTitle($bookmark)
185 {
186 return $bookmark->getTitle();
187 }
188
189 /**
190 * Format Title HTML: to be displayed in HTML content, it can contains HTML tags.
191 *
192 * @param Bookmark $bookmark instance
193 *
194 * @return string formatted Title
195 */
196 protected function formatTitleHtml($bookmark)
197 {
198 return $bookmark->getTitle();
199 }
200
201 /**
202 * Format Description
203 *
204 * @param Bookmark $bookmark instance
205 *
206 * @return string formatted Description
207 */
208 protected function formatDescription($bookmark)
209 {
210 return $bookmark->getDescription();
211 }
212
213 /**
214 * Format Thumbnail
215 *
216 * @param Bookmark $bookmark instance
217 *
218 * @return string formatted Thumbnail
219 */
220 protected function formatThumbnail($bookmark)
221 {
222 return $bookmark->getThumbnail();
223 }
224
225 /**
226 * Format Tags
227 *
228 * @param Bookmark $bookmark instance
229 *
230 * @return array formatted Tags
231 */
232 protected function formatTagList($bookmark)
233 {
234 return $this->filterTagList($bookmark->getTags());
235 }
236
237 /**
238 * Format Url Encoded Tags
239 *
240 * @param Bookmark $bookmark instance
241 *
242 * @return array formatted Tags
243 */
244 protected function formatTagListUrlEncoded($bookmark)
245 {
246 return array_map('urlencode', $this->filterTagList($bookmark->getTags()));
247 }
248
249 /**
250 * Format Tags HTML: to be displayed in HTML content, it can contains HTML tags.
251 *
252 * @param Bookmark $bookmark instance
253 *
254 * @return array formatted Tags
255 */
256 protected function formatTagListHtml($bookmark)
257 {
258 return $this->formatTagList($bookmark);
259 }
260
261 /**
262 * Format TagString
263 *
264 * @param Bookmark $bookmark instance
265 *
266 * @return string formatted TagString
267 */
268 protected function formatTagString($bookmark)
269 {
270 return implode($this->conf->get('general.tags_separator', ' '), $this->formatTagList($bookmark));
271 }
272
273 /**
274 * Format TagString
275 *
276 * @param Bookmark $bookmark instance
277 *
278 * @return string formatted TagString
279 */
280 protected function formatTagStringUrlEncoded($bookmark)
281 {
282 return implode(' ', $this->formatTagListUrlEncoded($bookmark));
283 }
284
285 /**
286 * Format Class
287 * Used to add specific CSS class for a link
288 *
289 * @param Bookmark $bookmark instance
290 *
291 * @return string formatted Class
292 */
293 protected function formatClass($bookmark)
294 {
295 return $bookmark->isPrivate() ? 'private' : '';
296 }
297
298 /**
299 * Format Created
300 *
301 * @param Bookmark $bookmark instance
302 *
303 * @return DateTimeInterface instance
304 */
305 protected function formatCreated(Bookmark $bookmark)
306 {
307 return $bookmark->getCreated();
308 }
309
310 /**
311 * Format Updated
312 *
313 * @param Bookmark $bookmark instance
314 *
315 * @return DateTimeInterface instance
316 */
317 protected function formatUpdated(Bookmark $bookmark)
318 {
319 return $bookmark->getUpdated();
320 }
321
322 /**
323 * Format CreatedTimestamp
324 *
325 * @param Bookmark $bookmark instance
326 *
327 * @return int formatted CreatedTimestamp
328 */
329 protected function formatCreatedTimestamp(Bookmark $bookmark)
330 {
331 if (! empty($bookmark->getCreated())) {
332 return $bookmark->getCreated()->getTimestamp();
333 }
334 return 0;
335 }
336
337 /**
338 * Format UpdatedTimestamp
339 *
340 * @param Bookmark $bookmark instance
341 *
342 * @return int formatted UpdatedTimestamp
343 */
344 protected function formatUpdatedTimestamp(Bookmark $bookmark)
345 {
346 if (! empty($bookmark->getUpdated())) {
347 return $bookmark->getUpdated()->getTimestamp();
348 }
349 return 0;
350 }
351
352 /**
353 * Format tag list, e.g. remove private tags if the user is not logged in.
354 * TODO: this method is called multiple time to format tags, the result should be cached.
355 *
356 * @param array $tags
357 *
358 * @return array
359 */
360 protected function filterTagList(array $tags): array
361 {
362 if ($this->isLoggedIn === true) {
363 return $tags;
364 }
365
366 $out = [];
367 foreach ($tags as $tag) {
368 if (strpos($tag, '.') === 0) {
369 continue;
370 }
371
372 $out[] = $tag;
373 }
374
375 return $out;
376 }
377}