Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Asset/Transport.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Asset;
3
4
use Redaxscript\Language;
5
use Redaxscript\Registry;
6
use function is_array;
7
use function json_encode;
8
9
/**
10
 * class to transport javascript variables
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Asset
16
 * @author Henry Ruhs
17
 */
18
19
class Transport
20
{
21
	/**
22
	 * instance of the registry class
23
	 *
24
	 * @var Registry
25
	 */
26
27
	protected $_registry;
28
29
	/**
30
	 * instance of the language class
31
	 *
32
	 * @var Language
33
	 */
34
35
	protected $_language;
36
37
	/**
38
	 * registry array to be transported
39
	 *
40
	 * @since 3.0.0
41
	 *
42
	 * @var array
43
	 */
44
45
	protected $_registryArray =
46
	[
47
		'token',
48
		'loggedIn',
49
		'firstParameter',
50
		'secondParameter',
51
		'thirdParameter',
52
		'adminParameter',
53
		'tableParameter',
54
		'idParameter',
55
		'aliasParameter',
56
		'lastParameter',
57
		'firstTable',
58
		'secondTable',
59
		'thirdTable',
60
		'lastTable',
61
		'fullRoute',
62
		'fullTopRoute',
63
		'parameterRoute',
64
		'languageRoute',
65
		'templateRoute',
66
		'refreshRoute',
67
		'language',
68
		'template',
69
		'myBrowser',
70
		'myBrowserVersion',
71
		'myEngine',
72
		'myDesktop',
73
		'myMobile',
74
		'myTablet'
75
	];
76
77
	/**
78
	 * constructor of the class
79
	 *
80
	 * @since 2.4.0
81
	 *
82
	 * @param Registry $registry instance of the registry class
83
	 * @param Language $language instance of the language class
84
	 */
85
86 4
	public function __construct(Registry $registry, Language $language)
87
	{
88 4
		$this->_registry = $registry;
89 4
		$this->_language = $language;
90 4
	}
91
92
	/**
93
	 * get the array
94
	 *
95
	 * @since 3.0.0
96
	 *
97
	 * @return array
98
	 */
99
100 1
	public function getArray() : array
101
	{
102
		$transportArray =
103
		[
104 1
			'baseUrl' => $this->_registry->get('root') . '/',
105 1
			'generator' => $this->_language->get('name', '_package') . ' ' . $this->_language->get('version', '_package'),
106 1
			'language' => $this->_language->get(),
107 1
			'version' => $this->_language->get('version', '_package')
108
		];
109
110
		/* process registry */
111
112 1
		foreach ($this->_registryArray as $value)
113
		{
114 1
			$transportArray['registry'][$value] = $this->_registry->get($value);
115
		}
116 1
		return $transportArray;
117
	}
118
119
	/**
120
	 * render the javascript variables
121
	 *
122
	 * @since 3.0.0
123
	 *
124
	 * @param string $key
125
	 * @param string|array $value
126
	 *
127
	 * @return string|null
128
	 */
129
130 3
	public function render(string $key = null, $value = null) : ?string
131
	{
132 3
		$output = null;
133 3
		if (is_array($value))
134
		{
135 1
			foreach ($value as $keyChildren => $valueChildren)
136
			{
137 1
				$output .= 'window.' . $key . '.' . $keyChildren . ' = ' . json_encode($valueChildren) . ';';
138
			}
139
		}
140 2
		else if ($value)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
141
		{
142 2
			$output = 'window.' . $key . ' = ' . json_encode($value) . ';';
143
		}
144 3
		return $output;
145
	}
146
}