/** * Make uri * * @param array $globals The $_SERVER super-global * @return \Psr\Http\Message\UriInterface */ public function makeUri(array $globals) : UriInterface { $env = new Collection($globals); // Scheme $isSecure = $env->get('HTTPS'); $scheme = empty($isSecure) || $isSecure === 'off' ? 'http' : 'https'; // Authority: Username and password $username = $env->get('PHP_AUTH_USER', ''); $password = $env->get('PHP_AUTH_PW', ''); // Authority: Host if ($env->has('HTTP_HOST')) { $host = $env->get('HTTP_HOST'); } else { $host = $env->get('SERVER_NAME'); } // Authority: Port $port = (int) $env->get('SERVER_PORT', 80); if (preg_match('/^(\\[[a-fA-F0-9:.]+\\])(:\\d+)?\\z/', $host, $matches)) { $host = $matches[1]; if ($matches[2]) { $port = (int) substr($matches[2], 1); } } else { $pos = strpos($host, ':'); if ($pos !== false) { $port = (int) substr($host, $pos + 1); $host = strstr($host, ':', true); } } // parse_url() requires a full URL. As we don't extract the domain name or scheme, // we use a stand-in. $requestUri = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_PATH); // Query string $queryString = $env->get('QUERY_STRING', ''); // Fragment $fragment = ''; // Build Uri return new Uri($scheme, $host, $port, $requestUri, $queryString, $fragment, $username, $password); }
/** * Retrieve a single derived request attribute. * * Retrieves a single derived request attribute as described in * getAttributes(). If the attribute has not been previously set, returns * the default value as provided. * * This method obviates the need for a hasAttribute() method, as it allows * specifying a default value to return if the attribute is not found. * * @see getAttributes() * @param string $name The attribute name. * @param mixed $default Default value to return if the attribute does not exist. * @return mixed */ public function getAttribute($name, $default = null) { return $this->attributes->get($name, $default); }
public function testGetWithDefault() { $this->property->setValue($this->bag, ['foo' => 'bar']); $this->assertEquals('default', $this->bag->get('abc', 'default')); }
/** * Get HTTP header key as originally specified * * @param string $key The case-insensitive header name * @param mixed $default The default value if key does not exist * * @return string */ public function getOriginalKey($key, $default = null) { if ($this->has($key)) { return parent::get($this->normalizeKey($key))['originalKey']; } return $default; }