index/src/Http/Headers/IfNoneMatchHeader.php

47 lines
1.1 KiB
PHP

<?php
// IfNoneMatchHeader.php
// Created: 2022-02-14
// Updated: 2022-02-27
namespace Index\Http\Headers;
use Index\Http\HttpHeader;
class IfNoneMatchHeader {
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($tag, $this->tags);
}
public static function parse(HttpHeader $header): IfNoneMatchHeader {
$tags = [];
$rawTags = array_unique(
array_map(
fn($directive) => trim($directive),
explode(',', strtolower($header->getFirstLine()))
)
);
foreach($rawTags as $raw) {
if(str_starts_with($raw, 'W/"'))
$raw = substr($raw, 2);
$tags[] = trim($raw, '"');
}
return new IfNoneMatchHeader($tags);
}
}