index/src/Http/Headers/ContentRangeHeader.php

75 lines
1.8 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// ContentRangeHeader.php
// Created: 2022-02-14
// Updated: 2023-01-01
2022-09-13 13:13:11 +00:00
namespace Index\Http\Headers;
use Index\Http\HttpHeader;
class ContentRangeHeader {
private string $unit;
private int $rangeStart = -1;
private int $rangeEnd = -1;
private int $size = -1;
public function __construct(string $unit, int $rangeStart, int $rangeEnd, int $size) {
$this->unit = $unit;
$this->rangeStart = $rangeStart;
$this->rangeEnd = $rangeEnd;
$this->size = $size;
}
public function getUnit(): string {
return $this->unit;
}
public function isBytes(): bool {
return $this->unit === 'bytes';
}
public function getSize(): int {
return $this->size;
}
public function isUnknownSize(): bool {
return $this->size < 0;
}
public function isUnspecifiedRange(): bool {
return $this->rangeStart < 0 || $this->rangeEnd < 0;
}
public function getRangeStart(): int {
return $this->rangeStart;
}
public function getRangeEnd(): int {
return $this->rangeEnd;
}
public static function parse(HttpHeader $header): ContentRangeHeader {
$parts = explode(' ', trim($header->getFirstLine()), 2);
$unit = array_shift($parts);
$parts = explode('/', $parts[1] ?? '', 2);
$size = trim($parts[1] ?? '*');
if($size !== '*')
$size = max(0, (int)$size);
2022-09-13 13:13:11 +00:00
else
$size = -1;
$range = trim($parts[0]);
if($range !== '*') {
$parts = explode('-', $range, 2);
$rangeStart = (int)trim($parts[0]);
$rangeEnd = (int)trim($parts[1] ?? '0');
2022-09-13 13:13:11 +00:00
} else
$rangeStart = $rangeEnd = -1;
return new ContentRangeHeader($unit, $rangeStart, $rangeEnd, $size);
}
}