Passed
Push — master ( 11e596...ed648f )
by Gabriel
13:25
created

CanRenderTrait::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\View\Traits;
5
6
use League\Plates\Template\Template;
7
8
/**
9
 * Class CanRenderTrait
10
 * @package Nip\View\Traits
11
 */
12
trait CanRenderTrait
13
{
14
    protected $blocks = [];
15
16
    /**
17
     * @param bool|Callable $condition
18 2
     * @param $view
19
     * @param array $variables
20 2
     * @param bool $return
21 2
     * @return bool|string|void|null
22 2
     */
23
    public function loadIf($condition, $view, $variables = [], $return = false)
24 2
    {
25
        $condition = is_callable($condition) ? $condition() : $condition;
26
        if ($condition == false) {
27
            return;
28
        }
29
        return $this->load($view, $variables, $return);
30
    }
31
32
    /**
33 1
     * @param $view
34
     * @param array $variables
35 1
     * @param bool $return
36
     * @return bool|string|void|null
37
     */
38
    public function loadIfExists($view, $variables = [], $return = false)
39
    {
40
        return $this->loadIf($this->existPath($view), $view, $variables, $return);
0 ignored issues
show
Bug introduced by
It seems like existPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return $this->loadIf($this->/** @scrutinizer ignore-call */ existPath($view), $view, $variables, $return);
Loading history...
41
    }
42
43
    /**
44
     * @param $view
45 4
     * @param array $variables
46
     * @param bool $return
47 4
     * @return bool|string|void|null
48
     */
49 4
    public function loadWithFallback($view, $fallback, $variables = [], $return = false)
50
    {
51
        $view = $this->existPath($view) ? $view : $fallback;
52
        return $this->load($view, $variables, $return);
53 4
    }
54
55 4
    /** @noinspection PhpInconsistentReturnPointsInspection
56
     *
57
     * @param $view
58
     * @param array $variables
59
     * @param bool $return
60
     * @return string|boolean
61
     */
62
    public function load($view, $variables = [], $return = false)
63 4
    {
64
        $html = $this->getContents($view, $variables);
0 ignored issues
show
Deprecated Code introduced by
The function Nip\View\Traits\CanRenderTrait::getContents() has been deprecated: use render($view, $variables) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
        $html = /** @scrutinizer ignore-deprecated */ $this->getContents($view, $variables);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65 4
66
        if ($return === true) {
67 4
            return $html;
68
        }
69 4
70 4
        echo $html;
71
72 4
        return null;
73 4
    }
74 4
75
    /**
76 4
     * @param $view
77
     * @param array $variables
78
     * @return string
79
     * @deprecated use render($view, $variables)
80
     */
81
    public function getContents($view, array $variables = [])
82
    {
83
        return $this->render($view, $variables);
84
    }
85
86
    /**
87
     * @param string $block
88
     */
89
    public function render($name, array $data = [])
90
    {
91
        if ($this->isBlock($name)) {
92
            $name = "/" . $this->blocks[$name];
93
        }
94
        return parent::render($name, $data);
95
    }
96
97
    /**
98
     * Builds path for including
99
     * If $view starts with / the path will be relative to the root of the views folder.
100
     * Otherwise to caller file location.
101
     *
102
     * @param string $view
103
     * @return string
104
     */
105
    public function buildPath($view)
106
    {
107
        return $this->getResolveTemplatePath()->find($view);
0 ignored issues
show
Bug introduced by
It seems like getResolveTemplatePath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        return $this->/** @scrutinizer ignore-call */ getResolveTemplatePath()->find($view);
Loading history...
108
    }
109
110
    /**
111
     * @param $name
112
     * @return mixed
113
     */
114
    public function getBlock($name)
115
    {
116
        return $this->blocks[$name];
117
    }
118
119
    /**
120
     * @param $name
121
     * @param $block
122
     */
123
    public function setBlock($name, $block)
124
    {
125
        $this->blocks[$name] = $block;
126
    }
127
128
    /**
129
     * @param string $block
130
     * @return bool
131
     */
132
    public function isBlock($block = 'default'): bool
133
    {
134
        if (!is_string($block)) {
0 ignored issues
show
introduced by
The condition is_string($block) is always true.
Loading history...
135
            return false;
136
        }
137
        return isset($this->blocks[$block]) && !empty($this->blocks[$block]);
138
    }
139
140
141
    /**
142
     * Create a new template.
143
     * @param string $name
144
     * @return Template
145
     */
146
    public function make($name)
147
    {
148
        return new \Nip\View\Template\Template($this, $name);
0 ignored issues
show
Bug introduced by
$this of type Nip\View\Traits\CanRenderTrait is incompatible with the type League\Plates\Engine expected by parameter $engine of Nip\View\Template\Template::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
        return new \Nip\View\Template\Template(/** @scrutinizer ignore-type */ $this, $name);
Loading history...
149
    }
150
}
151