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