Completed
Branchmaster (8f7741)
created

Transport::__construct()   A

↳ Parent: Transport

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 5
Code Lines 3

Importance

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