1: <?php
2:
3: namespace PHPixie\HTTP\Data;
4:
5: /**
6: * Server data storage
7: */
8: class Server
9: {
10: /**
11: * @var array
12: */
13: protected $server;
14:
15: /**
16: * Constructor
17: * @param array $server Server data
18: */
19: public function __construct($server = array())
20: {
21: $this->server = $server;
22: }
23:
24: /**
25: * Get value, or if missing return a default one
26: * @param string $key
27: * @param string|null $default
28: * @return string
29: */
30: public function get($key, $default = null)
31: {
32: $key = $this->normalizeKey($key);
33: if(!$this->existsNormalized($key)) {
34: return $default;
35: }
36:
37: return $this->server[$key];
38: }
39:
40: /**
41: * Get value, or if missing throw an exception
42: * @param string $key
43: * @return string
44: * @throws \PHPixie\HTTP\Exception
45: */
46: public function getRequired($key)
47: {
48: $key = $this->normalizeKey($key);
49: if(!$this->existsNormalized($key)) {
50: throw new \PHPixie\HTTP\Exception("server variable '$key' is not set");
51: }
52:
53: return $this->server[$key];
54: }
55:
56: /**
57: * Get data as array
58: * @return array
59: */
60: public function asArray()
61: {
62: return $this->server;
63: }
64:
65: /**
66: * @param string $normlizedKey
67: * @return bool
68: */
69: protected function existsNormalized($normlizedKey)
70: {
71: return array_key_exists($normlizedKey, $this->server);
72: }
73:
74: /**
75: * @param string $key
76: * @return string
77: */
78: protected function normalizeKey($key)
79: {
80: return strtoupper($key);
81: }
82: }