Passed
Push — master ( 0602c9...8f96ba )
by Thierry
02:02
created

Renderer::share()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
126
            ->setView($xRenderer, $sNamespace, $sViewName);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
127
        // Set the store to null so a new store will be created for the next view.
128
        $this->xStore = null;
129
        // Return the store
130
        return $xStore;
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
132
}
133