aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/formatter/FormatterFactory.php
blob: 5f282f686b95ace2439c6db273be4ce78637f876 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace Shaarli\Formatter;

use Shaarli\Config\ConfigManager;

/**
 * Class FormatterFactory
 *
 * Helper class used to instantiate the proper BookmarkFormatter.
 *
 * @package Shaarli\Formatter
 */
class FormatterFactory
{
    /** @var ConfigManager instance */
    protected $conf;

    /** @var bool */
    protected $isLoggedIn;

    /**
     * FormatterFactory constructor.
     *
     * @param ConfigManager $conf
     * @param bool          $isLoggedIn
     */
    public function __construct(ConfigManager $conf, bool $isLoggedIn)
    {
        $this->conf = $conf;
        $this->isLoggedIn = $isLoggedIn;
    }

    /**
     * Instanciate a BookmarkFormatter depending on the configuration or provided formatter type.
     *
     * @param string|null $type force a specific type regardless of the configuration
     *
     * @return BookmarkFormatter instance.
     */
    public function getFormatter(string $type = null)
    {
        $type = $type ? $type : $this->conf->get('formatter', 'default');
        $className = '\\Shaarli\\Formatter\\Bookmark'. ucfirst($type) .'Formatter';
        if (!class_exists($className)) {
            $className = '\\Shaarli\\Formatter\\BookmarkDefaultFormatter';
        }

        return new $className($this->conf, $this->isLoggedIn);
    }
}