Test Failed
Push — main ( d7f1b4...976a1c )
by Guillaume
02:42
created

AbstractVueJS::onBeforeDestroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
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 14
	
22 14
	protected function __construct() {
23 14
	    $this->data = [];
24 14
	    $this->methods = [];
25 14
	    $this->computeds = [];
26 14
	    $this->watchers = [];
27 14
	    $this->components = [];
28 14
	    $this->directives = [];
29 14
	    $this->filters = [];
30
	    $this->hooks = [];
31 3
	}
32 3
	
33 3
	protected function addHook(string $name, string $body):void {
34
	    $this->hooks[$name] = JsUtils::generateFunction($body,[],false);
35 1
	}
36 1
	
37 1
	public function onBeforeCreate(string $body):void {
38
		$this->addHook("beforeCreate", $body);
39 1
	}
40 1
	
41 1
	public function onCreated(string $body):void {
42
		$this->addHook("created", $body);
43 1
	}
44 1
	
45 1
	public function onBeforeMount(string $body):void {
46
		$this->addHook("beforeMount", $body);
47 2
	}
48 2
	
49 2
	public function onMounted(string $body):void {
50
		$this->addHook("mounted", $body);
51 1
	}
52 1
	
53 1
	public function onBeforeUpdate(string $body):void {
54
		$this->addHook("beforeUpdate", $body);
55 1
	}
56 1
	
57 1
	public function onUpdated(string $body):void {
58
		$this->addHook("updated", $body);
59 1
	}
60 1
	
61 1
	public function onUpdatedNextTick(string $body):void {
62
		$this->addHook("updated", "this.\$nextTick(function () {".$body."})");
63 1
	}
64 1
	
65 1
	public function onBeforeDestroy(string $body):void {
66
		$this->addHook("beforeDestroy", $body);
67 1
	}
68 1
	
69 1
	public function onDestroyed(string $body):void {
70
		$this->addHook("destroyed", $body);
71 3
	}
72 3
73 3
	public function addData(string $name, $value):void {
74 3
	    $name = JsUtils::removeQuotes($name);
75
        $this->data["data"][$name] = $value;
76 2
	}
77 2
78 2
	public function addDataRaw(string $name, string $value):void {
79 2
        $name = JsUtils::removeQuotes($name);
80
        $this->data["data"][$name] = JsUtils::removeQuotes($value);
81 5
	}
82 5
	
83 5
	public function addMethod(string $name, string $body, array $params = []):void {
84 5
        $name = JsUtils::removeQuotes($name);
85
        $this->methods["methods"][$name] = JsUtils::generateFunction($body,$params);
86 3
	}
87 3
	
88 3
	public function addComputed(string $name, string $get, string $set=null):void {
89 3
        $name = JsUtils::removeQuotes($name);
90 3
	    $vc = (is_null($set)) ? JsUtils::generateFunction($get) : JsUtils::removeQuotes("{ get: ".JsUtils::generateFunction($get,[],false).", set: ".JsUtils::generateFunction($set,["v"],false)." }");
91
	    $this->computeds["computeds"][$name] = $vc;
92 2
	}
93 2
94 2
    public function addLocalComponent(VueJSComponent $component, $varName = false, bool $import = true):void {
95
        $name = $component->getName();
96 1
        if(!$varName){
97 1
            $varName = JsUtils::kebabToPascal($name);
98 1
        }
99 1
        $this->components['components'][$name] = JsUtils::removeQuotes($varName);
100
        $vueManager = VueManager::getInstance();
101 1
        if($import){
102 1
            $vueManager->addImport(JsUtils::declareVariable('const',$varName,$component->generateLocalScript()));
103 1
        }
104 1
    }
105 1
106
	public function addWatcher(string $var, string $body, array $params=[]):void {
107 1
        $var = JsUtils::removeQuotes($var);
108
	    $this->watchers["watch"][$var] = JsUtils::generateFunction($body,$params);
109 4
	}
110 4
111
	public function addFilter(string $name, string $body, array $params = []):void {
112
	    $name = JsUtils::removeQuotes($name);
113 1
        $this->filters["filters"][$name] = JsUtils::generateFunction($body,$params);
114 1
    }
115 1
116
    public function addDirective(string $name, array $hookFunction):void {
117 3
	    $name = JsUtils::removeQuotes($name);
118 3
	    foreach ($hookFunction as $key => $value){
119
            $key = JsUtils::removeQuotes($key);
120
            $this->directives["directives"][$name][$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode']);
121 1
        }
122 1
    }
123 1
124
    public function getData():array {
125 3
	    return $this->data;
126 3
    }
127
128
    public function getMethods():array {
129 1
	    return $this->methods;
130 1
    }
131 1
132
    public function getComputeds():array {
133 2
	    return $this->computeds;
134 2
    }
135
136
    public function getDirectives():array {
137 1
	    return $this->directives;
138 1
    }
139 1
140
    public function getFilters():array {
141 2
	    return $this->filters;
142 2
    }
143
144
    public function getWatchers():array {
145 1
	    return $this->watchers;
146 1
    }
147 1
148
    public function getHooks():array {
149 2
	    return $this->hooks;
150
    }
151
}