Passed
Push — main ( f61cf1...0f2cdb )
by Guillaume
02:44
created

VueJSComponent::generateObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
use PHPMV\utils\JsUtils;
7
8
class VueJSComponent extends AbstractVueJS {
9
	protected string $name;
10
	protected array $configuration;
11
	protected array $props;
12
	protected array $template;
13
	protected array $extends;
14
	protected string $varName;
15
16 1
	public function __construct(string $name, string $varName = null) {
17 1
		parent::__construct();
18 1
		$this->name = $name;
19 1
		$this->configuration = [];
20 1
		$this->props = [];
21 1
		$this->extends = [];
22 1
		$this->template = [];
23 1
		if (!$varName) {
24 1
			$varName = JsUtils::kebabToPascal($name);
25
		}
26 1
		$this->varName = $varName;
27 1
	}
28
29 1
	public function extends(VueJSComponent $component): void {
30 1
		$this->extends['extends'] = $component->getVarName();
31 1
	}
32
33 1
	public function generateObject(): string {
34 1
		$script = JavascriptUtils::arrayToJsObject($this->components + $this->filters + $this->extends + $this->mixins + $this->configuration + $this->props + $this->data + $this->computeds + $this->watchers + $this->hooks + $this->methods + $this->template);
35 1
		$script = JsUtils::cleanJSONFunctions($script);
36 1
		return $script;
37
	}
38
39 1
	public function generateGlobalScript(): string {
40 1
		$script = "Vue.component('" . $this->name . "',";
41 1
		$script .= $this->generateObject();
42 1
		$script .= ");";
43 1
		return $script;
44
	}
45
46 1
	public function generateFile(bool $inVariable = false, bool $global = false): void {
47 1
		$script = ($inVariable) ? JsUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript();
48 1
		if (!$global) {
49 1
			\file_put_contents($this->name . ".js", $script);
50
		} elseif (file_exists("components.js")) {
51
			\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND);
52
		} else {
53
			\file_put_contents("components.js", $script);
54
		}
55 1
	}
56
57 1
	public function addProps(string ...$props): void {
58 1
		$this->props["props"] = $props;
59 1
	}
60
61 1
	public function setInheritAttrs(bool $inheritAttrs): void {
62 1
		$this->configuration['inheritAttrs'] = $inheritAttrs;
63 1
	}
64
65 1
	public function setModel(string $prop, string $event): void {
66 1
		$this->configuration['model'] = "{ prop: '" . $prop . "', event: '" . $event . "' }";
67 1
	}
68
69 1
	public function addTemplate(string $template): void {
70 1
		$this->template["template"] = "'" . $template . "'";
71 1
	}
72
73 1
	public function onActivated(string $body): void {
74 1
		$this->addHook("activated", $body);
75 1
	}
76
77 1
	public function onDeactivated(string $body): void {
78 1
		$this->addHook("deactivated", $body);
79 1
	}
80
81 1
	public function getName(): string {
82 1
		return $this->name;
83
	}
84
85 1
	public function getVarName(): string {
86 1
		return $this->varName;
87
	}
88
}