]>
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 | ||
19 | /** | |
20 | * FormatterFactory constructor. | |
21 | * | |
22 | * @param ConfigManager $conf | |
23 | */ | |
24 | public function __construct(ConfigManager $conf) | |
25 | { | |
26 | $this->conf = $conf; | |
27 | } | |
28 | ||
29 | /** | |
30 | * Instanciate a BookmarkFormatter depending on the configuration or provided formatter type. | |
31 | * | |
32 | * @param string|null $type force a specific type regardless of the configuration | |
33 | * | |
34 | * @return BookmarkFormatter instance. | |
35 | */ | |
36 | public function getFormatter($type = null) | |
37 | { | |
38 | $type = $type ? $type : $this->conf->get('formatter', 'default'); | |
39 | $className = '\\Shaarli\\Formatter\\Bookmark'. ucfirst($type) .'Formatter'; | |
40 | if (!class_exists($className)) { | |
41 | $className = '\\Shaarli\\Formatter\\BookmarkDefaultFormatter'; | |
42 | } | |
43 | ||
44 | return new $className($this->conf); | |
45 | } | |
46 | } |