Completed
Push — master ( 0fba96...2d7b41 )
by Sergey
02:08
created

StringStream::getString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date: 3/18/16
5
 * @time: 1:15 PM
6
 */
7
8
namespace LTDBeget\stringstream;
9
10
11
use LTDBeget\ascii\AsciiChar;
12
13
/**
14
 * String stream data structure
15
 * Class StringStream
16
 * @package LTDBeget\stringstream
17
 */
18
class StringStream
19
{
20
    /**
21
     * StringStream constructor.
22
     * @param string $string
23
     */
24
    public function __construct(string $string)
25
    {
26
        if(empty($string)) {
27
            throw new \BadMethodCallException("Cannot make stream from empty string");
28
        }
29
30
        $this->originalString = $string;
31
        $this->stream = unpack('C*', $string);
32
        $this->length = count($this->stream);
33
34
        $this->pointerAtStart = true;
35
        $this->pointerAtEnd = false;
36
    }
37
38
    /**
39
     * Original string data of stream
40
     * @return string
41
     */
42
    public function getString()
43
    {
44
        return $this->originalString;
45
    }
46
47
    /**
48
     * Length of string
49
     * @return int
50
     */
51
    public function length()
52
    {
53
        return $this->length;
54
    }
55
56
    /**
57
     * Current char of stream
58
     * @return string
59
     */
60
    public function current() : string
61
    {
62
        return chr(current($this->stream));
63
    }
64
65
    /**
66
     * Current char of stream as AsciiChar
67
     * @return AsciiChar
68
     */
69
    public function currentAscii() : AsciiChar
70
    {
71
        return AsciiChar::get(current($this->stream));
72
    }
73
74
    /**
75
     * Position in stream of current char
76
     * @return int
77
     */
78
    public function position() : int
79
    {
80
        return key($this->stream);
81
    }
82
83
    /**
84
     * go to next char in stream
85
     */
86
    public function next()
87
    {
88
        $this->pointerAtStart = false;
89
        $this->pointerAtEnd = next($this->stream) === false;
90
    }
91
92
    /**
93
     * go to previous char of stream
94
     */
95
    public function previous()
96
    {
97
        $this->pointerAtEnd = false;
98
        $this->pointerAtStart = prev($this->stream) === false;
99
    }
100
101
    /**
102
     * go to start of stream
103
     */
104
    public function start()
105
    {
106
        reset($this->stream);
107
    }
108
109
    /**
110
     * is start of stream
111
     * @return bool
112
     */
113
    public function isStart() : bool
114
    {
115
        return $this->pointerAtStart;
116
    }
117
118
    /**
119
     * go to end of stream
120
     */
121
    public function end()
122
    {
123
        end($this->stream);
124
    }
125
126
    /**
127
     * is end of stream
128
     * @return bool
129
     */
130
    public function isEnd() : bool
131
    {
132
        return $this->pointerAtEnd;
133
    }
134
135
    /**
136
     * ignore chars in stream while it is white space
137
     */
138
    public function ignoreWhitespace()
139
    {
140
        ignoreWhitespace:
141
        if (! $this->isEnd() && $this->currentAscii()->isWhiteSpace()) {
1 ignored issue
show
Bug introduced by
The method isWhiteSpace() does not seem to exist on object<LTDBeget\ascii\AsciiChar>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
            $this->next();
143
            goto ignoreWhitespace;
144
        }
145
    }
146
147
    /**
148
     * ignore chars in stream while it is horizontal space
149
     */
150
    public function ignoreHorizontalSpace()
151
    {
152
        ignoreHorizontalSpace:
153
        if (!$this->isEnd() && $this->currentAscii()->isHorizontalSpace()) {
1 ignored issue
show
Bug introduced by
The method isHorizontalSpace() does not seem to exist on object<LTDBeget\ascii\AsciiChar>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
            $this->next();
155
            goto ignoreHorizontalSpace;
156
        }
157
    }
158
159
    /**
160
     * ignore chars in stream while it is vertical space
161
     */
162
    public function ignoreVerticalSpace()
163
    {
164
        ignoreHorizontalSpace:
165
        if (! $this->isEnd() && $this->currentAscii()->isVerticalSpace()) {
1 ignored issue
show
Bug introduced by
The method isVerticalSpace() does not seem to exist on object<LTDBeget\ascii\AsciiChar>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
            $this->next();
167
            goto ignoreHorizontalSpace;
168
        }
169
    }
170
171
    /**
172
     * @var string
173
     */
174
    private $originalString;
175
176
    /**
177
     * @var array
178
     */
179
    private $stream;
180
181
    /**
182
     * @var int
183
     */
184
    private $length;
185
186
    /**
187
     * if next returns false it member will be true, else false
188
     * @var bool
189
     */
190
    private $pointerAtEnd;
191
192
    /**
193
     * if prev returns false it member will be true, else false
194
     * @var bool
195
     */
196
    private $pointerAtStart;
197
}