]>
Commit | Line | Data |
---|---|---|
336a28fa A |
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 | ||
a39acb25 A |
19 | /** @var bool */ |
20 | protected $isLoggedIn; | |
21 | ||
336a28fa A |
22 | /** |
23 | * FormatterFactory constructor. | |
24 | * | |
25 | * @param ConfigManager $conf | |
a39acb25 | 26 | * @param bool $isLoggedIn |
336a28fa | 27 | */ |
a39acb25 | 28 | public function __construct(ConfigManager $conf, bool $isLoggedIn) |
336a28fa A |
29 | { |
30 | $this->conf = $conf; | |
a39acb25 | 31 | $this->isLoggedIn = $isLoggedIn; |
336a28fa A |
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 | */ | |
301c7ab1 | 41 | public function getFormatter(string $type = null): BookmarkFormatter |
336a28fa A |
42 | { |
43 | $type = $type ? $type : $this->conf->get('formatter', 'default'); | |
53054b2b | 44 | $className = '\\Shaarli\\Formatter\\Bookmark' . ucfirst($type) . 'Formatter'; |
336a28fa A |
45 | if (!class_exists($className)) { |
46 | $className = '\\Shaarli\\Formatter\\BookmarkDefaultFormatter'; | |
47 | } | |
48 | ||
a39acb25 | 49 | return new $className($this->conf, $this->isLoggedIn); |
336a28fa A |
50 | } |
51 | } |