Completed
Push — master ( 532fa7...558f34 )
by Josh
19:31
created

Encoder   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 239
ccs 72
cts 72
cp 1
rs 10
c 2
b 1
f 0
wmc 28
lcom 2
cbo 3

15 Methods

Rating   Name   Duplication   Size   Complexity  
A encodeBoolean() 0 4 2
A encodeCode() 0 4 1
A encodeConfigValue() 0 4 2
A encodeDictionary() 0 4 1
A encodeIndexedArray() 0 4 1
A encodePropertyName() 0 4 3
A encodeRegexp() 0 4 1
A encodeScalar() 0 4 1
A __construct() 0 18 1
A encode() 0 10 2
A encodeAssociativeArray() 0 14 2
A encodeObject() 0 12 3
A encodeArray() 0 4 2
A isIndexedArray() 0 14 4
A isLegalProp() 0 15 2
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\JavaScript;
9
10
use RuntimeException;
11
use s9e\TextFormatter\Configurator\Items\Regexp;
12
use s9e\TextFormatter\Configurator\JavaScript\Code;
13
use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
14
use s9e\TextFormatter\Configurator\JavaScript\Encoder;
15
16
class Encoder
17
{
18
	/**
19
	* @var callable[]
20
	*/
21
	public $objectEncoders;
22
23
	/**
24
	* @var callable[]
25
	*/
26
	public $typeEncoders;
27
28
	/**
29
	* Constructor
30
	*
31
	* Will set up the default encoders
32
	*/
33 24
	public function __construct()
34
	{
35 24
		$ns = 's9e\\TextFormatter\\Configurator\\';
36 24
		$this->objectEncoders = [
37 24
			$ns . 'Items\\Regexp'           => [$this, 'encodeRegexp'],
38 24
			$ns . 'JavaScript\\Code'        => [$this, 'encodeCode'],
39 24
			$ns . 'JavaScript\\ConfigValue' => [$this, 'encodeConfigValue'],
40 24
			$ns . 'JavaScript\\Dictionary'  => [$this, 'encodeDictionary']
41 24
		];
42 24
		$this->typeEncoders = [
43 24
			'array'   => [$this, 'encodeArray'],
44 24
			'boolean' => [$this, 'encodeBoolean'],
45 24
			'double'  => [$this, 'encodeScalar'],
46 24
			'integer' => [$this, 'encodeScalar'],
47 24
			'object'  => [$this, 'encodeObject'],
48 24
			'string'  => [$this, 'encodeScalar']
49 24
		];
50 24
	}
51
52
	/**
53
	* Encode a value into JavaScript
54
	*
55
	* @param  mixed  $value
56
	* @return string
57
	*/
58 24
	public function encode($value)
59
	{
60 24
		$type = gettype($value);
61 24
		if (!isset($this->typeEncoders[$type]))
62 24
		{
63 1
			throw new RuntimeException('Cannot encode ' . $type . ' value');
64
		}
65
66 23
		return $this->typeEncoders[$type]($value);
67
	}
68
69
	/**
70
	* Encode an array to JavaScript
71
	*
72
	* @param  array  $array
73
	* @return string
74
	*/
75 9
	protected function encodeArray(array $array)
76
	{
77 9
		return ($this->isIndexedArray($array)) ? $this->encodeIndexedArray($array) : $this->encodeAssociativeArray($array);
78
	}
79
80
	/**
81
	* Encode an associative array to JavaScript
82
	*
83
	* @param  array  $array
84
	* @param  bool   $preserveNames
85
	* @return string
86
	*/
87 11
	protected function encodeAssociativeArray(array $array, $preserveNames = false)
88
	{
89 11
		ksort($array);
90 11
		$src = '{';
91 11
		$sep = '';
92 11
		foreach ($array as $k => $v)
93
		{
94 11
			$src .= $sep . $this->encodePropertyName("$k", $preserveNames) . ':' . $this->encode($v);
95 11
			$sep = ',';
96 11
		}
97 11
		$src .= '}';
98
99 11
		return $src;
100
	}
101
102
	/**
103
	* Encode a boolean value into JavaScript
104
	*
105
	* @param  bool   $value
106
	* @return string
107
	*/
108 3
	protected function encodeBoolean($value)
109
	{
110 3
		return ($value) ? '!0' : '!1';
111
	}
112
113
	/**
114
	* Encode a Code instance into JavaScript
115
	*
116
	* @param  Code   $code
117
	* @return string
118
	*/
119 1
	protected function encodeCode(Code $code)
120
	{
121 1
		return (string) $code;
122
	}
123
124
	/**
125
	* Encode a ConfigValue instance into JavaScript
126
	*
127
	* @param  ConfigValue $configValue
128
	* @return string
129
	*/
130 2
	protected function encodeConfigValue(ConfigValue $configValue)
131
	{
132 2
		return ($configValue->isDeduplicated()) ? $configValue->getVarName() : $this->encode($configValue->getValue());
133
	}
134
135
	/**
136
	* Encode a Dictionary object into a JavaScript object
137
	*
138
	* @param  Dictionary $dict
139
	* @return string
140
	*/
141 6
	protected function encodeDictionary(Dictionary $dict)
142
	{
143 6
		return $this->encodeAssociativeArray($dict->getArrayCopy(), true);
144
	}
145
146
	/**
147
	* Encode an indexed array to JavaScript
148
	*
149
	* @param  array  $array
150
	* @return string
151
	*/
152 4
	protected function encodeIndexedArray(array $array)
153
	{
154 4
		return '[' . implode(',', array_map([$this, 'encode'], $array)) . ']';
155
	}
156
157
	/**
158
	* Encode an object into JavaScript
159
	*
160
	* @param  object $object
161
	* @return string
162
	*/
163 11
	protected function encodeObject($object)
164
	{
165 11
		foreach ($this->objectEncoders as $className => $callback)
166
		{
167 11
			if ($object instanceof $className)
168 11
			{
169 10
				return $callback($object);
170
			}
171 10
		}
172
173 1
		throw new RuntimeException('Cannot encode instance of ' . get_class($object));
174
	}
175
176
	/**
177
	* Encode an object property name into JavaScript
178
	*
179
	* @param  string $name
180
	* @param  bool   $preserveNames
181
	* @return string
182
	*/
183 11
	protected function encodePropertyName($name, $preserveNames)
184
	{
185 11
		return ($preserveNames || !$this->isLegalProp($name)) ? json_encode($name) : $name;
186
	}
187
188
	/**
189
	* Encode a Regexp object into JavaScript
190
	*
191
	* @param  Regexp $regexp
192
	* @return string
193
	*/
194 1
	protected function encodeRegexp(Regexp $regexp)
195
	{
196 1
		return (string) $regexp->toJS();
197
	}
198
199
	/**
200
	* Encode a scalar value into JavaScript
201
	*
202
	* @param  mixed  $value
203
	* @return string
204
	*/
205 16
	protected function encodeScalar($value)
206
	{
207 16
		return json_encode($value);
208
	}
209
210
	/**
211
	* Test whether given array is a numerically indexed array
212
	*
213
	* @param  array $array
214
	* @return bool
215
	*/
216 9
	protected function isIndexedArray(array $array)
217
	{
218 9
		if (empty($array))
219 9
		{
220 1
			return true;
221
		}
222
223 8
		if (isset($array[0]) && array_keys($array) === range(0, count($array) - 1))
224 8
		{
225 3
			return true;
226
		}
227
228 5
		return false;
229
	}
230
231
	/**
232
	* Test whether a string can be used as a property name, unquoted
233
	*
234
	* @link http://es5.github.io/#A.1
235
	*
236
	* @param  string $name Property's name
237
	* @return bool
238
	*/
239 5
	protected function isLegalProp($name)
240
	{
241
		/**
242
		* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words
243
		* @link http://www.crockford.com/javascript/survey.html
244
		*/
245 5
		$reserved = ['abstract', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with'];
246
247 5
		if (in_array($name, $reserved, true))
248 5
		{
249 1
			return false;
250
		}
251
252 5
		return (bool) preg_match('#^(?![0-9])[$_\\pL][$_\\pL\\pNl]+$#Du', $name);
253
	}
254
}