|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-client-curl/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-client-curl |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Client\Curl; |
|
15
|
|
|
|
|
16
|
|
|
use CURLFile; |
|
17
|
|
|
use Psr\Http\Message\StreamInterface; |
|
18
|
|
|
use Sunrise\Http\Message\Exception\RuntimeException; |
|
19
|
|
|
|
|
20
|
|
|
use function array_walk_recursive; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @since 2.2.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
final class FormData implements StreamInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public readonly array $data; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array<array-key, mixed> $data |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(array $data) |
|
36
|
|
|
{ |
|
37
|
|
|
array_walk_recursive($data, static function (mixed &$value): void { |
|
38
|
|
|
if ($value instanceof StreamInterface) { |
|
39
|
|
|
/** @var string $uri */ |
|
40
|
|
|
$uri = $value->getMetadata('uri'); |
|
41
|
|
|
$value = new CURLFile($uri); |
|
42
|
|
|
} |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$this->data = $data; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function __toString(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function close(): void |
|
54
|
|
|
{ |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function detach(): mixed |
|
58
|
|
|
{ |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getSize(): ?int |
|
63
|
|
|
{ |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function tell(): int |
|
68
|
|
|
{ |
|
69
|
|
|
throw new RuntimeException('Stream does not support the tell operation'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function eof(): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function isSeekable(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function seek(int $offset, int $whence = SEEK_SET): void |
|
83
|
|
|
{ |
|
84
|
|
|
throw new RuntimeException('Stream is not seekable'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function rewind(): void |
|
88
|
|
|
{ |
|
89
|
|
|
throw new RuntimeException('Stream is not seekable'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function isWritable(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function write(string $string): int |
|
98
|
|
|
{ |
|
99
|
|
|
throw new RuntimeException('Stream is not writable'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function isReadable(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function read(int $length): string |
|
108
|
|
|
{ |
|
109
|
|
|
throw new RuntimeException('Stream is not readable'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getContents(): string |
|
113
|
|
|
{ |
|
114
|
|
|
throw new RuntimeException('Stream is not readable'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getMetadata(?string $key = null): mixed |
|
118
|
|
|
{ |
|
119
|
|
|
return $key === null ? [] : null; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|