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

GlobalVueJS::addGlobalFilter()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
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 3
    protected function __construct() {
14 3
        $this->global = [];
15 3
    }
16
17 3
    public static function getInstance():GlobalVueJS {
18 3
        if (!isset(self::$instance)) {
19 3
            self::$instance = new GlobalVueJS();
20
        }
21 3
        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 3
    public static function deleteInstance():void {
25 3
        self::$instance = null;
26 3
    }
27
28 1
    public function addGlobalDirective(string $name,array $hookFunction):void {
29 1
        foreach ($hookFunction as $key => $value){
30 1
            $hookFunction[$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode'],false);
31
        }
32 1
        $this->global[] = "Vue.directive('".$name."',".JavascriptUtils::arrayToJsObject($hookFunction).");\n";
33 1
    }
34
35 1
    public function addGlobalFilter(string $name,string $body, array $params = []):void {
36 1
        $this->global[] = "Vue.filter('".$name."',".JsUtils::generateFunction($body,$params,false).");\n";
37 1
    }
38
39 1
    public function addGlobalObservable(string $varName, array $object):void {
40 1
        $this->global[] = JsUtils::declareVariable('const',$varName,"Vue.observable(". JavascriptUtils::arrayToJsObject($object) .")");
41 1
    }
42
43
    public function addGlobalComponent(VueJSComponent $component):void {
44
        $this->global[] = $component->generateGlobalScript();
45
    }
46
47
    public function __toString():string {
48
        $script="";
49
        if(!empty($this->global)){
50
            foreach($this->global as $global){
51
                $script .= $global."\n";
52
            }
53
        }
54
        $script = JsUtils::cleanJSONFunctions($script);
55
        $script = JavascriptUtils::wrapScript($script);
56
        return $script;
57
    }
58
59 3
    public function getGlobal():array {
60 3
        return $this->global;
61
    }
62
}