/** * Build HeaderBag from Server variables ($_SERVER). * * @param array $headers * * @return HeaderBag */ public static function fromServer(array $headers) { $headerBag = new HeaderBag(); foreach ($headers as $name => $values) { foreach ((array) $values as $value) { $parts = explode('-', str_replace('_', '-', strtolower($name))); $name = implode('-', array_map('ucfirst', $parts)); $namespace = 'Borobudur\\Http\\Header'; if (0 === strpos($name, 'Accept')) { $namespace = sprintf('%s\\Accept', $namespace); } elseif (0 === strpos($name, 'Content')) { $namespace = sprintf('%s\\Content', $namespace); } $class = sprintf('%s\\%sHeader', $namespace, str_replace('-', '', $name)); if (class_exists($class)) { /** * @var HeaderInterface $class */ $headerBag->set($class::fromString(sprintf('%s: %s', $name, $value)), true); } else { $headerBag->set(new GenericHeader($name, $value), true); } } } return $headerBag; }
public function testHeaderBag() { $header = new HeaderBag(); $header->set(new ContentTypeHeader('text/html')); $header->set((new CacheControlHeader())->setNoCache()); $this->assertTrue($header->has('Content-Type')); $this->assertSame(2, $header->count()); $header->remove('Content-Type'); $this->assertSame(1, $header->count()); }
/** * Constructor. * * @param array $server * @param array $query * @param array $request * @param array $cookies * @param array $files * @param SessionHandlerInterface|null $sessionHandler * @param string|null $content */ public function __construct(array $server = array(), array $query = array(), array $request = array(), array $cookies = array(), array $files = array(), SessionHandlerInterface $sessionHandler = null, $content = null) { $this->server = new ServerBag($server); $this->query = new ParameterBag($query); $this->request = new ParameterBag($request); $this->cookies = new ParameterBag($cookies); $this->headers = HeaderBag::fromServer($this->server->getHeaders()); $this->schemaHost = new SchemaHost($this->server); $this->uri = new Uri($this->server); $this->url = new Url($this->uri, $this->schemaHost, $this->getQueryString()); $this->files = new UploadFileBag($files); $this->session = new Session($sessionHandler ?: new NativeSessionSaveHandler()); $this->content = $content; }
/** * Set content type. * * @param string $contentType * * @return $this */ public function setContentType($contentType) { $this->headers->set(new ContentTypeHeader($contentType)); $this->contentType = $contentType; return $this; }