Passed
Push — master ( afdc50...dc7db1 )
by Gabriel
07:13
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Nip\View;
4
5
use Nip\View\Extensions\Helpers\HasHelpersTrait;
6
use Nip\View\Traits\HasDataTrait;
7
use Nip\View\Traits\HasExtensionsTrait;
8
use Nip\View\Traits\HasMethodsTrait;
9
use Nip\View\Traits\HasPathsTrait;
10
use Nip\View\Traits\MethodsOverloadingTrait;
11
use Nip\View\ViewFinder\HasViewFinder;
12
13
/**
14
 * Class View
15
 *
16
 */
17
class View implements ViewInterface
18
{
19
    use HasDataTrait;
20
    use HasExtensionsTrait;
21
    use HasHelpersTrait;
22
    use HasMethodsTrait;
23
    use HasPathsTrait;
24
    use HasViewFinder;
25
    use MethodsOverloadingTrait;
26
27
    protected $helpers = [];
28
    protected $blocks = [];
29
30 7
    public function __construct()
31
    {
32 7
        $this->addMethodsPipelineStage();
33 7
        $this->addHelpersExtension();
34 7
        $this->initFinder();
35 7
    }
36
37
    /**
38
     * @param $name
39
     * @param $block
40
     */
41
    public function setBlock($name, $block)
42
    {
43
        $this->blocks[$name] = $block;
44
    }
45
46
    /**
47
     * @param string $block
48
     */
49
    public function render($block = 'default')
50
    {
51
        if (!empty($this->blocks[$block])) {
52
            $this->load("/" . $this->blocks[$block]);
53
        } else {
54
            trigger_error("No $block block", E_USER_ERROR);
55
        }
56
    }
57
58
    /** @noinspection PhpInconsistentReturnPointsInspection
59
     *
60
     * @param $view
61
     * @param array $variables
62
     * @param bool $return
63
     * @return string|boolean
64
     */
65 4
    public function load($view, $variables = [], $return = false)
66
    {
67 4
        $html = $this->getContents($view, $variables);
68
69 4
        if ($return === true) {
70
            return $html;
71
        }
72
73 4
        echo $html;
74
75 4
        return null;
76
    }
77
78
    /**
79
     * @param $view
80
     * @param array $variables
81
     * @return string
82
     */
83 4
    public function getContents($view, $variables = [])
84
    {
85 4
        extract($variables);
86
87 4
        $path = $this->buildPath($view);
88
89 4
        unset($view, $variables);
90 4
        ob_start();
91
        /** @noinspection PhpIncludeInspection */
92 4
        include($path);
93 4
        $html = ob_get_contents();
94 4
        ob_end_clean();
95
96 4
        return $html;
97
    }
98
99
    /**
100
     * Builds path for including
101
     * If $view starts with / the path will be relative to the root of the views folder.
102
     * Otherwise to caller file location.
103
     *
104
     * @param string $view
105
     * @return string
106
     */
107 4
    public function buildPath($view)
108
    {
109 4
        return $this->getFinder()->find($view);
110
    }
111
112
    /**
113
     * @param string $block
114
     * @return bool
115
     */
116
    public function isBlock($block = 'default')
117
    {
118
        return empty($this->blocks[$block]) ? false : true;
119
    }
120
121
    /**
122
     * Assigns variables in bulk in the current scope
123
     *
124
     * @param array $array
125
     * @return $this
126
     */
127
    public function assign($array = [])
128
    {
129
        foreach ($array as $key => $value) {
130
            if (is_string($key)) {
131
                $this->set($key, $value);
132
            }
133
        }
134
135
        return $this;
136
    }
137
}
138