ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Headers.php
Go to the documentation of this file.
1<?php
9namespace Slim\Http;
10
13
27class Headers extends Collection implements HeadersInterface
28{
34 protected static $special = [
35 'CONTENT_TYPE' => 1,
36 'CONTENT_LENGTH' => 1,
37 'PHP_AUTH_USER' => 1,
38 'PHP_AUTH_PW' => 1,
39 'PHP_AUTH_DIGEST' => 1,
40 'AUTH_TYPE' => 1,
41 ];
42
51 public static function createFromEnvironment(Environment $environment)
52 {
53 $data = [];
54 $environment = self::determineAuthorization($environment);
55 foreach ($environment as $key => $value) {
56 $key = strtoupper($key);
57 if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
58 if ($key !== 'HTTP_CONTENT_LENGTH') {
59 $data[$key] = $value;
60 }
61 }
62 }
63
64 return new static($data);
65 }
66
76 public static function determineAuthorization(Environment $environment)
77 {
78 $authorization = $environment->get('HTTP_AUTHORIZATION');
79
80 if (empty($authorization) && is_callable('getallheaders')) {
81 $headers = getallheaders();
82 $headers = array_change_key_case($headers, CASE_LOWER);
83 if (isset($headers['authorization'])) {
84 $environment->set('HTTP_AUTHORIZATION', $headers['authorization']);
85 }
86 }
87
88 return $environment;
89 }
90
98 public function all()
99 {
100 $all = parent::all();
101 $out = [];
102 foreach ($all as $key => $props) {
103 $out[$props['originalKey']] = $props['value'];
104 }
105
106 return $out;
107 }
108
118 public function set($key, $value)
119 {
120 if (!is_array($value)) {
121 $value = [$value];
122 }
123 parent::set($this->normalizeKey($key), [
124 'value' => $value,
125 'originalKey' => $key
126 ]);
127 }
128
137 public function get($key, $default = null)
138 {
139 if ($this->has($key)) {
140 return parent::get($this->normalizeKey($key))['value'];
141 }
142
143 return $default;
144 }
145
154 public function getOriginalKey($key, $default = null)
155 {
156 if ($this->has($key)) {
157 return parent::get($this->normalizeKey($key))['originalKey'];
158 }
159
160 return $default;
161 }
162
173 public function add($key, $value)
174 {
175 $oldValues = $this->get($key, []);
176 $newValues = is_array($value) ? $value : [$value];
177 $this->set($key, array_merge($oldValues, array_values($newValues)));
178 }
179
187 public function has($key)
188 {
189 return parent::has($this->normalizeKey($key));
190 }
191
197 public function remove($key)
198 {
200 }
201
213 public function normalizeKey($key)
214 {
215 $key = strtr(strtolower($key), '_', '-');
216 if (strpos($key, 'http-') === 0) {
217 $key = substr($key, 5);
218 }
219
220 return $key;
221 }
222}
An exception for terminatinating execution or to throw for unit testing.
Collection.
Definition: Collection.php:22
set($key, $value)
Set collection item.
Definition: Collection.php:50
get($key, $default=null)
Get collection item for key.
Definition: Collection.php:63
has($key)
Does this collection have a given header?
Definition: Headers.php:187
getOriginalKey($key, $default=null)
Get HTTP header key as originally specified.
Definition: Headers.php:154
normalizeKey($key)
Normalize header name.
Definition: Headers.php:213
static determineAuthorization(Environment $environment)
If HTTP_AUTHORIZATION does not exist tries to get it from getallheaders() when available.
Definition: Headers.php:76
all()
Return array of HTTP header names and values.
Definition: Headers.php:98
static $special
Definition: Headers.php:34
static createFromEnvironment(Environment $environment)
Create new headers collection with data extracted from the application Environment object.
Definition: Headers.php:51
add($key, $value)
Add HTTP header value.
Definition: Headers.php:173
$key
Definition: croninfo.php:18
Slim Framework (https://slimframework.com)
Definition: Body.php:9
remove()
Definition: remove.php:2