|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RouterOS\Streams; |
|
4
|
|
|
|
|
5
|
|
|
use RouterOS\Interfaces\StreamInterface; |
|
6
|
|
|
use RouterOS\Exceptions\StreamException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* class StringStream |
|
10
|
|
|
* |
|
11
|
|
|
* Initialized with a string, the read method retreive it as done with fread, consuming the buffer. |
|
12
|
|
|
* When all the string has been read, exception is thrown when try to read again. |
|
13
|
|
|
* |
|
14
|
|
|
* @package RouterOS\Streams |
|
15
|
|
|
* @since 0.9 |
|
16
|
|
|
*/ |
|
17
|
|
|
class StringStream implements StreamInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string $buffer Stores the string to use |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $buffer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* StringStream constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $string |
|
28
|
|
|
*/ |
|
29
|
7 |
|
public function __construct(string $string) |
|
30
|
|
|
{ |
|
31
|
7 |
|
$this->buffer = $string; |
|
32
|
7 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritDoc} |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \InvalidArgumentException when length parameter is invalid |
|
38
|
|
|
* @throws StreamException when the stream have been tatly red and read methd is called again |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function read(int $length): string |
|
41
|
|
|
{ |
|
42
|
6 |
|
$remaining = strlen($this->buffer); |
|
43
|
|
|
|
|
44
|
6 |
|
if ($length < 0) { |
|
45
|
1 |
|
throw new \InvalidArgumentException('Cannot read a negative count of bytes from a stream'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
if (0 === $remaining) { |
|
49
|
4 |
|
throw new StreamException('End of stream'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
if ($length >= $remaining) { |
|
53
|
|
|
// returns all |
|
54
|
1 |
|
$result = $this->buffer; |
|
55
|
|
|
// No more in the buffer |
|
56
|
1 |
|
$this->buffer = ''; |
|
57
|
|
|
} else { |
|
58
|
|
|
// acquire $length characters from the buffer |
|
59
|
1 |
|
$result = substr($this->buffer, 0, $length); |
|
60
|
|
|
// remove $length characters from the buffer |
|
61
|
1 |
|
$this->buffer = substr_replace($this->buffer, '', 0, $length); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Fake write method, do nothing except return the "writen" length |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $string The string to write |
|
71
|
|
|
* @param int|null $length the number of characters to write |
|
72
|
|
|
* @return int number of "writen" bytes |
|
73
|
|
|
* @throws \InvalidArgumentException on invalid length |
|
74
|
|
|
*/ |
|
75
|
13 |
|
public function write(string $string, int $length = null): int |
|
76
|
|
|
{ |
|
77
|
13 |
|
if (null === $length) { |
|
78
|
3 |
|
$length = strlen($string); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
13 |
|
if ($length < 0) { |
|
82
|
1 |
|
throw new \InvalidArgumentException('Cannot write a negative count of bytes'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
12 |
|
return min($length, strlen($string)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Close stream connection |
|
90
|
|
|
* |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function close() |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->buffer = ''; |
|
96
|
1 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|