Test Failed
Push — main ( 4ff247...482e0e )
by Guillaume
02:34
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
eloc 23
dl 0
loc 45
ccs 22
cts 22
cp 1
rs 10
c 2
b 0
f 0
wmc 8

6 Methods

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