Phly\Http\ServerRequestFactory::marshalHeaders PHP Method

marshalHeaders() public static method

Marshal headers from $_SERVER
public static marshalHeaders ( array $server ) : array
$server array
return array
    public static function marshalHeaders(array $server)
    {
        $headers = array();
        foreach ($server as $key => $value) {
            if (strpos($key, 'HTTP_COOKIE') === 0) {
                // Cookies are handled using the $_COOKIE superglobal
                continue;
            }
            if ($value && strpos($key, 'HTTP_') === 0) {
                $name = strtr(substr($key, 5), '_', ' ');
                $name = strtr(ucwords(strtolower($name)), ' ', '-');
                $name = strtolower($name);
                $headers[$name] = $value;
                continue;
            }
            if ($value && strpos($key, 'CONTENT_') === 0) {
                $name = substr($key, 8);
                // Content-
                $name = 'Content-' . ($name == 'MD5' ? $name : ucfirst(strtolower($name)));
                $name = strtolower($name);
                $headers[$name] = $value;
                continue;
            }
        }
        return $headers;
    }

Usage Example

 public function testMarshalsExpectedHeadersFromServerArray()
 {
     $server = ['HTTP_COOKIE' => 'COOKIE', 'HTTP_AUTHORIZATION' => 'token', 'HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json', 'HTTP_X_FOO_BAR' => 'FOOBAR', 'CONTENT_MD5' => 'CONTENT-MD5', 'CONTENT_LENGTH' => 'UNSPECIFIED'];
     $expected = ['authorization' => 'token', 'content-type' => 'application/json', 'accept' => 'application/json', 'x-foo-bar' => 'FOOBAR', 'content-md5' => 'CONTENT-MD5', 'content-length' => 'UNSPECIFIED'];
     $this->assertEquals($expected, ServerRequestFactory::marshalHeaders($server));
 }