Json   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 15 4
A isJsonFile() 0 3 2
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\TSSFunction;
8
class Json implements \Transphporm\TSSFunction {
9
	private $filePath;
10
11
	public function __construct(\Transphporm\FilePath $filePath) {
12
		$this->filePath = $filePath;
13
	}
14
15
	public function run(array $args, \DomElement $element = null) {
16
		$json = $args[0];
17
18
		if ($this->isJsonFile($json)) {
19
			$path = $this->filePath->getFilePath($json);
20
			if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path);
21
			$json = file_get_contents($path);
22
		}
23
24
		$map = json_decode($json, true);
25
26
		if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg());
27
28
		return $map;
29
	}
30
31
	private function isJsonFile($json) {
32
		return trim($json)[0] != '{' && trim($json)[0] != '[';
33
	}
34
}
35