<?php
 declare(strict_types=1); namespace Shopify\Clients; final class HttpHeaders { public const X_SHOPIFY_ACCESS_TOKEN = "X-Shopify-Access-Token"; public const X_SHOPIFY_STOREFRONT_ACCESS_TOKEN = "X-Shopify-Storefront-Access-Token"; public const X_REQUEST_ID = "x-request-id"; public const X_SHOPIFY_HMAC = 'X-Shopify-Hmac-Sha256'; public const X_SHOPIFY_TOPIC = 'X-Shopify-Topic'; public const X_SHOPIFY_DOMAIN = 'X-Shopify-Shop-Domain'; public const X_SHOPIFY_API_DEPRECATED_REASON = 'X-Shopify-API-Deprecated-Reason'; public const PAGINATION_HEADER = 'Link'; public const CONTENT_TYPE = 'Content-Type'; public const CONTENT_LENGTH = 'Content-Length'; public const USER_AGENT = 'User-Agent'; public const RETRY_AFTER = 'Retry-After'; private $headerSet = []; public function __construct(array $headers) { $this->normalizeAndLoadHeaders($headers); } public function has(string $header, bool $allowEmpty = true): bool { list($header) = $this->normalizeHeader($header); return ( array_key_exists($header, $this->headerSet) && ($allowEmpty || !empty($this->headerSet[$header])) ); } public function diff(array $headers, bool $allowEmpty = true): array { $missing = []; foreach ($headers as $header) { if (!$this->has($header, $allowEmpty)) { $missing[] = $header; } } return $missing; } public function get(string $header): ?string { if (!$this->has($header)) { return null; } list($header) = $this->normalizeHeader($header); return implode(',', $this->headerSet[$header]); } public function toArray(): array { return $this->headerSet; } private function normalizeAndLoadHeaders(array $headers): void { $this->headerSet = []; foreach ($headers as $header => $value) { list($header, $value) = $this->normalizeHeader($header, $value); $this->headerSet[$header] = $value; } } private function normalizeHeader(string $header, $value = null): array { if (empty($value)) { $value = null; } else { $value = array_map( function ($item) { return $item ? (string)$item : null; }, (array)$value ); } return [strtolower($header), $value]; } } 