47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
|
<?php
|
||
|
// IfMatchHeader.php
|
||
|
// Created: 2022-02-14
|
||
|
// Updated: 2022-02-27
|
||
|
|
||
|
namespace Index\Http\Headers;
|
||
|
|
||
|
use Index\Http\HttpHeader;
|
||
|
|
||
|
class IfMatchHeader {
|
||
|
private array $tags;
|
||
|
private int $count;
|
||
|
|
||
|
public function __construct(array $tags) {
|
||
|
$this->tags = $tags;
|
||
|
$this->count = count($tags);
|
||
|
}
|
||
|
|
||
|
public function getTags(): array {
|
||
|
return $this->tags;
|
||
|
}
|
||
|
|
||
|
public function hasTag(string $tag): bool {
|
||
|
if($this->count === 1 && $this->tags[0] === '*')
|
||
|
return true;
|
||
|
return in_array((string)$tag, $this->tags);
|
||
|
}
|
||
|
|
||
|
public static function parse(HttpHeader $header): IfMatchHeader {
|
||
|
$tags = [];
|
||
|
$rawTags = array_unique(
|
||
|
array_map(
|
||
|
fn($directive) => trim($directive),
|
||
|
explode(',', strtolower($header->getFirstLine()))
|
||
|
)
|
||
|
);
|
||
|
|
||
|
foreach($rawTags as $raw) {
|
||
|
if(substr($raw, 0, 3) === 'W/"')
|
||
|
continue;
|
||
|
$tags[] = trim($raw, '"');
|
||
|
}
|
||
|
|
||
|
return new IfMatchHeader($tags);
|
||
|
}
|
||
|
}
|