View   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 81
ccs 24
cts 27
cp 0.8889
rs 10
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 2
A setViewDir() 0 8 2
A __get() 0 4 2
A template() 0 8 1
1
<?php
2
namespace JayaCode\Framework\Core\View;
3
4
use JayaCode\Framework\Core\View\Converter\BasicConvert;
5
use JayaCode\Framework\Core\View\Converter\Converter;
6
use JayaCode\Framework\Core\View\Template\BasicTemplate;
7
use JayaCode\Framework\Core\View\Template\Template;
8
use JayaCode\Framework\Core\View\VariableCollector\BasicVariableCollector;
9
use JayaCode\Framework\Core\View\VariableCollector\VariableCollector;
10
11
/**
12
 * Class View
13
 * @property VariableCollector vars
14
 * @package JayaCode\Framework\Core\View
15
 */
16
class View
17
{
18
    /**
19
     * @var VariableCollector
20
     */
21
    protected $varCollector;
22
23
    /**
24
     * @var array
25
     */
26
    protected $options = array();
27
28
    /**
29
     * @var Converter
30
     */
31
    protected $converter;
32
33
    /**
34
     * View constructor.
35
     * @param string $viewDir
36
     * @param $vars
37
     * @param array $options
38
     * @throws \Exception
39
     */
40 6
    public function __construct($viewDir = "/", $vars = null, $options = array())
41
    {
42
43 6
        $this->options = $options + [
44 6
            "extension" => ".vj",
45 6
            "variableCollectionClass" => BasicVariableCollector::class,
46 6
            "templateClass" => BasicTemplate::class,
47 6
            "converter" => BasicConvert::class,
48
            "cacheDir" => null
49 6
        ];
50
51 6
        $this->varCollector = new $this->options["variableCollectionClass"]();
52 6
        $this->converter = new $this->options["converter"]();
53
54 6
        Template::$extension = $this->options["extension"];
55
56 6
        $this->setViewDir($viewDir);
57 6
        if ($vars) {
58
            $this->varCollector->add($vars);
59
        }
60 6
    }
61
62
    /**
63
     * @param mixed $viewDir
64
     * @throws \Exception
65
     */
66 6
    public function setViewDir($viewDir)
67
    {
68 6
        if (!is_dir($viewDir)) {
69
            throw new \Exception("not found directory {$viewDir}");
70
        }
71
72 6
        $this->options['viewDir'] = rtrim($viewDir, "/") . "/";
73 6
    }
74
75
    /**
76
     * @param $name
77
     * @return VariableCollector|null
78
     */
79 6
    public function __get($name)
80
    {
81 6
        return ($name == "vars")?$this->varCollector:null;
82
    }
83
84
    /**
85
     * @param $fileTemplate
86
     * @return Template
87
     */
88 3
    public function template($fileTemplate)
89
    {
90 3
        $fileTemplateReal = str_replace(".", "/", $fileTemplate);
91 3
        $fileTemplateReal .= $this->options["extension"];
92
93 3
        return new $this->options["templateClass"]($this->options["viewDir"], $fileTemplateReal,
94 3
                                                    $this->varCollector, $this->converter, $this->options['cacheDir']);
95
    }
96
}
97