Completed
Push — master ( 2c71ef...178e64 )
by Mathieu
04:04
created

ViewableTrait::setDynamicTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Charcoal\View;
4
5
// Dependencies from `PHP`
6
use InvalidArgumentException;
7
8
// Local namespace dependencies
9
use Charcoal\View\ViewInterface;
10
11
/**
12
 * Implementation, as trait, of the {@see \Charcoal\View\ViewableInterface}.
13
 */
14
trait ViewableTrait
15
{
16
    /**
17
     * The object's template identifier.
18
     *
19
     * @var string
20
     */
21
    private $templateIdent;
22
23
    /**
24
     * The context for the {@see self::$view} to render templates.
25
     *
26
     * @var ViewableInterface
27
     */
28
    private $viewController;
29
30
    /**
31
     * The renderable view.
32
     *
33
     * @var ViewInterface
34
     */
35
    private $view;
36
37
    /**
38
     * Render the viewable object.
39
     *
40
     * @return string
41
     */
42
    public function __toString()
43
    {
44
        return $this->render();
45
    }
46
47
    /**
48
     * Set the template identifier for this viewable object.
49
     *
50
     * Usually, a path to a file containing the template to be rendered at runtime.
51
     *
52
     * @param string $templateIdent The template ID.
53
     * @throws InvalidArgumentException If the template identifier is not a string.
54
     * @return ViewableInterface Chainable
55
     */
56
    public function setTemplateIdent($templateIdent)
57
    {
58
        if (!is_string($templateIdent)) {
59
            throw new InvalidArgumentException(
60
                'Template identifier must be a string.'
61
            );
62
        }
63
64
        $this->templateIdent = $templateIdent;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Retrieve the template identifier for this viewable object.
71
     *
72
     * @return string
73
     */
74
    public function templateIdent()
75
    {
76
        return $this->templateIdent;
77
    }
78
79
    /**
80
     * Set the renderable view.
81
     *
82
     * @param ViewInterface|array $view The view instance to use to render.
83
     * @throws InvalidArgumentException If the view parameter is not an array or a View object.
84
     * @return ViewableInterface Chainable
85
     */
86
    public function setView(ViewInterface $view)
87
    {
88
        $this->view = $view;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Retrieve the renderable view.
95
     *
96
     * @return ViewInterface The object's View instance.
97
     */
98
    public function view()
99
    {
100
        return $this->view;
101
    }
102
103
    /**
104
     * Render the template by the given identifier.
105
     *
106
     * Usually, a path to a file containing the template to be rendered at runtime.
107
     *
108
     * @param string $templateIdent The template to load, parse, and render.
109
     *     If NULL, will use the object's previously set template identifier.
110
     * @return string The rendered template.
111
     */
112
    public function render($templateIdent = null)
113
    {
114
        if ($templateIdent === null) {
115
            $templateIdent = $this->templateIdent();
116
        }
117
118
        return $this->view()->render($templateIdent, $this->viewController());
119
    }
120
121
    /**
122
     * Render the given template from string.
123
     *
124
     * @param string $templateString The template  to render from string.
125
     * @return string The rendered template.
126
     */
127
    public function renderTemplate($templateString)
128
    {
129
        return $this->view()->renderTemplate($templateString, $this->viewController());
130
    }
131
132
    /**
133
     * Set a view controller for the template's context.
134
     *
135
     * @param ViewableInterface|object|array|null $controller A view controller to use when rendering.
136
     * @throws InvalidArgumentException If the controller is invalid.
137
     * @return ViewableInterface Chainable
138
     */
139
    public function setViewController($controller)
140
    {
141
        if (is_scalar($controller) || is_resource($controller)) {
142
            throw new InvalidArgumentException(
143
                'View controller must be an object, null or an array'
144
            );
145
        }
146
147
        $this->viewController = $controller;
0 ignored issues
show
Documentation Bug introduced by
It seems like $controller can also be of type array. However, the property $viewController is declared as type object<Charcoal\View\ViewableInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
148
149
        return $this;
150
    }
151
152
    /**
153
     * Retrieve a view controller for the template's context.
154
     *
155
     * If no controller has been defined, it will return itself.
156
     *
157
     * @return ViewableInterface
158
     */
159
    public function viewController()
160
    {
161
        if ($this->viewController === null) {
162
            return $this;
163
        }
164
165
        return $this->viewController;
166
    }
167
168
    /**
169
     * @param string      $varName       The name of the variable to set this template unto.
170
     * @param string|null $templateIdent The "dynamic template" to set. null to clear.
171
     * @return void
172
     */
173
    public function setDynamicTemplate($varName, $templateIdent)
174
    {
175
        $this->view()->setDynamicTemplate($varName, $templateIdent);
176
    }
177
}
178