Passed
Branch main (6957a7)
by Moises
07:57 queued 06:04
created

Assets::setScriptPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace DevPontes\View;
4
5
/**
6
 * Class DevPontes Assets
7
 *
8
 * @author Moises Pontes <[email protected]>
9
 * @package DevPontes\View
10
 */
11
class Assets
12
{
13
    /** @var string */
14
    private $styles;
15
16
    /** @var string */
17
    private $scripts;
18
19
    /** @var string */
20
    private $stylePath;
21
22
    /** @var string */
23
    private $scriptPath;
24
25
    /** @var string */
26
    private $source;
27
28
    /** @var bool */
29
    private $cache;
30
31
    /**
32
     * Assets constructor.
33
     *
34
     * @param string $src path to resources folder
35
     * @param boolean $cache [optional] cache on/off, defaults true
36
     * @example - new Assets('assets', false);
37
     */
38
    public function __construct(string $src, bool $cache = true)
39
    {
40
        $this->source = $src;
41
        $this->cache = $cache;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getStyles(): string
48
    {
49
        return $this->styles;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getScripts(): string
56
    {
57
        return $this->scripts;
58
    }
59
60
    /**
61
     * Set path for styles
62
     *
63
     * @param string $stylePath
64
     * @return View
65
     */
66
    public function setStylePath(string $stylePath): Assets
67
    {
68
        $this->stylePath = $stylePath;
69
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type DevPontes\View\Assets which is incompatible with the documented return type DevPontes\View\View.
Loading history...
70
    }
71
72
    /**
73
     * Set path for scripts
74
     *
75
     * @param string $scriptPath
76
     * @return View
77
     */
78
    public function setScriptPath(string $scriptPath): Assets
79
    {
80
        $this->scriptPath = $scriptPath;
81
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type DevPontes\View\Assets which is incompatible with the documented return type DevPontes\View\View.
Loading history...
82
    }
83
84
    /**
85
     * Make CSS styles
86
     *
87
     * @param array $css array of styles
88
     * @return Assets
89
     * @example - $a->makeStyle($css['global', ...]);
90
     */
91
    public function makeStyle(array $css): Assets
92
    {
93
        $version = $this->cache ? "" : "?v=" . time();
94
        $stylePath = empty($this->stylePath) ? "css" : $this->stylePath;
95
96
        if ($css) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $css of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            foreach ($css as $style) {
98
                $this->styles .= "<link href='{$this->source}/{$stylePath}/{$style}.css{$version}' rel='stylesheet'>\n    ";
99
            }
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * Make JS scripts
107
     *
108
     * @param array $js array of scripts
109
     * @return Assets
110
     * @example - $a->makeScript($js['global', ...]);
111
     */
112
    public function makeScript(array $js): Assets
113
    {
114
        $version = $this->cache ? "" : "?v=" . time();
115
        $scriptPath = empty($this->scriptPath) ? "js" : $this->scriptPath;
116
117
        if ($js) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $js of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
118
            foreach ($js as $script) {
119
                $this->scripts .= "<script src='{$this->source}/{$scriptPath}/{$script}.js{$version}'></script>\n    ";
120
            }
121
        }
122
123
        return $this;
124
    }
125
}
126