Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Response
4
 * base response, all response objects should inherit from here
5
 *
6
 * @package     erdiko/core
7
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
8
 * @author      John Arroyo <[email protected]>
9
 */
10
namespace erdiko\core;
11
12
13
class Response
14
{
15
    /** Theme object */
16
    protected $_theme = null;
17
    /** Theme name */
18
    protected $_themeName;
19
    /** Theme template */
20
    protected $_themeTemplate = 'default';
21
    /** Theme Ignore (if set to true output is rendered without theme) */
22
    protected $_themeIgnore = false;    
23
    /** Content */
24
    protected $_content = null;
25
    /** Data */
26
    protected $_data = array();
27
    
28
    /**
29
     * Constructor
30
     *
31
     * @param Theme $theme - Theme Object (Container)
32
     */
33
    public function __construct($theme = null)
34
    {
35
        $this->_theme = $theme;
36
    }
37
38
    /**
39
     * Set key value data
40
     *
41
     * @param mixed $key
42
     * @param mixed $value
43
     */
44
    public function setKeyValue($key, $value)
45
    {
46
        $this->_data[$key] = $value;
47
    }
48
49
    /**
50
     * Get data value by key
51
     *
52
     * @param mixed $key
53
     * @return mixed
54
     */
55
    public function getKeyValue($key)
56
    {
57
        return empty($this->_data[$key]) ? null: $this->_data[$key];
58
    }
59
60
    /**
61
     * Add a pool of key/values segmented by type
62
     * This is useful for js/css includes and other grouped data
63
     */
64
    public function addTypedKeyValue($type, $key, $value)
65
    {
66
        if(empty($this->_data[$type]))
67
            $this->_data[$type] = array();
68
        $this->_data[$type][$key] = $value;
69
    }
70
71
    /**
72
     * Set theme
73
     *
74
     * @param Theme object $theme - Theme Object (Container)
75
     */
76
    public function setTheme($theme)
77
    {
78
        $this->_theme = $theme;
79
    }
80
81
    /**
82
     * Get theme
83
     *
84
     * @return Theme Object $theme
85
     */
86
    public function getTheme()
87
    {
88
        if($this->_theme === null)
89
            $this->_theme = new \erdiko\core\Theme($this->getThemeName(), null, $this->getThemeTemplate());
90
91
        return $this->_theme;
92
    }
93
94
    /**
95
     * Set Theme Name
96
     *
97
     * @param string $themeName
98
     */
99
    public function setThemeName($themeName)
100
    {
101
        $this->_themeName = $themeName;
102
    }
103
104
    /**
105
     * Get the theme name
106
     * Name pecking order: response, theme, config
107
     *
108
     * @return string $name
109
     */
110
    public function getThemeName()
111
    {
112
        if(!empty($this->_themeName))
113
            $name = $this->_themeName;
114
        elseif(!empty($this->_theme))
115
            $name = $this->_theme->getName();
116
        else
117
            $name = Helper::getConfig()['theme']['name'];
118
        
119
        return $name;
120
    }
121
122
    /**
123
     * Set Theme Template
124
     *
125
     * @param string $tamplate
0 ignored issues
show
Bug introduced by
There is no parameter named $tamplate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
126
     */
127
    public function setThemeTemplate($template)
128
    {
129
        $this->_themeTemplate = $template;
130
        if ($this->getTheme() != null) {
131
            $this->getTheme()->setTemplate($this->_themeTemplate);
132
        }
133
    }
134
135
    /**
136
     * Get the theme template
137
     *
138
     * @return string $_themeTemplate
139
     */
140
    public function getThemeTemplate()
141
    {
142
        return $this->_themeTemplate;
143
    }
144
145
    /**
146
     * Set content
147
     *
148
     * @param Container $content - e.g. View or Layout Object
149
     */
150
    public function setContent($content)
151
    {
152
        $this->_content = $content;
153
    }
154
155
    /**
156
     * Get content
157
     *
158
     * @return string
159
     */
160
    public function getContent()
161
    {
162
        return $this->_content;
163
    }
164
165
    /**
166
     * Append some html content to the existing content
167
     *
168
     * @param string $content
169
     * @todo check to see if content is a container, if so treat accordingly
170
     */
171
    public function appendContent($content)
172
    {
173
        $this->_content .= $content;
174
    }
175
    
176
    /**
177
     * Render
178
     *
179
     * @return string
180
     */
181
    public function render()
182
    {
183
        // Render all objects to html (string)
184
        $html = (string) $this->_content;
185
186
        if (!$this->_themeIgnore) {
187
            $theme = $this->getTheme();
188
            $theme->setContent($html); // rendered html (body content)
189
            $html = (string) $theme;
190
        }
191
192
        return $html;
193
    }
194
195
    /**
196
     * Render and send data to browser then end request
197
     *
198
     * @notice USE WITH CAUTION, 
199
     *  This should be called at the end of processing the response
200
     */
201
    public function send()
202
    {
203
        echo $this->render();
204
    }
205
}
206