Store::setView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
namespace Jaxon\App\View;
4
5
use JsonSerializable;
6
use Stringable;
7
8
use function array_merge;
9
10
class Store implements JsonSerializable, Stringable
11
{
12
    /**
13
     * The view renderer
14
     *
15
     * @var ViewInterface
16
     */
17
    protected $xRenderer;
18
19
    /**
20
     * The view namespace
21
     *
22
     * @var string
23
     */
24
    protected $sNamespace;
25
26
    /**
27
     * The view name
28
     *
29
     * @var string
30
     */
31
    protected $sViewName;
32
33
    /**
34
     * The view data
35
     *
36
     * @var array
37
     */
38
    protected $aViewData = [];
39
40
    /**
41
     * Make a piece of data available for the rendered view
42
     *
43
     * @param string $sName    The data name
44
     * @param mixed $xValue    The data value
45
     *
46
     * @return Store
47
     */
48
    public function with(string $sName, $xValue): Store
49
    {
50
        $this->aViewData[$sName] = $xValue;
51
        return $this;
52
    }
53
54
    /**
55
     * Set the data to be rendered
56
     *
57
     * @param array $aViewData    The view data
58
     *
59
     * @return Store
60
     */
61
    public function setData(array $aViewData): Store
62
    {
63
        $this->aViewData = array_merge($this->aViewData, $aViewData);
64
        return $this;
65
    }
66
67
    /**
68
     * Set the view to be rendered, with optional data
69
     *
70
     * @param ViewInterface $xRenderer    The view renderer
71
     * @param string $sNamespace    The view namespace
72
     * @param string $sViewName    The view name
73
     * @param array $aViewData    The view data
74
     *
75
     * @return Store
76
     */
77
    public function setView(ViewInterface $xRenderer, string $sNamespace, string $sViewName, array $aViewData = []): Store
78
    {
79
        $this->xRenderer = $xRenderer;
80
        $this->sNamespace = trim($sNamespace);
81
        $this->sViewName = trim($sViewName);
82
        $this->aViewData = array_merge($this->aViewData, $aViewData);
83
        return $this;
84
    }
85
86
    /**
87
     * Get the view namespace
88
     *
89
     * @return string        The view namespace
90
     */
91
    public function getNamespace(): string
92
    {
93
        return $this->sNamespace;
94
    }
95
96
    /**
97
     * Get the view name
98
     *
99
     * @return string        The view name
100
     */
101
    public function getViewName(): string
102
    {
103
        return $this->sViewName;
104
    }
105
106
    /**
107
     * Get the view data
108
     *
109
     * @return array         The view data
110
     */
111
    public function getViewData(): array
112
    {
113
        return $this->aViewData;
114
    }
115
116
    /**
117
     * Render a view using third party view system
118
     *
119
     * @return string        The string representation of the view
120
     */
121
    public function __toString(): string
122
    {
123
        return !$this->xRenderer ? '' : $this->xRenderer->render($this);
124
    }
125
126
    /**
127
     * Convert this object to string for json.
128
     *
129
     * This is a method of the JsonSerializable interface.
130
     *
131
     * @return string
132
     */
133
    public function jsonSerialize(): string
134
    {
135
        return $this->__toString();
136
    }
137
}
138