index/src/Http/Headers/OriginHeader.php

62 lines
1.4 KiB
PHP

<?php
// OriginHeader.php
// Created: 2022-02-14
// Updated: 2022-02-27
namespace Index\Http\Headers;
use Index\DateTime;
use Index\Http\HttpHeader;
class OriginHeader {
private bool $isNull;
private string $scheme;
private string $host;
private int $port;
public function __construct(bool $isNull, string $scheme, string $host, int $port) {
$this->isNull = $isNull;
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
}
public function isNull(): bool {
return $this->isNull;
}
public function isValid(): bool {
return $this->isNull || ($this->scheme !== '' && $this->host !== '');
}
public function getScheme(): string {
return $this->scheme;
}
public function getHost(): string {
return $this->host;
}
public function hasPort(): bool {
return $this->port > 0;
}
public function getPort(): int {
return $this->port;
}
public static function parse(HttpHeader $header): OriginHeader {
$line = $header->getFirstLine();
if($line === 'null')
return new OriginHeader(true, '', '', -1);
return new OriginHeader(
false,
parse_url($line, PHP_URL_SCHEME) ?? '',
parse_url($line, PHP_URL_HOST) ?? '',
parse_url($line, PHP_URL_PORT) ?? -1
);
}
}