Passed
Push — main ( 8a2c2c...f2e444 )
by Guillaume
02:30
created

AbstractVueJS::generateFunction()   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 2
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
/**
5
 * Created by PhpStorm.
6
 * User: qgorak
7
 * Date: 19/11/2020
8
 * Time: 14:20
9
 */
10
class AbstractVueJS {
11
    static private $removeQuote = ["start"=>"!!%","end"=>"%!!"];
12
    static protected $global;
13
	protected $data;
14
	protected $methods;
15
	protected $computeds;
16
	protected $watchers;
17
	protected $directives;
18
	protected $filters;
19
	protected $hooks;
20
	
21 11
	public function __construct() {
22 11
	    $this->data=[];
23 11
	    $this->methods=[];
24 11
	    $this->computeds=[];
25 11
	    $this->watchers=[];
26 11
	    $this->directives=[];
27 11
	    $this->filters=[];
28 11
	    $this->hooks=[];
29 11
	}
30
	
31 2
	public function addHook(string $name,string $body):void {
32 2
	    $this->hooks[$name] = self::generateFunction($body);
33 2
	}
34
	
35 1
	public function onBeforeCreate(string $body):void {
36 1
		$this->addHook("beforeCreate", $body);
37 1
	}
38
	
39 1
	public function onCreated(string $body):void {
40 1
		$this->addHook("created", $body);
41 1
	}
42
	
43 1
	public function onBeforeMount(string $body):void {
44 1
		$this->addHook("beforeMount", $body);
45 1
	}
46
	
47 2
	public function onMounted(string $body):void {
48 2
		$this->addHook("mounted", $body);
49 2
	}
50
	
51 1
	public function onBeforeUpdate(string $body):void {
52 1
		$this->addHook("beforeUpdate", $body);
53 1
	}
54
	
55 1
	public function onUpdated(string $body):void {
56 1
		$this->addHook("updated", $body);
57 1
	}
58
	
59 1
	public function onUpdatedNextTick(string $body):void {
60 1
		$this->addHook("updated", "this.\$nextTick(function () {".$body."})");
61 1
	}
62
	
63 1
	public function onBeforeDestroy(string $body):void {
64 1
		$this->addHook("beforeDestroy", $body);
65 1
	}
66
	
67 1
	public function onDestroyed(string $body):void {
68 1
		$this->addHook("destroyed", $body);
69 1
	}
70
71 3
	public function addData(string $name,$value):void {
72 3
        $this->data["data"][$name]=$value;
73 3
	}
74
75 2
	public function addDataRaw(string $name,string $value):void {
76 2
        $this->data["data"][$name]=self::$removeQuote["start"].$value.self::$removeQuote["end"];
77 2
	}
78
	
79 4
	public function addMethod(string $name,string $body, array $params = []):void {
80 4
        $this->methods["methods"][$name]=self::generateFunction($body,$params);
81 4
	}
82
	
83 3
	public function addComputed(string $name,string $get,string $set=null):void {
84 3
	    $vc=(is_null($set)) ? self::generateFunction($get) : self::$removeQuote["start"]."{ get: ".self::generateFunction($get).", set: ".self::generateFunction($set,["v"])." }".self::$removeQuote["end"];
85 3
	    $this->computeds["computeds"][self::$removeQuote["start"].$name.self::$removeQuote["end"]]=$vc;
86 3
	}
87
	
88 2
	public function addWatcher(string $var,string $body,array $params=[]):void {
89 2
	    $this->watchers["watch"][$var]=self::generateFunction($body,$params);
90 2
	}
91
92
	public function addFilter(string $name,string $body, array $params = []):void {
93
        $this->methods["filters"][self::$removeQuote["start"].$name.self::$removeQuote["end"]]=self::generateFunction($body,$params);
94
    }
95
96
    static function addGlobalFilter(string $name,string $body, array $params = []):void {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
        self::$global[]=self::$removeQuote["start"]."Vue.filter('".$name."',".self::generateFunction($body,$params).");".self::$removeQuote["end"];
98
    }
99
100 4
	public function getData():array {
101 4
	    return $this->data;
102
	}
103
	
104 1
	public function setData(array $data):void {
105 1
	    $this->data=$data;
106 1
	}
107
	
108 3
	public function getMethods():array {
109 3
	    return $this->methods;
110
	}
111
	
112 1
	public function setMethods(array $methods):void {
113 1
	    $this->methods=$methods;
114 1
	}
115
	
116 3
	public function getComputeds():array {
117 3
		return $this->computeds;
118
	}
119
120 1
	public function setComputeds(array $computeds):void {
121 1
		$this->computeds = $computeds;
122 1
	}
123
124 2
	public function getWatchers():array {
125 2
	    return $this->watchers;
126
	}
127
	
128 1
	public function setWatchers(array $watchers):void {
129 1
		$this->watchers = $watchers;
130 1
	}
131
	
132 2
	public function getHooks():array {
133 2
		return $this->hooks;
134
	}
135
	
136 1
	public function setHooks(array $hooks):void {
137 1
		$this->hooks = $hooks;
138 1
	}
139
140 6
	static function generateFunction(string $body, array $params = []):string {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
141 6
        return self::$removeQuote["start"]."function(".implode(",",$params)."){".$body."}".self::$removeQuote["end"];
142
    }
143
}