Passed
Push — master ( 5ebdc1...038fc1 )
by Gabriel
15:21
created

View   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 31.82%

Importance

Changes 4
Bugs 2 Features 1
Metric Value
eloc 27
c 4
b 2
f 1
dl 0
loc 91
ccs 7
cts 22
cp 0.3182
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setBlock() 0 3 1
A buildPath() 0 3 1
A isBlock() 0 3 2
A assign() 0 9 3
A getBlock() 0 3 1
A render() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\View;
5
6
use ArrayAccess;
7
use Nip\View\Extensions\Helpers\HasHelpersTrait;
8
use Nip\View\ViewFinder\HasViewFinder;
9
10
/**
11
 * Class View
12
 *
13
 */
14
class View implements ViewInterface, ArrayAccess
15
{
16
    use Traits\CanRenderTrait;
17
    use Traits\HasDataTrait;
18
    use Traits\HasExtensionsTrait;
19
    use HasHelpersTrait;
20
    use Traits\HasMethodsTrait;
21
    use Traits\HasPathsTrait;
22
    use Traits\HasRequestTrait;
23
    use Traits\MethodsOverloadingTrait;
24
25
    use HasViewFinder;
26
27
    protected $helpers = [];
28
    protected $blocks = [];
29 11
30
    public function __construct()
31 11
    {
32 11
        $this->addMethodsPipelineStage();
33 11
        $this->addHelpersExtension();
34 11
        $this->initFinder();
35
    }
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 $name
48
     * @param $default
49
     * @return string|null
50
     */
51
    public function getBlock($name, $default = null)
52
    {
53
        return $this->blocks[$name] ?? $default;
54
    }
55
56
    /**
57
     * @param string $block
58
     */
59
    public function render($block = 'default')
60
    {
61
        if (!empty($this->blocks[$block])) {
62
            $this->load("/" . $this->blocks[$block]);
63
        } else {
64
            trigger_error("No $block block", E_USER_ERROR);
65 4
        }
66
    }
67 4
68
    /**
69
     * Builds path for including
70
     * If $view starts with / the path will be relative to the root of the views folder.
71
     * Otherwise to caller file location.
72
     *
73
     * @param string $view
74
     * @return string
75
     */
76
    public function buildPath($view)
77
    {
78
        return $this->getFinder()->find($view);
79
    }
80
81
    /**
82
     * @param string $block
83
     * @return bool
84
     */
85
    public function isBlock($block = 'default')
86
    {
87
        return empty($this->blocks[$block]) ? false : true;
88
    }
89
90
    /**
91
     * Assigns variables in bulk in the current scope
92
     *
93
     * @param array $array
94
     * @return $this
95
     */
96
    public function assign($array = [])
97
    {
98
        foreach ($array as $key => $value) {
99
            if (is_string($key)) {
100
                $this->set($key, $value);
101
            }
102
        }
103
104
        return $this;
105
    }
106
}
107