|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Middleware\Xml; |
|
4
|
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Transport\ParsedContentsInterface; |
|
6
|
|
|
use Psr\Http\Message\StreamInterface; |
|
7
|
|
|
use RingCentral\Psr7\BufferStream; |
|
8
|
|
|
|
|
9
|
|
|
class XmlStream implements StreamInterface, ParsedContentsInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $xml = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var StreamInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $bufferStream; |
|
20
|
|
|
|
|
21
|
6 |
|
public function __construct(array $xml) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$this->xml = $xml; |
|
24
|
6 |
|
$jsonString = json_encode($xml); |
|
25
|
6 |
|
$this->bufferStream = new BufferStream(strlen($jsonString)); |
|
26
|
6 |
|
$this->bufferStream->write($jsonString); |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __toString() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getContents(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function getParsedContents(): array |
|
38
|
|
|
{ |
|
39
|
6 |
|
return $this->xml; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function getContents() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->bufferStream->getContents(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function close() |
|
48
|
|
|
{ |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function detach() |
|
52
|
|
|
{ |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function getSize() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->bufferStream->getSize(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function isReadable() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->bufferStream->isReadable(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isWritable() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->bufferStream->isWritable(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function isSeekable() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->bufferStream->isSeekable(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function rewind() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->bufferStream->rewind(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function seek($offset, $whence = SEEK_SET) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->bufferStream->seek($offset, $whence); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function eof() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->bufferStream->eof(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function tell() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->bufferStream->tell(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Reads data from the buffer. |
|
97
|
|
|
* @param mixed $length |
|
98
|
|
|
*/ |
|
99
|
|
|
public function read($length) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->bufferStream->read($length); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Writes data to the buffer. |
|
106
|
|
|
* @param mixed $string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function write($string) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->bufferStream->write($string); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getMetadata($key = null) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->bufferStream->getMetadata($key); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|