]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/formatter/FormatterFactory.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / formatter / FormatterFactory.php
1 <?php
2
3 namespace Shaarli\Formatter;
4
5 use Shaarli\Config\ConfigManager;
6
7 /**
8 * Class FormatterFactory
9 *
10 * Helper class used to instantiate the proper BookmarkFormatter.
11 *
12 * @package Shaarli\Formatter
13 */
14 class FormatterFactory
15 {
16 /** @var ConfigManager instance */
17 protected $conf;
18
19 /** @var bool */
20 protected $isLoggedIn;
21
22 /**
23 * FormatterFactory constructor.
24 *
25 * @param ConfigManager $conf
26 * @param bool $isLoggedIn
27 */
28 public function __construct(ConfigManager $conf, bool $isLoggedIn)
29 {
30 $this->conf = $conf;
31 $this->isLoggedIn = $isLoggedIn;
32 }
33
34 /**
35 * Instanciate a BookmarkFormatter depending on the configuration or provided formatter type.
36 *
37 * @param string|null $type force a specific type regardless of the configuration
38 *
39 * @return BookmarkFormatter instance.
40 */
41 public function getFormatter(string $type = null): BookmarkFormatter
42 {
43 $type = $type ? $type : $this->conf->get('formatter', 'default');
44 $className = '\\Shaarli\\Formatter\\Bookmark' . ucfirst($type) . 'Formatter';
45 if (!class_exists($className)) {
46 $className = '\\Shaarli\\Formatter\\BookmarkDefaultFormatter';
47 }
48
49 return new $className($this->conf, $this->isLoggedIn);
50 }
51 }