1 <?php
2 /* --------------------------------------------------------------
3 HttpContext.inc.php 2015-03-12 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('HttpContextInterface');
13
14 /**
15 * Class HttpContext
16 *
17 * @category System
18 * @package Http
19 * @subpackage ValueObjects
20 * @extends HttpContextInterface
21 */
22 class HttpContext implements HttpContextInterface
23 {
24 /**
25 * @var array
26 */
27 protected $serverArray;
28
29 /**
30 * @var array
31 */
32 protected $getArray;
33
34 /**
35 * @var array
36 */
37 protected $postArray;
38
39 /**
40 * @var array
41 */
42 protected $cookieArray;
43
44 /**
45 * @var array
46 */
47 protected $sessionArray;
48
49
50 /**
51 * Initializes the http context.
52 *
53 * @param array $serverArray Usually, the $_SERVER array is passed here.
54 * @param array $getArray Usually, the $_GET array is passed here.
55 * @param array $postArray Usually, the $_POST array is passed here.
56 * @param array $cookieArray Usually, the $_COOKIE array is passed here.
57 * @param array $sessionArray Usually, the $_SESSION array is passed here.
58 */
59 public function __construct(array $serverArray,
60 array $getArray,
61 array $postArray,
62 array $cookieArray,
63 array $sessionArray)
64 {
65 $this->serverArray = $serverArray;
66 $this->getArray = $getArray;
67 $this->postArray = $postArray;
68 $this->cookieArray = $cookieArray;
69 $this->sessionArray = $sessionArray;
70 }
71
72
73 /**
74 * Returns an item of the $_SERVER array by the given key name.
75 *
76 * @param string $keyName Key to determine which value of the $_SERVER array should be returned.
77 *
78 * @return array|string|int|double Expected item of $_SERVER array.
79 */
80 public function getServerItem($keyName)
81 {
82 if(!array_key_exists($keyName, $this->serverArray))
83 {
84 return null;
85 }
86
87 return $this->serverArray[$keyName];
88 }
89
90
91 /**
92 * Returns an item of the $_GET array by the given key name.
93 *
94 * @param string $keyName Key to determine which value of the $_GET array should be returned.
95 *
96 * @return array|string|int|double Expected item of $_GET array.
97 */
98 public function getGetItem($keyName)
99 {
100 if(!array_key_exists($keyName, $this->getArray))
101 {
102 return null;
103 }
104
105 return $this->getArray[$keyName];
106 }
107
108
109 /**
110 * Returns an item of the $_POST array by the given key name.
111 *
112 * @param string $keyName Key to determine which value of the $_POST array should be returned.
113 *
114 * @return array|string|int|double Expected item of $_POST array.
115 */
116 public function getPostItem($keyName)
117 {
118 if(!array_key_exists($keyName, $this->postArray))
119 {
120 return null;
121 }
122
123 return $this->postArray[$keyName];
124 }
125
126
127 /**
128 * Returns an item of the $_COOKIE array by the given key name.
129 *
130 * @param string $keyName Key to determine which value of the $_COOKIE array should be returned.
131 *
132 * @return array|string|int|double Expected item of $_COOKIE array.
133 */
134 public function getCookieItem($keyName)
135 {
136 if(!array_key_exists($keyName, $this->cookieArray))
137 {
138 return null;
139 }
140
141 return $this->cookieArray[$keyName];
142 }
143
144
145 /**
146 * Returns an item of the $_SESSION array by the given key name.
147 *
148 * @param string $keyName Key to determine which value of the $_SESSION array should be returned.
149 *
150 * @return array|string|int|double Expected item of $_SESSION array.
151 */
152 public function getSessionItem($keyName)
153 {
154 if(!array_key_exists($keyName, $this->sessionArray))
155 {
156 return null;
157 }
158
159 return $this->sessionArray[$keyName];
160 }
161
162
163 /**
164 * Returns an array which is equal to the global $_GET variable in an object oriented layer.
165 *
166 * @return array Array which is equal to $_GET.
167 */
168 public function getGetArray()
169 {
170 return $this->getArray;
171 }
172
173
174 /**
175 * Returns an array which is equal to the global $_POST variable in an object oriented layer.
176 *
177 * @return array Array which is equal to $_POST.
178 */
179 public function getPostArray()
180 {
181 return $this->postArray;
182 }
183 }