Passed
Push — main ( 81266e...c2a6e5 )
by Guillaume
03:04
created

VueJSComponent::addDataRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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