Passed
Push — main ( 9a9034...2543d5 )
by Guillaume
02:37
created

AbstractVueJS::onUpdatedNextTick()   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\js\JavascriptUtils;
5
6
/**
7
 * Created by PhpStorm.
8
 * User: qgorak
9
 * Date: 19/11/2020
10
 * Time: 14:20
11
 */
12
class AbstractVueJS {
13
    static private $removeQuote = ["start"=>"!!%","end"=>"%!!"];
14
    static protected $global;
15
	protected $data;
16
	protected $methods;
17
	protected $computeds;
18
	protected $watchers;
19
	protected $directives;
20
	protected $filters;
21
	protected $hooks;
22
	
23 12
	public function __construct() {
24 12
	    $this->data=[];
25 12
	    $this->methods=[];
26 12
	    $this->computeds=[];
27 12
	    $this->watchers=[];
28 12
	    $this->directives=[];
29 12
	    $this->filters=[];
30 12
	    $this->hooks=[];
31 12
	}
32
	
33 2
	public function addHook(string $name,string $body):void {
34 2
	    $this->hooks[$name] = self::generateFunction($body);
35 2
	}
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 3
	public function addData(string $name,$value):void {
74 3
        $this->data["data"][$name]=$value;
75 3
	}
76
77 2
	public function addDataRaw(string $name,string $value):void {
78 2
        $this->data["data"][$name]=self::removeQuotes($value);
79 2
	}
80
	
81 4
	public function addMethod(string $name,string $body, array $params = []):void {
82 4
        $this->methods["methods"][$name]=self::generateFunction($body,$params);
83 4
	}
84
	
85 3
	public function addComputed(string $name,string $get,string $set=null):void {
86 3
        $name=self::removeQuotes($name);
87 3
	    $vc=(is_null($set)) ? self::generateFunction($get) : self::removeQuotes("{ get: ".self::generateFunction($get).", set: ".self::generateFunction($set,["v"])." }");
88 3
	    $this->computeds["computeds"][$name]=$vc;
89 3
	}
90
	
91 2
	public function addWatcher(string $var,string $body,array $params=[]):void {
92 2
	    $this->watchers["watch"][$var]=self::generateFunction($body,$params);
93 2
	}
94
95
	public function addFilter(string $name,string $body, array $params = []):void {
96
	    $name=self::removeQuotes($name);
97
        $this->methods["filters"][$name]=self::generateFunction($body,$params);
98
    }
99
100 1
    public function addDirective(string $name,array $hookFunction):void {
101 1
	    $name = self::removeQuotes($name);
102 1
	    foreach ($hookFunction as $key=>$value){
103 1
	        $hookFunction[$key] = self::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode']);
104
        }
105 1
	    $this->directives["directives"][$name] = self::removeQuotes(JavascriptUtils::arrayToJsObject($hookFunction));
106 1
    }
107
108
    public static function addGlobalDirective(){}
109
110
    public static function addGlobalFilter(string $name,string $body, array $params = []):void {
111
        self::$global[]=self::removeQuotes("Vue.filter('".$name."',".self::generateFunction($body,$params).");");
112
    }
113
114
    public static function addGlobalObservable(string $varName, array $object){
115
        self::$global[]=self::removeQuotes("const ".$varName." = Vue.observable(". JavascriptUtils::arrayToJsObject($object) .");");
116
    }
117
118 4
	public function getData():array {
119 4
	    return $this->data;
120
	}
121
	
122 1
	public function setData(array $data):void {
123 1
	    $this->data=$data;
124 1
	}
125
	
126 3
	public function getMethods():array {
127 3
	    return $this->methods;
128
	}
129
	
130 1
	public function setMethods(array $methods):void {
131 1
	    $this->methods=$methods;
132 1
	}
133
	
134 3
	public function getComputeds():array {
135 3
		return $this->computeds;
136
	}
137
138 1
	public function setComputeds(array $computeds):void {
139 1
		$this->computeds = $computeds;
140 1
	}
141
142 2
	public function getWatchers():array {
143 2
	    return $this->watchers;
144
	}
145
	
146 1
	public function setWatchers(array $watchers):void {
147 1
		$this->watchers = $watchers;
148 1
	}
149
150 1
    public function getDirectives():array {
151 1
        return $this->directives;
152
    }
153
154
    public function setDirectives(array $directives):void {
155
        $this->directives = $directives;
156
    }
157
	
158 2
	public function getHooks():array {
159 2
		return $this->hooks;
160
	}
161
	
162 1
	public function setHooks(array $hooks):void {
163 1
		$this->hooks = $hooks;
164 1
	}
165
166 8
	public static function removeQuotes(string $body):string{
167 8
        return self::$removeQuote["start"].$body.self::$removeQuote["end"];
168
    }
169
170 7
	public static function generateFunction(string $body, array $params = []):string {
171 7
        return self::removeQuotes("function(".implode(",",$params)."){".$body."}");
172
    }
173
}