Completed
Push — master ( 14c945...6811c1 )
by Henry
05:37
created

Script::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.2621

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 9
cts 13
cp 0.6923
rs 9.392
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3.2621
1
<?php
2
namespace Redaxscript\Head;
3
4
use Redaxscript\Asset;
5
use Redaxscript\Html;
6
use Redaxscript\Language;
7
use Redaxscript\Registry;
8
use function array_merge;
9
10
/**
11
 * children class to create the script tag
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Head
17
 * @author Henry Ruhs
18
 */
19
20
class Script extends HeadAbstract
21
{
22
	/**
23
	 * options of the script
24
	 *
25
	 * @var string
26
	 */
27
28
	protected static $_optionArray =
29
	[
30
		'directory' => 'cache/scripts',
31
		'extension' => 'js',
32
		'attribute' => 'src',
33
		'lifetime' => 3600
34
	];
35
36
	/**
37
	 * inline script
38
	 *
39
	 * @var string
40
	 */
41
42
	protected static $_inline;
43
44
	/**
45
	 * append script file
46
	 *
47
	 * @since 3.0.0
48
	 *
49
	 * @param string|array $source
50
	 *
51
	 * @return self
52
	 */
53
54
	public function appendFile($source = null) : self
55
	{
56
		foreach ((array)$source as $value)
57
		{
58
			$this->append('src', $value);
59
		}
60
		return $this;
61
	}
62
63
	/**
64
	 * prepend script file
65
	 *
66
	 * @since 3.0.0
67
	 *
68
	 * @param string|array $source
69
	 *
70
	 * @return self
71
	 */
72
73
	public function prependFile($source = null) : self
74
	{
75
		foreach ((array)$source as $value)
76
		{
77
			$this->prepend('src', $value);
78
		}
79
		return $this;
80
	}
81
82
	/**
83
	 * remove script file
84
	 *
85
	 * @since 3.0.0
86
	 *
87
	 * @param string|array $source
88
	 *
89
	 * @return self
90
	 */
91
92
	public function removeFile($source = null) : self
93
	{
94
		foreach ((array)$source as $value)
95
		{
96
			$this->remove('src', $value);
97
		}
98
		return $this;
99
	}
100
101
	/**
102
	 * append inline script
103
	 *
104
	 * @since 3.0.0
105
	 *
106
	 * @param string $inline
107
	 *
108
	 * @return self
109
	 */
110
111
	public function appendInline(string $inline = null) : self
112
	{
113
		self::$_inline .= $inline;
114
		return $this;
115
	}
116
117
	/**
118
	 * prepend inline script
119
	 *
120
	 * @since 3.0.0
121
	 *
122
	 * @param string $inline
123
	 *
124
	 * @return self
125
	 */
126
127
	public function prependInline(string $inline = null) : self
128
	{
129
		self::$_inline = $inline . self::$_inline;
130
		return $this;
131
	}
132
133
	/**
134
	 * transport javascript variables
135
	 *
136
	 * @since 3.0.0
137
	 *
138
	 * @param string $key
139
	 * @param string|array $value
140
	 *
141
	 * @return self
142
	 */
143
144
	public function transportVar(string $key = null, $value = null) : self
145
	{
146
		$transport = new Asset\Transport(Registry::getInstance(), Language::getInstance());
147
		$inline = $transport->render($key, $value);
148
		$this->appendInline($inline);
149
		return $this;
150
	}
151
152
	/**
153
	 * concat the script
154
	 *
155
	 * @since 3.0.0
156
	 *
157
	 * @param array $optionArray
158
	 *
159
	 * @return self
160
	 */
161
162
	public function concat(array $optionArray = []) : self
163
	{
164
		$optionArray = array_merge(self::$_optionArray, $optionArray);
165
		$loader = new Asset\Loader(Registry::getInstance());
166
		$loader
167
			->init($this->_getCollectionArray())
168
			->concat($optionArray);
169
170
		/* update collection */
171
172
		$this->_setCollectionArray($loader->getCollectionArray());
173
		return $this;
174
	}
175
176
	/**
177
	 * render the script
178
	 *
179
	 * @since 3.0.0
180
	 *
181
	 * @return string|null
182
	 */
183
184 1
	public function render() : ?string
185
	{
186 1
		$output = null;
187
188
		/* html element */
189
190 1
		$scriptElement = new Html\Element();
191 1
		$scriptElement->init('script');
192
193
		/* handle collection */
194
195 1
		$collectionArray = $this->_getCollectionArray();
196
197
		/* process collection */
198
199 1
		foreach ($collectionArray as $attribute)
200
		{
201
			$output .= $scriptElement
202
				->copy()
203
				->attr($attribute);
204
		}
205
206
		/* collect inline */
207
208 1
		if (self::$_inline)
209
		{
210
			$output .= $scriptElement
211
				->copy()
212
				->text(self::$_inline);
213
		}
214 1
		$this->clear();
215 1
		return $output;
216
	}
217
218
	/**
219
	 * clear the script
220
	 *
221
	 * @since 3.0.0
222
	 *
223
	 * @return HeadAbstract
224
	 */
225
226 1
	public function clear() : HeadAbstract
227
	{
228 1
		self::$_inline = null;
229 1
		return parent::clear();
230
	}
231
}
232