Passed
Push — master ( 6eb156...b34d93 )
by Thierry
04:25
created

ViewRenderer::setDefaultNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Ui\View;
4
5
use function strrpos;
6
use function substr;
7
use function array_merge;
8
9
class ViewRenderer
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ViewRenderer
Loading history...
10
{
11
    /**
12
     * The view data store
13
     *
14
     * @var Store
15
     */
16
    protected $xStore = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
17
18
    /**
19
     * The default namespace
20
     *
21
     * @var string
22
     */
23
    protected $sDefaultNamespace = 'jaxon';
24
25
    /**
26
     * The view global data
27
     *
28
     * @var array
29
     */
30
    protected $aViewData = [];
31
32
    /**
33
     * The view manager
34
     *
35
     * @var ViewManager
36
     */
37
    protected $xViewManager;
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xViewManager should have a doc-comment as per coding-style.
Loading history...
40
     * The constructor
41
     */
42
    public function __construct(ViewManager $xViewManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
43
    {
44
        $this->xViewManager = $xViewManager;
45
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
46
47
    /**
48
     * Set the default namespace
49
     *
50
     * @param string $sDefaultNamespace
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
    public function setDefaultNamespace(string $sDefaultNamespace): void
53
    {
54
        $this->sDefaultNamespace = $sDefaultNamespace;
55
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
56
57
    /**
58
     * Get the current store or create a new store
59
     *
60
     * @return Store
61
     */
62
    protected function store(): Store
63
    {
64
        if(!$this->xStore)
65
        {
66
            $this->xStore = new Store();
67
        }
68
        return $this->xStore;
69
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
70
71
    /**
72
     * Make a piece of data available for the rendered view
73
     *
74
     * @param string $sName    The data name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
75
     * @param mixed $xValue    The data value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
76
     *
77
     * @return ViewRenderer
78
     */
79
    public function set(string $sName, $xValue): ViewRenderer
80
    {
81
        $this->store()->with($sName, $xValue);
82
        return $this;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Make a piece of data available for all views
87
     *
88
     * @param string $sName    The data name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
89
     * @param mixed $xValue    The data value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
90
     *
91
     * @return ViewRenderer
92
     */
93
    public function share(string $sName, $xValue): ViewRenderer
94
    {
95
        $this->aViewData[$sName] = $xValue;
96
        return $this;
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Make an array of data available for all views
101
     *
102
     * @param array $aValues    The data values
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
103
     *
104
     * @return ViewRenderer
105
     */
106
    public function shareValues(array $aValues): ViewRenderer
107
    {
108
        foreach($aValues as $sName => $xValue)
109
        {
110
            $this->share($sName, $xValue);
111
        }
112
        return $this;
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
114
115
    /**
116
     * Render a view using a store
117
     *
118
     * The store returned by this function will later be used with the make() method to render the view.
119
     *
120
     * @param string $sViewName    The view name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
121
     * @param array $aViewData    The view data
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
122
     *
123
     * @return null|Store   A store populated with the view data
124
     */
125
    public function render(string $sViewName, array $aViewData = []): ?Store
126
    {
127
        // Get the store
128
        $xStore = $this->store();
129
        // Get the default view namespace
130
        $sNamespace = $this->sDefaultNamespace;
131
        // Get the namespace from the view name
132
        $nSeparatorPosition = strrpos($sViewName, '::');
133
        if($nSeparatorPosition !== false)
134
        {
135
            $sNamespace = substr($sViewName, 0, $nSeparatorPosition);
136
        }
137
        $xRenderer = $this->xViewManager->getNamespaceRenderer($sNamespace);
138
        if(!$xRenderer)
139
        {
140
            // Cannot render a view if there's no renderer corresponding to the namespace.
141
            return null;
142
        }
143
        $xStore->setData(array_merge($this->aViewData, $aViewData))->setView($xRenderer, $sNamespace, $sViewName);
144
        // Set the store to null so a new store will be created for the next view.
145
        $this->xStore = null;
146
        // Return the store
147
        return $xStore;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
149
}
150