Test Failed
Push — main ( 77a5ed...f65f10 )
by Guillaume
02:41
created

GlobalVueJS::deleteInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
namespace PHPMV;
3
4
5
use PHPMV\js\JavascriptUtils;
6
use PHPMV\utils\JsUtils;
7
8
class GlobalVueJS{
9
10
    private static ?GlobalVueJS $instance = null;
11
    protected array $global;
12
13
    protected function __construct() {
14
        $this->global=[];
15
    }
16
17
    public static function getInstance(): GlobalVueJS{
18
        if (!isset(self::$instance)) {
19
            self::$instance = new GlobalVueJS();
20
        }
21
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return PHPMV\GlobalVueJS. Consider adding an additional type-check to rule them out.
Loading history...
22
    }
23
24
    public static function deleteInstance():void {
25
        self::$instance = null;
26
    }
27
28
    public function addGlobalDirective(string $name,array $hookFunction):void {
29
        foreach ($hookFunction as $key=>$value){
30
            $hookFunction[$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode'],false);
31
        }
32
        $this->global[]="Vue.directive('".$name."',".JavascriptUtils::arrayToJsObject($hookFunction).");";
33
    }
34
35
    public function addGlobalFilter(string $name,string $body, array $params = []):void {
36
        $this->global[]="Vue.filter('".$name."',".JsUtils::generateFunction($body,$params,false).");";
37
    }
38
39
    public function addGlobalObservable(string $varName, array $object){
40
        $this->global[]="const ".$varName." = Vue.observable(". JavascriptUtils::arrayToJsObject($object) .");";
41
    }
42
43
    public function __toString():string {
44
        $script="";
45
        if(!empty($this->global)){
46
            foreach($this->global as $global){
47
                $script .= $global."\n";
48
            }
49
        }
50
        $script = JsUtils::cleanJSONFunctions($script);
51
        $script = JavascriptUtils::wrapScript($script);
52
        return $script;
53
    }
54
}