1
+ <?php
2
+ namespace Gt \Fetch \Response ;
3
+
4
+ /**
5
+ * @property-read int size
6
+ * @property-read string type
7
+ */
8
+ class Blob {
9
+ const ENDINGS_TRANSPARENT = "transparent " ;
10
+ const ENDINGS_NATIVE = "transparent " ;
11
+ const PART_TYPE_ARRAY = "array " ;
12
+ const PART_TYPE_ARRAYBUFFER = "arraybuffer " ;
13
+ const PART_TYPE_BLOB = "blob " ;
14
+ const PART_TYPE_STRING = "string " ;
15
+
16
+ protected $ type ;
17
+ protected $ endings ;
18
+ /** @var string */
19
+ protected $ content ;
20
+
21
+ public function __construct ($ blobParts , array $ options = []) {
22
+ $ this ->type = $ options ["type " ] ?? "" ;
23
+ $ this ->endings = $ options ["endings " ] ?? self ::ENDINGS_TRANSPARENT ;
24
+
25
+ $ partType = $ this ->getBlobPartsType ($ blobParts );
26
+
27
+ switch ($ partType ) {
28
+ case self ::PART_TYPE_ARRAY :
29
+ $ this ->content = $ this ->loadArray ($ blobParts );
30
+ break ;
31
+
32
+ case self ::PART_TYPE_ARRAYBUFFER :
33
+ $ this ->content = $ this ->loadArrayBuffer ($ blobParts );
34
+ break ;
35
+
36
+ case self ::PART_TYPE_BLOB :
37
+ $ this ->content = $ this ->loadBlob ($ blobParts );
38
+ break ;
39
+
40
+ case self ::PART_TYPE_STRING :
41
+ $ this ->content = $ this ->loadString ($ blobParts );
42
+ break ;
43
+ }
44
+ }
45
+
46
+ public function __toString ():string {
47
+ return $ this ->getContent ();
48
+ }
49
+
50
+ public function __get (string $ key ) {
51
+ switch ($ key ) {
52
+ case "size " :
53
+ return $ this ->size ;
54
+ break ;
55
+
56
+ case "type " :
57
+ return $ this ->type ;
58
+ break ;
59
+ }
60
+ }
61
+
62
+ public function getContent ():string {
63
+ return $ this ->content ;
64
+ }
65
+
66
+ protected function getBlobPartsType ($ blobParts ):string {
67
+ $ type = null ;
68
+
69
+ if (is_array ($ blobParts )) {
70
+ return self ::PART_TYPE_ARRAY ;
71
+ }
72
+
73
+ if ($ blobParts instanceof ArrayBuffer) {
74
+ return self ::PART_TYPE_ARRAYBUFFER ;
75
+ }
76
+
77
+ if ($ blobParts instanceof self) {
78
+ return self ::PART_TYPE_BLOB ;
79
+ }
80
+
81
+ if (is_string ($ blobParts )) {
82
+ return self ::PART_TYPE_STRING ;
83
+ }
84
+
85
+ if (is_null ($ type )) {
86
+ throw new InvlidBlobPartTypeException ($ blobParts );
87
+ }
88
+ }
89
+
90
+ protected function loadArray (array $ input ):string {
91
+ return $ this ->loadIterable ($ input );
92
+ }
93
+
94
+ protected function loadArrayBuffer (ArrayBuffer $ input ):string {
95
+ return $ this ->loadIterable ($ input );
96
+ }
97
+
98
+ protected function loadBlob (self $ input ):string {
99
+ return $ input ->content ;
100
+ }
101
+
102
+ protected function loadString (string $ input ):string {
103
+ return $ input ;
104
+ }
105
+
106
+ protected function loadIterable (iterable $ input ):string {
107
+ $ buffer = "" ;
108
+
109
+ foreach ($ input as $ i ) {
110
+ if (self ::ENDINGS_NATIVE ) {
111
+ $ i = str_replace (
112
+ ["\n" , "\r\n" ],
113
+ PHP_EOL ,
114
+ $ i
115
+ );
116
+ }
117
+
118
+ $ buffer .= $ i ;
119
+ }
120
+
121
+ return $ buffer ;
122
+ }
123
+ }
0 commit comments