Completed
Push — master ( e714e6...a2d6e6 )
by Gabriel
03:55
created

View::buildPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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