namespace Shaarli\Formatter;
use DateTime;
-use Shaarli\Config\ConfigManager;
use Shaarli\Bookmark\Bookmark;
+use Shaarli\Config\ConfigManager;
/**
* Class BookmarkFormatter
*/
protected $conf;
+ /** @var bool */
+ protected $isLoggedIn;
+
/**
* @var array Additional parameters than can be used for specific formatting
* e.g. index_url for Feed formatting
* LinkDefaultFormatter constructor.
* @param ConfigManager $conf
*/
- public function __construct(ConfigManager $conf)
+ public function __construct(ConfigManager $conf, bool $isLoggedIn)
{
$this->conf = $conf;
+ $this->isLoggedIn = $isLoggedIn;
}
/**
$out['title'] = $this->formatTitle($bookmark);
$out['description'] = $this->formatDescription($bookmark);
$out['thumbnail'] = $this->formatThumbnail($bookmark);
+ $out['urlencoded_taglist'] = $this->formatUrlEncodedTagList($bookmark);
$out['taglist'] = $this->formatTagList($bookmark);
+ $out['urlencoded_tags'] = $this->formatUrlEncodedTagString($bookmark);
$out['tags'] = $this->formatTagString($bookmark);
$out['sticky'] = $bookmark->isSticky();
$out['private'] = $bookmark->isPrivate();
public function addContextData($key, $value)
{
$this->contextData[$key] = $value;
+
+ return $this;
}
/**
*/
protected function formatRealUrl($bookmark)
{
- return $bookmark->getUrl();
+ return $this->formatUrl($bookmark);
}
/**
*/
protected function formatTagList($bookmark)
{
- return $bookmark->getTags();
+ return $this->filterTagList($bookmark->getTags());
+ }
+
+ /**
+ * Format Url Encoded Tags
+ *
+ * @param Bookmark $bookmark instance
+ *
+ * @return array formatted Tags
+ */
+ protected function formatUrlEncodedTagList($bookmark)
+ {
+ return array_map('urlencode', $this->filterTagList($bookmark->getTags()));
}
/**
*/
protected function formatTagString($bookmark)
{
- return implode(' ', $bookmark->getTags());
+ return implode(' ', $this->formatTagList($bookmark));
+ }
+
+ /**
+ * Format TagString
+ *
+ * @param Bookmark $bookmark instance
+ *
+ * @return string formatted TagString
+ */
+ protected function formatUrlEncodedTagString($bookmark)
+ {
+ return implode(' ', $this->formatUrlEncodedTagList($bookmark));
}
/**
}
return 0;
}
+
+ /**
+ * Format tag list, e.g. remove private tags if the user is not logged in.
+ *
+ * @param array $tags
+ *
+ * @return array
+ */
+ protected function filterTagList(array $tags): array
+ {
+ if ($this->isLoggedIn === true) {
+ return $tags;
+ }
+
+ $out = [];
+ foreach ($tags as $tag) {
+ if (strpos($tag, '.') === 0) {
+ continue;
+ }
+
+ $out[] = $tag;
+ }
+
+ return $out;
+ }
}