Passed
Push — main ( 17ed8d...71af5c )
by Thierry
03:29 queued 01:55
created

LibraryHelper::getJsOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Jaxon\Dialogs\Dialog;
4
5
use Jaxon\App\Config\ConfigManager;
6
use Jaxon\App\Dialog\Library\LibraryInterface;
7
use Jaxon\Utils\Template\TemplateEngine;
8
9
use function is_array;
10
use function rtrim;
11
use function trim;
12
13
class LibraryHelper
14
{
15
    /**
16
     * The name of the library
17
     *
18
     * @var string
19
     */
20
    protected $sName = '';
21
22
    /**
23
     * The URI where to get the library files from
24
     *
25
     * @var string
26
     */
27
    protected $sUri = '';
28
29
    /**
30
     * The constructor
31
     *
32
     * @param LibraryInterface $xDialogLibrary
33
     * @param ConfigManager $xConfigManager
34
     * @param TemplateEngine $xTemplateEngine
35
     */
36
    public function __construct(LibraryInterface $xDialogLibrary,
37
        private ConfigManager $xConfigManager, private TemplateEngine $xTemplateEngine)
38
    {
39
        // Set the library name
40
        $this->sName = $xDialogLibrary->getName();
41
        // Set the default URI.
42
        $sDefaultUri = $xConfigManager->getAppOption('dialogs.lib.uri', $xDialogLibrary->getUri());
43
        // Set the library URI.
44
        $this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/');
45
    }
46
47
    /**
48
     * Get the value of a config option
49
     *
50
     * @param string $sOptionName The option name
51
     * @param mixed $xDefault The default value, to be returned if the option is not defined
52
     *
53
     * @return mixed
54
     */
55
    public function getOption(string $sOptionName, $xDefault = null)
56
    {
57
        $sOptionName = "dialogs.{$this->sName}.$sOptionName";
58
        return $this->xConfigManager->getAppOption($sOptionName, $xDefault);
59
    }
60
61
    /**
62
     * Check the presence of a config option
63
     *
64
     * @param string $sOptionName The option name
65
     *
66
     * @return bool
67
     */
68
    public function hasOption(string $sOptionName): bool
69
    {
70
        return $this->xConfigManager->hasAppOption("dialogs.{$this->sName}.$sOptionName");
71
    }
72
73
    /**
74
     * Get the names of the options matching a given prefix
75
     *
76
     * @param string $sPrefix The prefix to match
77
     *
78
     * @return array
79
     */
80
    public function getOptionNames(string $sPrefix): array
81
    {
82
        // The options names are relative to the plugin in Dialogs configuration
83
        return $this->xConfigManager->getOptionNames("dialogs.{$this->sName}.$sPrefix");
84
    }
85
86
    /**
87
     * Get the options of the js library
88
     *
89
     * @return array
90
     */
91
    public function getJsOptions(): array
92
    {
93
        $xOptions = $this->xConfigManager->getAppOption("dialogs.{$this->sName}.options", []);
94
        return is_array($xOptions) ? $xOptions : [];
95
    }
96
97
    /**
98
     * @param string $sOption The assets option name
99
     * @param string $sFile The javascript file name
100
     *
101
     * @return string|null
102
     */
103
    private function getAssetUri(string $sOption, string $sFile): ?string
104
    {
105
        return !$this->hasOption($sOption) ? "{$this->sUri}/$sFile" :
106
            (trim($this->getOption($sOption)) ?: null);
0 ignored issues
show
Bug introduced by
It seems like $this->getOption($sOption) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            (trim(/** @scrutinizer ignore-type */ $this->getOption($sOption)) ?: null);
Loading history...
107
    }
108
109
    /**
110
     * Get the javascript HTML header code
111
     *
112
     * @param string $sFile The javascript file name
113
     *
114
     * @return string
115
     */
116
    public function getJsCode(string $sFile): string
117
    {
118
        // If this 'assets.js' option is defined and evaluates to false, then the asset is not displayed.
119
        $sUri = $this->getAssetUri('assets.js', $sFile);
120
        return !$sUri ? '' : '<script type="text/javascript" src="' . $sUri . '"></script>';
121
    }
122
123
    /**
124
     * Get the CSS HTML header code
125
     *
126
     * @param string $sFile The CSS file name
127
     *
128
     * @return string
129
     */
130
    public function getCssCode(string $sFile): string
131
    {
132
        // If this 'assets.css' option is defined and evaluates to false, then the asset is not displayed.
133
        $sUri = $this->getAssetUri('assets.css', $sFile);
134
        return !$sUri ? '' : '<link rel="stylesheet" href="' . $sUri . '" />';
135
    }
136
137
    /**
138
     * Render a template
139
     *
140
     * @param string $sTemplate The name of template to be rendered
141
     * @param array $aVars The template vars
142
     *
143
     * @return string
144
     */
145
    public function render(string $sTemplate, array $aVars = []): string
146
    {
147
        return $this->xTemplateEngine->render("jaxon::dialogs::$sTemplate", $aVars);
148
    }
149
}
150