Passed
Push — main ( 482e0e...ad07de )
by Guillaume
04:07
created

VueJSComponent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 2
b 0
f 0
dl 0
loc 45
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setProps() 0 2 1
A onDeactivated() 0 2 1
A onActivated() 0 2 1
A createGlobal() 0 2 1
A create() 0 18 3
1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\utils\JsUtils;
6
7
class VueJSComponent extends AbstractVueJS{
8
    protected string $name;
9
    protected array $props=["props"=>[]];
10
    protected array $template;
11
    
12 2
    public function __construct(string $template) {
13 2
        parent::__construct();
14 2
        $this->template["template"]="'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'");
15 2
        $this->name=$template;
16 2
    }
17
    
18 2
    public function setProps(string ...$name):void {
19 2
        $this->props["props"]=$name;
20 2
    }
21
22 1
    public function onActivated(string $body):void {
23 1
        $this->addHook("activated", $body);
24 1
    }
25
26 1
    public function onDeactivated(string $body):void {
27 1
        $this->addHook("deactivated", $body);
28 1
    }
29
30 2
    public function create(bool $global=false):string {
31 2
        $script="Vue.component('".$this->name."',";
32 2
        $script.=JavascriptUtils::arrayToJsObject($this->props + $this->data + $this->methods + $this->computeds + $this->watchers + $this->filters + $this->hooks + $this->template);
33 2
        $script=JsUtils::cleanJSONFunctions($script);
34 2
        $script.=")";
35 2
        if(!$global){
36 2
            \file_put_contents($this->name.".js",$script);
37
        }
38
        else{
39 2
            if(file_exists("components.js")){
40 1
                \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
41
            }
42
            else{
43 1
                \file_put_contents("components.js",$script);
44
            }
45
            
46
        }
47 2
        return $script;
48
    }
49
    
50 2
    public function createGlobal():string{
51 2
        return $this->create(true);   
52
    }
53
}