Passed
Push — main ( 6d290a...4a7e3a )
by Jean-Christophe
04:01
created

VueJSComponent::generateFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.2596

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 8
ccs 4
cts 7
cp 0.5714
crap 5.2596
rs 10
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
	protected string $type;
14
	protected bool $global;
15
16 1
	public function __construct(string $name, string $varName = null, string $type='component') {
17 1
		parent::__construct([]);
18 1
		$this->name = $name;
19 1
		$this->props = [];
20 1
		$this->extends = [];
21 1
		$this->template = [];
22 1
		$this->varName = $varName??JavascriptUtils::kebabToPascal($name);
23 1
		$this->type=$type;
24 1
		$this->global=false;
25 1
	}
26
27 1
	public function addData(string $name, $value): void {
28 1
		$this->data['data'][$name] = $value;
29 1
	}
30
31 1
	public function addDataRaw(string $name, string $value): void {
32 1
		$this->data['data'][$name] = JavascriptUtils::removeQuotes($value);
33 1
	}
34
35 1
	public function extends(VueJSComponent $component): void {
36 1
		$this->extends['extends'] = $component->getVarName();
37 1
	}
38
39 1
	public function generateObject(): string {
40 1
		$data = [];
41 1
		if (isset($this->data['data'])) {
42 1
			$data['data'] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"]));
43
		}
44 1
		return 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);
45
	}
46
47 1
	public function generateGlobalScript(): string {
48 1
		return "Vue.{$this->type}(\"{$this->name}\",{$this->generateObject()});";
49
50
	}
51
52 1
	public function generateFile(bool $inVariable = false, bool $global = false): void {
53 1
		$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript();
54 1
		if (!$global) {
55 1
			\file_put_contents("$this->name.js", $script);
56
		} elseif (\file_exists("components.js")) {
57
			\file_put_contents("components.js", \PHP_EOL . $script, \FILE_APPEND);
58
		} else {
59
			\file_put_contents("components.js", $script);
60
		}
61 1
	}
62
63 1
	public function addProps(string ...$props): void {
64 1
		$this->props['props'] = $props;
65 1
	}
66
67 1
	public function setInheritAttrs(bool $inheritAttrs): void {
68 1
		$this->configuration['inheritAttrs'] = $inheritAttrs;
69 1
	}
70
71 1
	public function setModel(string $prop, string $event): void {
72 1
		$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '$prop', event: '$event ' }");
73 1
	}
74
75 1
	public function addTemplate(string $template): void {
76 1
		$this->template['template'] = $template;
77 1
	}
78
79 1
	public function onActivated(string $body): void {
80 1
		$this->addHook('activated', $body);
81 1
	}
82
83 1
	public function onDeactivated(string $body): void {
84 1
		$this->addHook('deactivated', $body);
85 1
	}
86
87 1
	public function getName(): string {
88 1
		return $this->name;
89
	}
90
91 1
	public function getVarName(): string {
92 1
		return $this->varName;
93
	}
94
95
	/**
96
	 * @param string $type
97
	 * @param bool $global
98
	 */
99 1
	public function setTypeAndGlobal(string $type='component',bool $global=true): void {
100 1
		$this->type = $type;
101 1
		$this->global=$global;
102 1
	}
103
104
	/**
105
	 * @param bool $global
106
	 */
107 1
	public function setGlobal(bool $global): void {
108 1
		$this->global = $global;
109 1
	}
110
111 1
	public function __toString(): string {
112 1
		if($this->global){
113 1
			return $this->generateGlobalScript();
114
		}
115 1
		return JavascriptUtils::declareVariable('const', $this->varName, $this->generateObject(), false);
116
	}
117
118
}