diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /application/formatter/FormatterFactory.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'application/formatter/FormatterFactory.php')
-rw-r--r-- | application/formatter/FormatterFactory.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/application/formatter/FormatterFactory.php b/application/formatter/FormatterFactory.php new file mode 100644 index 00000000..a029579f --- /dev/null +++ b/application/formatter/FormatterFactory.php | |||
@@ -0,0 +1,51 @@ | |||
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 | } | ||