|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Solid Client PHP project. |
| 5 | + * (c) Kévin Dunglas <kevin@dunglas.fr> |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Dunglas\PhpSolidClient; |
| 13 | + |
| 14 | +use Symfony\Component\WebLink\HttpHeaderParser; |
| 15 | + |
| 16 | +final class ResourceMetadata |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param array<string, list<string>> $wacAllow parsed WAC-Allow header (e.g. ['user' => ['read', 'write'], 'public' => ['read']]) |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + public readonly ?string $contentType = null, |
| 23 | + public readonly ?int $contentLength = null, |
| 24 | + public readonly ?\DateTimeImmutable $lastModified = null, |
| 25 | + public readonly ?string $ldpType = null, |
| 26 | + public readonly array $wacAllow = [], |
| 27 | + public readonly ?string $aclUrl = null, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public static function isContainerType(string $type): bool |
| 32 | + { |
| 33 | + return 'http://www.w3.org/ns/ldp#BasicContainer' === $type |
| 34 | + || 'http://www.w3.org/ns/ldp#Container' === $type; |
| 35 | + } |
| 36 | + |
| 37 | + public function isContainer(): bool |
| 38 | + { |
| 39 | + return null !== $this->ldpType && self::isContainerType($this->ldpType); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param array<string, list<string>> $responseHeaders normalized response headers |
| 44 | + */ |
| 45 | + public static function fromResponseHeaders(array $responseHeaders): self |
| 46 | + { |
| 47 | + $contentType = isset($responseHeaders['content-type'][0]) |
| 48 | + ? explode(';', $responseHeaders['content-type'][0], 2)[0] |
| 49 | + : null; |
| 50 | + |
| 51 | + $contentLength = isset($responseHeaders['content-length'][0]) |
| 52 | + ? (int) $responseHeaders['content-length'][0] |
| 53 | + : null; |
| 54 | + |
| 55 | + $lastModified = isset($responseHeaders['last-modified'][0]) |
| 56 | + ? \DateTimeImmutable::createFromFormat(\DateTimeInterface::RFC7231, $responseHeaders['last-modified'][0]) ?: null |
| 57 | + : null; |
| 58 | + |
| 59 | + $ldpType = null; |
| 60 | + $aclUrl = null; |
| 61 | + $linkProvider = (new HttpHeaderParser())->parse($responseHeaders['link'] ?? []); |
| 62 | + foreach ($linkProvider->getLinks() as $link) { |
| 63 | + $rels = $link->getRels(); |
| 64 | + if (\in_array('type', $rels, true) && str_contains($link->getHref(), 'ldp#')) { |
| 65 | + $ldpType = $link->getHref(); |
| 66 | + } |
| 67 | + if (\in_array('acl', $rels, true)) { |
| 68 | + $aclUrl = $link->getHref(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $wacAllow = []; |
| 73 | + if (isset($responseHeaders['wac-allow'][0])) { |
| 74 | + // Format: user="read write append control",public="read" |
| 75 | + if (preg_match_all('/(\w+)="([^"]*)"/', $responseHeaders['wac-allow'][0], $matches, \PREG_SET_ORDER)) { |
| 76 | + foreach ($matches as $match) { |
| 77 | + $wacAllow[$match[1]] = array_filter(explode(' ', $match[2])); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return new self($contentType, $contentLength, $lastModified, $ldpType, $wacAllow, $aclUrl); |
| 83 | + } |
| 84 | +} |
0 commit comments