|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\IO; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* StringInputStream |
|
6
|
|
|
* @author Ryuichi TANAKA. |
|
7
|
|
|
* @since 2016/02/07 |
|
8
|
|
|
* @version 0.7 |
|
9
|
|
|
*/ |
|
10
|
|
|
class StringInputStream extends InputStream |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var int 文字列長 |
|
14
|
|
|
*/ |
|
15
|
|
|
private $length; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* construct |
|
19
|
|
|
* @param string $str 文字列 |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(string $str) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->cursorPosition = 0; |
|
24
|
|
|
$this->markedPosition = 0; |
|
25
|
|
|
|
|
26
|
|
|
// 文字列をストリームとして扱う |
|
27
|
|
|
$this->stream = $str; |
|
|
|
|
|
|
28
|
|
|
$this->length = count($str); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* 入力ストリームを閉じる |
|
33
|
|
|
*/ |
|
34
|
|
|
public function close() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->stream = null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 入力ストリームからデータを読み込む |
|
41
|
|
|
* 引数に数値を指定した場合、指定数値バイトだけ読み込む |
|
42
|
|
|
* @param int length 読み込みバイト数 |
|
43
|
|
|
* @return string 読み込みデータ |
|
44
|
|
|
*/ |
|
45
|
|
|
public function read($length = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->eof()) { |
|
48
|
|
|
return -1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$out = ""; |
|
|
|
|
|
|
52
|
|
|
if ($length === null) { |
|
53
|
|
|
$endPosition = $this->length > $this->cursorPosition ? |
|
54
|
|
|
$this->cursorPosition : $this->length; |
|
55
|
|
|
|
|
56
|
|
|
$out = substr($this->stream, $endPosition); |
|
57
|
|
|
} else { |
|
58
|
|
|
// $lengthがファイル終端を越えないようにする |
|
59
|
|
|
if ($this->length > $this->cursorPosition + $length) { |
|
60
|
|
|
$length = $this->length - $this->cursorPosition; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$out = substr($this->stream, $this->cursorPosition, $length); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $out; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function skip(int $pos) |
|
73
|
|
|
{ |
|
74
|
|
|
// 文字列長より後方を指定した場合、-1を返す |
|
75
|
|
|
if ($this->cursorPosition + $pos > $this->length - 1) { |
|
76
|
|
|
return -1; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$start = $this->cursorPosition; |
|
80
|
|
|
$this->cursorPosition = $pos; |
|
81
|
|
|
|
|
82
|
|
|
$skipNum = 0; |
|
|
|
|
|
|
83
|
|
View Code Duplication |
if ($start > $this->cursorPosition) { |
|
|
|
|
|
|
84
|
|
|
// 前方へ移動 |
|
85
|
|
|
$skipNum = $start - $this->cursorPosition; |
|
86
|
|
|
} else { |
|
87
|
|
|
// 後方へ移動 |
|
88
|
|
|
$skipNum = $this->cursorPosition - $start; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $skipNum; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* 入力ストリームの現在位置にmarkを設定する |
|
96
|
|
|
*/ |
|
97
|
|
|
public function mark() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->markedPosition = $this->cursorPosition; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* 最後にmarkされた位置に再配置する |
|
104
|
|
|
*/ |
|
105
|
|
|
public function reset() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->markedPosition = 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function eof() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->cursorPosition >= $this->length - 1; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isMarkSupported() |
|
119
|
|
|
{ |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..