Test Failed
Push — main ( 5327e9...2642ec )
by Guillaume
03:06
created

AbstractVueJS::addMixin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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