Passed
Push — main ( 2ed84c...44e977 )
by Guillaume
02:38
created

VueJSComponent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 37
ccs 21
cts 21
cp 1
rs 10
c 2
b 0
f 0
wmc 6

4 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
1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\utils\JsUtils;
6
7
class VueJSComponent extends AbstractVueJS{
8
    protected $name;
9
    protected $props=["props"=>[]];
10
    protected $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 create(bool $global=false):string {
23 1
        $script="Vue.component('".$this->name."',";
24 1
        $script.=JavascriptUtils::arrayToJsObject($this->props + $this->data + $this->methods + $this->computeds + $this->watchers + $this->hooks + $this->template);
25 1
        $script=JsUtils::cleanJSONFunctions($script);
26 1
        $script.=")";
27 1
        if(!$global){
28 1
            \file_put_contents($this->name.".js",$script);
29
        }
30
        else{
31 1
            if(file_exists("components.js")){
32 1
                \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
33
            }
34
            else{
35 1
                \file_put_contents("components.js",$script);
36
            }
37
            
38
        }
39 1
        return $script;
40
    }
41
    
42 1
    public function createGlobal():string{
43 1
        return $this->create(true);   
44
    }
45
}