forked from flow-php/etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteFile.php
More file actions
97 lines (81 loc) · 2.3 KB
/
Copy pathRemoteFile.php
File metadata and controls
97 lines (81 loc) · 2.3 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
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
namespace Flow\ETL\Stream;
use Flow\ETL\Exception\InvalidArgumentException;
/**
* @implements FileStream<array{uri: string, options: array<string, mixed>}>
*/
final class RemoteFile implements FileStream
{
/**
* @var array{
* scheme: string,
* host?: string,
* port?: int,
* user?: string,
* pass?: string,
* query?: string,
* path: string,
* fragment?: string
* }
*/
private array $urlParts;
/**
* @param string $uri
* @param array<string, mixed> $options
*
* @throws InvalidArgumentException
*/
public function __construct(private readonly string $uri, private readonly array $options = [])
{
$urlParts = \parse_url($uri);
if (!\is_array($urlParts)) {
throw new InvalidArgumentException('Invalid remote stream URI');
}
if (!\array_key_exists('scheme', $urlParts)) {
throw new InvalidArgumentException('Stream uri is missing scheme');
}
if (!\str_starts_with($urlParts['scheme'], 'flow-')) {
throw new InvalidArgumentException('Stream scheme must starts with "flow-"');
}
if (!\in_array($urlParts['scheme'], \stream_get_wrappers(), true)) {
throw new InvalidArgumentException("Unknown scheme \"{$urlParts['scheme']}\"");
}
if (!\array_key_exists('path', $urlParts)) {
throw new InvalidArgumentException('Stream uri is missing path');
}
$this->urlParts = $urlParts;
}
public function __serialize() : array
{
return [
'uri' => $this->uri,
'options' => $this->options,
];
}
public function __unserialize(array $data) : void
{
$this->uri = $data['uri'];
/**
* @psalm-suppress PropertyTypeCoercion
* @phpstan-ignore-next-line
*/
$this->urlParts = \parse_url($this->uri);
$this->options = $data['options'];
}
/**
* @return array<string, mixed>
*/
public function options() : array
{
return $this->options;
}
public function scheme() : string
{
return $this->urlParts['scheme'];
}
public function uri() : string
{
return $this->uri;
}
}