Passed
Branch master (e714e6)
by Gabriel
13:58 queued 11:36
created

View::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
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->getFinder()->find($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
     * @param string $block
102
     * @return bool
103
     */
104
    public function isBlock($block = 'default')
105
    {
106
        return empty($this->blocks[$block]) ? false : true;
107
    }
108
109
    /**
110
     * Assigns variables in bulk in the current scope
111
     *
112
     * @param array $array
113
     * @return $this
114
     */
115
    public function assign($array = [])
116
    {
117
        foreach ($array as $key => $value) {
118
            if (is_string($key)) {
119
                $this->set($key, $value);
120
            }
121
        }
122
123
        return $this;
124
    }
125
}
126