Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

ViewRenderer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 7 2
A share() 0 4 1
A shareValues() 0 7 2
A set() 0 4 1
A render() 0 27 3
A __construct() 0 3 1
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 view global data
20
     *
21
     * @var array
22
     */
23
    protected $aViewData = [];
24
25
    /**
26
     * The view manager
27
     *
28
     * @var ViewManager
29
     */
30
    protected $xViewManager;
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xViewManager should have a doc-comment as per coding-style.
Loading history...
33
     * The constructor
34
     */
35
    public function __construct(ViewManager $xViewManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
36
    {
37
        $this->xViewManager = $xViewManager;
38
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
39
40
    /**
41
     * Get the current store or create a new store
42
     *
43
     * @return Store
44
     */
45
    protected function store(): Store
46
    {
47
        if(!$this->xStore)
48
        {
49
            $this->xStore = new Store();
50
        }
51
        return $this->xStore;
52
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
53
54
    /**
55
     * Make a piece of data available for the rendered view
56
     *
57
     * @param string $sName    The data name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
58
     * @param mixed $xValue    The data value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
59
     *
60
     * @return ViewRenderer
61
     */
62
    public function set(string $sName, $xValue): ViewRenderer
63
    {
64
        $this->store()->with($sName, $xValue);
65
        return $this;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * Make a piece of data available for all views
70
     *
71
     * @param string $sName    The data name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
72
     * @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...
73
     *
74
     * @return ViewRenderer
75
     */
76
    public function share(string $sName, $xValue): ViewRenderer
77
    {
78
        $this->aViewData[$sName] = $xValue;
79
        return $this;
80
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
81
82
    /**
83
     * Make an array of data available for all views
84
     *
85
     * @param array $aValues    The data values
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
86
     *
87
     * @return ViewRenderer
88
     */
89
    public function shareValues(array $aValues): ViewRenderer
90
    {
91
        foreach($aValues as $sName => $xValue)
92
        {
93
            $this->share($sName, $xValue);
94
        }
95
        return $this;
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Render a view using a store
100
     *
101
     * The store returned by this function will later be used with the make() method to render the view.
102
     *
103
     * @param string $sViewName    The view name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
104
     * @param array $aViewData    The view data
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
105
     *
106
     * @return null|Store   A store populated with the view data
107
     */
108
    public function render(string $sViewName, array $aViewData = []): ?Store
109
    {
110
        // Get the store
111
        $xStore = $this->store();
112
113
        // Get the default view namespace
114
        $sNamespace = $this->xViewManager->getDefaultNamespace();
115
        // Get the namespace from the view name
116
        $nSeparatorPosition = strrpos($sViewName, '::');
117
        if($nSeparatorPosition !== false)
118
        {
119
            $sNamespace = substr($sViewName, 0, $nSeparatorPosition);
120
        }
121
122
        $xRenderer = $this->xViewManager->getNamespaceRenderer($sNamespace);
123
        if(!$xRenderer)
124
        {
125
            // Cannot render a view if there's no renderer corresponding to the namespace.
126
            return null;
127
        }
128
129
        $xStore->setData(array_merge($this->aViewData, $aViewData))
130
            ->setView($xRenderer, $sNamespace, $sViewName);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
131
        // Set the store to null so a new store will be created for the next view.
132
        $this->xStore = null;
133
        // Return the store
134
        return $xStore;
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
136
}
137