1 | <?php |
||
2 | |||
3 | namespace ByTIC\Assets\Assets\Asset; |
||
4 | |||
5 | use ByTIC\Assets\Assets\Asset; |
||
6 | use Nip\Utility\Arr; |
||
7 | |||
8 | /** |
||
9 | * Class AssetRenderer |
||
10 | * @package ByTIC\Assets\Assets\Asset |
||
11 | */ |
||
12 | class AssetRenderer |
||
13 | { |
||
14 | /** |
||
15 | * @var Asset |
||
16 | */ |
||
17 | protected $asset; |
||
18 | |||
19 | /** |
||
20 | * AssetRenderer constructor. |
||
21 | * @param Asset $asset |
||
22 | */ |
||
23 | 4 | protected function __construct(Asset $asset) |
|
24 | { |
||
25 | 4 | $this->asset = $asset; |
|
26 | 4 | } |
|
27 | |||
28 | /** |
||
29 | * @param Asset|Asset\Traits\CanRenderTrait $asset |
||
30 | * @return string |
||
31 | */ |
||
32 | 4 | public static function for(Asset $asset) |
|
33 | { |
||
34 | 4 | return (new static($asset))->render(); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | 4 | protected function render() |
|
41 | { |
||
42 | 4 | $tag = $this->tag(); |
|
43 | 4 | $attributes = $this->attribString(); |
|
44 | 4 | $content = $this->asset->getContent(); |
|
0 ignored issues
–
show
|
|||
45 | $output = |
||
46 | 4 | sprintf( |
|
47 | 4 | $tag, |
|
48 | 4 | $attributes, |
|
49 | 4 | $content |
|
50 | ); |
||
51 | |||
52 | 4 | return $output; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | 4 | protected function tag() |
|
59 | { |
||
60 | 4 | if ($this->asset->getType() == Asset::TYPE_STYLES && $this->asset->hasContent()) { |
|
61 | 1 | return '<style %s>%s</style>'; |
|
62 | } |
||
63 | 3 | if ($this->asset->getType() == Asset::TYPE_SCRIPTS) { |
|
64 | 1 | return '<script %s>%s</script>'; |
|
65 | } |
||
66 | |||
67 | 2 | return '<link %s />'; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | 4 | protected function attribString() |
|
74 | { |
||
75 | 4 | $attributesMap = $this->prepareAttribs(); |
|
76 | |||
77 | 4 | return implode(' ', array_map( |
|
78 | function ($key, $value) { |
||
79 | 4 | return sprintf('%s="%s"', $key, htmlentities($value)); |
|
80 | 4 | }, |
|
81 | 4 | array_keys($attributesMap), |
|
82 | 4 | $attributesMap |
|
83 | )); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return array |
||
88 | */ |
||
89 | 4 | protected function prepareAttribs() |
|
90 | { |
||
91 | 4 | $attributesMap = $this->asset->getAttribs(); |
|
92 | 4 | if (!$this->asset->getType()) { |
|
93 | return $attributesMap; |
||
94 | } |
||
95 | 4 | $method = 'prepareAttribs'.ucfirst(strtolower($this->asset->getType())); |
|
96 | 4 | if (method_exists($this, $method)) { |
|
97 | 4 | $attributesMap = $this->{$method}($attributesMap); |
|
98 | } |
||
99 | |||
100 | 4 | return $attributesMap; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param array $attributes |
||
105 | * @return array |
||
106 | */ |
||
107 | 3 | protected function prepareAttribsStyles($attributes) |
|
108 | { |
||
109 | 3 | unset($attributes['href']); |
|
110 | 3 | $source = $this->asset->getSource(); |
|
111 | 3 | if ($source) { |
|
112 | 2 | $attributes = Arr::prepend($attributes, $this->asset->getSource(), 'href'); |
|
113 | } |
||
114 | |||
115 | 3 | return $attributes; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param array $attributes |
||
120 | * @return array |
||
121 | */ |
||
122 | 1 | protected function prepareAttribsScripts($attributes) |
|
123 | { |
||
124 | 1 | unset($attributes['src']); |
|
125 | 1 | $source = $this->asset->getSource(); |
|
126 | 1 | if ($source) { |
|
127 | 1 | $attributes = Arr::prepend($attributes, $this->asset->getSource(), 'src'); |
|
128 | } |
||
129 | 1 | return $attributes; |
|
130 | } |
||
131 | } |
||
132 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.