Passed
Push — main ( a17faf...5c49fb )
by Jean-Christophe
02:14
created

JSX::getChildrenStr()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.8333
cc 7
nc 10
nop 2
crap 7
1
<?php
2
namespace PHPMV\utils;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\react\ReactJS;
6
7
/**
8
 * PHPMV\utils$JSX
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
class JSX {
16
17
	public static $reactCreateElement = 'React.createElement';
18
19
	private static $jsDetect = [
20
		'onBlur' => 0,
21
		'onChange' => 0,
22
		'onDblclick' => 0,
23
		'onClick' => 0,
24
		'value' => 0
25
	];
26
27 2
	private static function getName(string $name, ReactJS $react): string {
28 2
		return $react->components[$name] ?? '"' . $name . '"';
29
	}
30
31
	private static $attributes = [
32
		'classname' => 'className',
33
		'onblur' => 'onBlur',
34
		'onclick' => 'onClick',
35
		'onchange' => 'onChange'
36
	];
37
38 2
	private static function cleanJSONFunctions(string $json): string {
39 2
		return \str_replace([
40 2
			'"!!%',
41
			'%!!"'
42 2
		], '', $json);
43
	}
44
45 1
	private static function hasBraces(string $str): bool {
46 1
		return (\substr($str, 0, 1) === '{' && \substr($str, - 1) === '}');
47
	}
48
49 2
	private static function nodeToJs(\DOMNode $root, ?ReactJS $react): string {
50 2
		$attributes = [];
51 2
		$name = $root->nodeName;
52
53 2
		if ($root->hasAttributes()) {
54 1
			$attrs = $root->attributes;
55
56 1
			foreach ($attrs as $i => $attr) {
57 1
				$attrName = self::$attributes[$attr->name] ?? $attr->name;
58 1
				$attrValue = $attr->value;
59 1
				if (isset(self::$jsDetect[$attrName])) {
60 1
					if (self::hasBraces($attrValue)) {
61 1
						$attrValue = \substr($attrValue, 1, - 1);
62
					}
63 1
					$attributes[$attrName] = '!!%' . $attrValue . '%!!';
64
				} else {
65 1
					$attributes[$attrName] = $attrValue;
66
				}
67
			}
68
		}
69 2
		$childrenStr = self::getChildrenStr($root, $react);
70 2
		return self::$reactCreateElement . "(" . ((isset($react)) ? self::getName($name, $react) : $name) . "," . self::cleanJSONFunctions(JavascriptUtils::toJSON($attributes)) . "$childrenStr)";
71
	}
72
73 2
	private static function getChildrenStr(\DOMNode $root, ?ReactJS $react): string {
74 2
		$children = [];
75
76 2
		$childNodes = $root->childNodes;
77
78 2
		for ($i = 0; $i < $childNodes->length; $i ++) {
79 1
			$child = $childNodes->item($i);
80 1
			if ($child->nodeType == XML_TEXT_NODE) {
81 1
				$v = \trim($child->nodeValue);
82 1
				if ($v != null) {
83 1
					\preg_match_all('@\{(.*?)\}@', $v, $matches);
84 1
					if (\count($matches[1]) > 0) {
85 1
						foreach ($matches[1] as $ev) {
86 1
							$children[] = $ev;
87
						}
88
					} else {
89 1
						$children[] = "`$v`";
90
					}
91
				}
92
			} else {
93 1
				$children[] = self::nodeToJs($child, $react);
94
			}
95
		}
96 2
		return (count($children) > 0) ? (',' . implode(',', $children)) : '';
97
	}
98
99 2
	public static function toJs(string $html, ?ReactJS $react = null): string {
100 2
		\libxml_use_internal_errors(true);
101 2
		$dom = new \DOMDocument('1.0', 'UTF-8');
102 2
		$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
103 2
		return self::nodeToJs($dom->documentElement, $react);
104
	}
105
}
106