ReactClass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace PHPMV\core;
3
4
/**
5
 * A React class generator.
6
 * PHPMV\core$ReactClass
7
 * This class is part of php-react
8
 *
9
 * @author jc
10
 * @version 1.0.0
11
 *
12
 */
13
class ReactClass {
14
15
	/**
16
	 *
17
	 * @var TemplateParser
18
	 */
19
	private static $template;
20
21
	/**
22
	 *
23
	 * @var TemplateParser
24
	 */
25
	private static $methodTemplate;
26
27
	/**
28
	 * The class name.
29
	 *
30
	 * @var string
31
	 */
32
	private $name;
33
34
	/**
35
	 * The base class.
36
	 *
37
	 * @var string
38
	 */
39
	private $base;
40
41
	/**
42
	 *
43
	 * @var array
44
	 */
45
	private $methods;
46
47 4
	public function __construct(string $name, string $baseClass) {
48 4
		$this->name = $name;
49 4
		$this->base = $baseClass;
50 4
	}
51
52
	/**
53
	 * Initialize templates.
54
	 */
55 5
	public static function init(): void {
56 5
		$templateFolder = ReactLibrary::getTemplateFolder();
57 5
		self::$template = new TemplateParser();
58 5
		self::$template->loadTemplatefile($templateFolder . '/class');
59 5
		self::$methodTemplate = new TemplateParser();
60 5
		self::$methodTemplate->loadTemplatefile($templateFolder . '/method');
61 5
	}
62
63
	/**
64
	 * Add a new method to the class.
65
	 *
66
	 * @param string $name
67
	 *        	The method name
68
	 * @param string $body
69
	 *        	The method javascript code body
70
	 * @param mixed ...$params
71
	 *        	The method parameters
72
	 */
73 4
	public function addMethod(string $name, string $body, ...$params): void {
74 4
		$this->methods[$name] = [
75 4
			'body' => $body,
76 4
			'params' => $params
77
		];
78 4
	}
79
80
	/**
81
	 * Generate the component javascript code.
82
	 *
83
	 * @return string
84
	 */
85 4
	public function parse(): string {
86 4
		$body = [];
87 4
		foreach ($this->methods as $name => $arrayMethod) {
88 4
			$body[] = self::$methodTemplate->parse([
89 4
				'name' => $name,
90 4
				'body' => $arrayMethod['body'],
91 4
				'params' => implode(',', $arrayMethod['params'])
92
			]);
93
		}
94 4
		$bodyStr = implode("\n", $body);
95 4
		return self::$template->parse([
96 4
			'name' => $this->name,
97 4
			'base' => $this->base,
98 4
			'body' => $bodyStr
99
		]);
100
	}
101
}
102
103