index/src/Http/Headers/IfRangeHeader.php

59 lines
1.4 KiB
PHP

<?php
// IfRangeHeader.php
// Created: 2022-02-14
// Updated: 2022-02-27
namespace Index\Http\Headers;
use DateTimeInterface;
use Index\DateTime;
use Index\Http\HttpHeader;
class IfRangeHeader {
private ?string $tag;
private ?DateTime $dateTime;
public function __construct(?string $tag, ?DateTime $dateTime) {
$this->tag = $tag;
$this->dateTime = $dateTime;
}
public function hasTag(): bool {
return $this->tag !== null;
}
public function getTag(): string {
return $this->tag;
}
public function hasDateTime(): bool {
return $this->dateTime !== null;
}
public function getDateTime(): DateTime {
return $this->dateTime;
}
public function matches(string|DateTimeInterface $other): bool {
if($this->hasTag() && is_string($other)) {
if(str_starts_with($other, 'W/"'))
return false;
return $this->tag === $other;
}
if($this->hasDateTime() && $other instanceof DateTimeInterface)
return $this->dateTime->isLessThanOrEqual($other);
return false;
}
public static function parse(HttpHeader $header): IfRangeHeader {
$line = $header->getFirstLine();
if($line[0] !== '"' && !str_starts_with($line, 'W/"'))
return new IfRangeHeader(null, DateTime::createFromFormat(\DateTimeInterface::RFC7231, $line));
return new IfRangeHeader($line, null);
}
}