diff options
Diffstat (limited to 'application/formatter/BookmarkFormatter.php')
-rw-r--r-- | application/formatter/BookmarkFormatter.php | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/application/formatter/BookmarkFormatter.php b/application/formatter/BookmarkFormatter.php new file mode 100644 index 00000000..0042dafe --- /dev/null +++ b/application/formatter/BookmarkFormatter.php | |||
@@ -0,0 +1,313 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Formatter; | ||
4 | |||
5 | use DateTime; | ||
6 | use Shaarli\Bookmark\Bookmark; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | |||
9 | /** | ||
10 | * Class BookmarkFormatter | ||
11 | * | ||
12 | * Abstract class processing all bookmark attributes through methods designed to be overridden. | ||
13 | * | ||
14 | * @package Shaarli\Formatter | ||
15 | */ | ||
16 | abstract class BookmarkFormatter | ||
17 | { | ||
18 | /** | ||
19 | * @var ConfigManager | ||
20 | */ | ||
21 | protected $conf; | ||
22 | |||
23 | /** @var bool */ | ||
24 | protected $isLoggedIn; | ||
25 | |||
26 | /** | ||
27 | * @var array Additional parameters than can be used for specific formatting | ||
28 | * e.g. index_url for Feed formatting | ||
29 | */ | ||
30 | protected $contextData = []; | ||
31 | |||
32 | /** | ||
33 | * LinkDefaultFormatter constructor. | ||
34 | * @param ConfigManager $conf | ||
35 | */ | ||
36 | public function __construct(ConfigManager $conf, bool $isLoggedIn) | ||
37 | { | ||
38 | $this->conf = $conf; | ||
39 | $this->isLoggedIn = $isLoggedIn; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Convert a Bookmark into an array usable by templates and plugins. | ||
44 | * | ||
45 | * All Bookmark attributes are formatted through a format method | ||
46 | * that can be overridden in a formatter extending this class. | ||
47 | * | ||
48 | * @param Bookmark $bookmark instance | ||
49 | * | ||
50 | * @return array formatted representation of a Bookmark | ||
51 | */ | ||
52 | public function format($bookmark) | ||
53 | { | ||
54 | $out['id'] = $this->formatId($bookmark); | ||
55 | $out['shorturl'] = $this->formatShortUrl($bookmark); | ||
56 | $out['url'] = $this->formatUrl($bookmark); | ||
57 | $out['real_url'] = $this->formatRealUrl($bookmark); | ||
58 | $out['title'] = $this->formatTitle($bookmark); | ||
59 | $out['description'] = $this->formatDescription($bookmark); | ||
60 | $out['thumbnail'] = $this->formatThumbnail($bookmark); | ||
61 | $out['urlencoded_taglist'] = $this->formatUrlEncodedTagList($bookmark); | ||
62 | $out['taglist'] = $this->formatTagList($bookmark); | ||
63 | $out['urlencoded_tags'] = $this->formatUrlEncodedTagString($bookmark); | ||
64 | $out['tags'] = $this->formatTagString($bookmark); | ||
65 | $out['sticky'] = $bookmark->isSticky(); | ||
66 | $out['private'] = $bookmark->isPrivate(); | ||
67 | $out['class'] = $this->formatClass($bookmark); | ||
68 | $out['created'] = $this->formatCreated($bookmark); | ||
69 | $out['updated'] = $this->formatUpdated($bookmark); | ||
70 | $out['timestamp'] = $this->formatCreatedTimestamp($bookmark); | ||
71 | $out['updated_timestamp'] = $this->formatUpdatedTimestamp($bookmark); | ||
72 | return $out; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Add additional data available to formatters. | ||
77 | * This is used for example to add `index_url` in description's links. | ||
78 | * | ||
79 | * @param string $key Context data key | ||
80 | * @param string $value Context data value | ||
81 | */ | ||
82 | public function addContextData($key, $value) | ||
83 | { | ||
84 | $this->contextData[$key] = $value; | ||
85 | |||
86 | return $this; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Format ID | ||
91 | * | ||
92 | * @param Bookmark $bookmark instance | ||
93 | * | ||
94 | * @return int formatted ID | ||
95 | */ | ||
96 | protected function formatId($bookmark) | ||
97 | { | ||
98 | return $bookmark->getId(); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Format ShortUrl | ||
103 | * | ||
104 | * @param Bookmark $bookmark instance | ||
105 | * | ||
106 | * @return string formatted ShortUrl | ||
107 | */ | ||
108 | protected function formatShortUrl($bookmark) | ||
109 | { | ||
110 | return $bookmark->getShortUrl(); | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * Format Url | ||
115 | * | ||
116 | * @param Bookmark $bookmark instance | ||
117 | * | ||
118 | * @return string formatted Url | ||
119 | */ | ||
120 | protected function formatUrl($bookmark) | ||
121 | { | ||
122 | return $bookmark->getUrl(); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * Format RealUrl | ||
127 | * Legacy: identical to Url | ||
128 | * | ||
129 | * @param Bookmark $bookmark instance | ||
130 | * | ||
131 | * @return string formatted RealUrl | ||
132 | */ | ||
133 | protected function formatRealUrl($bookmark) | ||
134 | { | ||
135 | return $this->formatUrl($bookmark); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Format Title | ||
140 | * | ||
141 | * @param Bookmark $bookmark instance | ||
142 | * | ||
143 | * @return string formatted Title | ||
144 | */ | ||
145 | protected function formatTitle($bookmark) | ||
146 | { | ||
147 | return $bookmark->getTitle(); | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * Format Description | ||
152 | * | ||
153 | * @param Bookmark $bookmark instance | ||
154 | * | ||
155 | * @return string formatted Description | ||
156 | */ | ||
157 | protected function formatDescription($bookmark) | ||
158 | { | ||
159 | return $bookmark->getDescription(); | ||
160 | } | ||
161 | |||
162 | /** | ||
163 | * Format Thumbnail | ||
164 | * | ||
165 | * @param Bookmark $bookmark instance | ||
166 | * | ||
167 | * @return string formatted Thumbnail | ||
168 | */ | ||
169 | protected function formatThumbnail($bookmark) | ||
170 | { | ||
171 | return $bookmark->getThumbnail(); | ||
172 | } | ||
173 | |||
174 | /** | ||
175 | * Format Tags | ||
176 | * | ||
177 | * @param Bookmark $bookmark instance | ||
178 | * | ||
179 | * @return array formatted Tags | ||
180 | */ | ||
181 | protected function formatTagList($bookmark) | ||
182 | { | ||
183 | return $this->filterTagList($bookmark->getTags()); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * Format Url Encoded Tags | ||
188 | * | ||
189 | * @param Bookmark $bookmark instance | ||
190 | * | ||
191 | * @return array formatted Tags | ||
192 | */ | ||
193 | protected function formatUrlEncodedTagList($bookmark) | ||
194 | { | ||
195 | return array_map('urlencode', $this->filterTagList($bookmark->getTags())); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * Format TagString | ||
200 | * | ||
201 | * @param Bookmark $bookmark instance | ||
202 | * | ||
203 | * @return string formatted TagString | ||
204 | */ | ||
205 | protected function formatTagString($bookmark) | ||
206 | { | ||
207 | return implode(' ', $this->formatTagList($bookmark)); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Format TagString | ||
212 | * | ||
213 | * @param Bookmark $bookmark instance | ||
214 | * | ||
215 | * @return string formatted TagString | ||
216 | */ | ||
217 | protected function formatUrlEncodedTagString($bookmark) | ||
218 | { | ||
219 | return implode(' ', $this->formatUrlEncodedTagList($bookmark)); | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * Format Class | ||
224 | * Used to add specific CSS class for a link | ||
225 | * | ||
226 | * @param Bookmark $bookmark instance | ||
227 | * | ||
228 | * @return string formatted Class | ||
229 | */ | ||
230 | protected function formatClass($bookmark) | ||
231 | { | ||
232 | return $bookmark->isPrivate() ? 'private' : ''; | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * Format Created | ||
237 | * | ||
238 | * @param Bookmark $bookmark instance | ||
239 | * | ||
240 | * @return DateTime instance | ||
241 | */ | ||
242 | protected function formatCreated(Bookmark $bookmark) | ||
243 | { | ||
244 | return $bookmark->getCreated(); | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * Format Updated | ||
249 | * | ||
250 | * @param Bookmark $bookmark instance | ||
251 | * | ||
252 | * @return DateTime instance | ||
253 | */ | ||
254 | protected function formatUpdated(Bookmark $bookmark) | ||
255 | { | ||
256 | return $bookmark->getUpdated(); | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * Format CreatedTimestamp | ||
261 | * | ||
262 | * @param Bookmark $bookmark instance | ||
263 | * | ||
264 | * @return int formatted CreatedTimestamp | ||
265 | */ | ||
266 | protected function formatCreatedTimestamp(Bookmark $bookmark) | ||
267 | { | ||
268 | if (! empty($bookmark->getCreated())) { | ||
269 | return $bookmark->getCreated()->getTimestamp(); | ||
270 | } | ||
271 | return 0; | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * Format UpdatedTimestamp | ||
276 | * | ||
277 | * @param Bookmark $bookmark instance | ||
278 | * | ||
279 | * @return int formatted UpdatedTimestamp | ||
280 | */ | ||
281 | protected function formatUpdatedTimestamp(Bookmark $bookmark) | ||
282 | { | ||
283 | if (! empty($bookmark->getUpdated())) { | ||
284 | return $bookmark->getUpdated()->getTimestamp(); | ||
285 | } | ||
286 | return 0; | ||
287 | } | ||
288 | |||
289 | /** | ||
290 | * Format tag list, e.g. remove private tags if the user is not logged in. | ||
291 | * | ||
292 | * @param array $tags | ||
293 | * | ||
294 | * @return array | ||
295 | */ | ||
296 | protected function filterTagList(array $tags): array | ||
297 | { | ||
298 | if ($this->isLoggedIn === true) { | ||
299 | return $tags; | ||
300 | } | ||
301 | |||
302 | $out = []; | ||
303 | foreach ($tags as $tag) { | ||
304 | if (strpos($tag, '.') === 0) { | ||
305 | continue; | ||
306 | } | ||
307 | |||
308 | $out[] = $tag; | ||
309 | } | ||
310 | |||
311 | return $out; | ||
312 | } | ||
313 | } | ||