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

AbstractVueJS   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 52
dl 0
loc 112
ccs 81
cts 81
cp 1
rs 10
c 3
b 1
f 0
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A addFilter() 0 3 1
A addMixin() 0 3 1
A onUpdatedNextTick() 0 2 1
A addData() 0 3 1
A onCreated() 0 2 1
A onBeforeMount() 0 2 1
A addComponent() 0 2 1
A addWatcher() 0 3 1
A onBeforeDestroy() 0 2 1
A addDirective() 0 9 3
A addMethod() 0 3 1
A onBeforeCreate() 0 2 1
A addDataRaw() 0 3 1
A __construct() 0 10 1
A addComputed() 0 4 2
A addHook() 0 2 1
A onMounted() 0 2 1
A onDestroyed() 0 2 1
A onBeforeUpdate() 0 2 1
A onUpdated() 0 2 1
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\utils\JsUtils;
6
7
/**
8
 * Created by PhpStorm.
9
 * User: qgorak
10
 * Date: 19/11/2020
11
 * Time: 14:20
12
 */
13
abstract class AbstractVueJS {
14
	protected array $data;
15
	protected array $methods;
16
	protected array $computeds;
17
	protected array $watchers;
18
	protected array $components;
19
	protected array $directives;
20
	protected array $filters;
21
	protected array $hooks;
22
	protected array $mixins;
23
24 1
	protected function __construct() {
25 1
		$this->data = [];
26 1
		$this->methods = [];
27 1
		$this->computeds = [];
28 1
		$this->watchers = [];
29 1
		$this->components = [];
30 1
		$this->directives = [];
31 1
		$this->filters = [];
32 1
		$this->hooks = [];
33 1
		$this->mixins = [];
34 1
	}
35
36 1
	protected function addHook(string $name, string $body): void {
37 1
		$this->hooks[$name] = JsUtils::generateFunction($body, [], false);
38 1
	}
39
40 1
	public function onBeforeCreate(string $body): void {
41 1
		$this->addHook("beforeCreate", $body);
42 1
	}
43
44 1
	public function onCreated(string $body): void {
45 1
		$this->addHook("created", $body);
46 1
	}
47
48 1
	public function onBeforeMount(string $body): void {
49 1
		$this->addHook("beforeMount", $body);
50 1
	}
51
52 1
	public function onMounted(string $body): void {
53 1
		$this->addHook("mounted", $body);
54 1
	}
55
56 1
	public function onBeforeUpdate(string $body): void {
57 1
		$this->addHook("beforeUpdate", $body);
58 1
	}
59
60 1
	public function onUpdated(string $body): void {
61 1
		$this->addHook("updated", $body);
62 1
	}
63
64 1
	public function onUpdatedNextTick(string $body): void {
65 1
		$this->addHook("updated", "this.\$nextTick(function () {" . $body . "})");
66 1
	}
67
68 1
	public function onBeforeDestroy(string $body): void {
69 1
		$this->addHook("beforeDestroy", $body);
70 1
	}
71
72 1
	public function onDestroyed(string $body): void {
73 1
		$this->addHook("destroyed", $body);
74 1
	}
75
76 1
	public function addData(string $name, $value): void {
77 1
		$name = JsUtils::removeQuotes($name);
78 1
		$this->data["data"][$name] = $value;
79 1
	}
80
81 1
	public function addDataRaw(string $name, string $value): void {
82 1
		$name = JsUtils::removeQuotes($name);
83 1
		$this->data["data"][$name] = JsUtils::removeQuotes($value);
84 1
	}
85
86 1
	public function addMethod(string $name, string $body, array $params = []): void {
87 1
		$name = JsUtils::removeQuotes($name);
88 1
		$this->methods["methods"][$name] = JsUtils::generateFunction($body, $params);
89 1
	}
90
91 1
	public function addComputed(string $name, string $get, string $set = null): void {
92 1
		$name = JsUtils::removeQuotes($name);
93 1
		$vc = (is_null($set)) ? JsUtils::generateFunction($get) : JsUtils::removeQuotes("{ get: " . JsUtils::generateFunction($get, [], false) . ", set: " . JsUtils::generateFunction($set, ["v"], false) . " }");
94 1
		$this->computeds["computeds"][$name] = $vc;
95 1
	}
96
97 1
	public function addComponent(VueJSComponent $component): void {
98 1
		$this->components['components'][$component->getName()] = JsUtils::removeQuotes($component->getVarName());
99 1
	}
100
101 1
	public function addWatcher(string $var, string $body, array $params = []): void {
102 1
		$var = JsUtils::removeQuotes($var);
103 1
		$this->watchers["watch"][$var] = JsUtils::generateFunction($body, $params);
104 1
	}
105
106 1
	public function addMixin(VueJSComponent $mixin): void {
107 1
		$varName = JsUtils::removeQuotes($mixin->getVarName());
108 1
		$this->mixins["mixins"][] = $varName;
109 1
	}
110
111 1
	public function addFilter(string $name, string $body, array $params = []): void {
112 1
		$name = JsUtils::removeQuotes($name);
113 1
		$this->filters["filters"][$name] = JsUtils::generateFunction($body, $params);
114 1
	}
115
116 1
	public function addDirective(string $name, array $hookFunction, array $configuration = []): void {
117 1
		$name = JsUtils::removeQuotes($name);
118 1
		foreach ($configuration as $key => $value) {
119 1
			$key = JsUtils::removeQuotes($key);
120 1
			$this->directives["directives"][$name][$key] = $value;
121
		}
122 1
		foreach ($hookFunction as $key => $value) {
123 1
			$key = JsUtils::removeQuotes($key);
124 1
			$this->directives["directives"][$name][$key] = JsUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
125
		}
126
	}
127
}