Completed
Pull Request — master (#1)
by Mathieu
04:21
created

ViewableTrait::templateEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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