Passed
Push — main ( 976a1c...cb0b4a )
by Guillaume
03:00
created

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