Completed
Push — master ( 2bc843...e59b65 )
by Sergey
03:29
created

StringStream::ord()   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 Hoa\Ustring\Ustring;
12
use LTDBeget\ascii\AsciiChar;
13
14
/**
15
 * String stream data structure
16
 * Class StringStream
17
 * @package LTDBeget\stringstream
18
 */
19
class StringStream
20
{
21
    /**
22
     * StringStream constructor.
23
     * @param string $string
24
     * @throws \BadMethodCallException
25
     * @throws \Hoa\Ustring\Exception
26
     */
27
    public function __construct(string $string)
28
    {
29
        if(empty($string)) {
30
            throw new \BadMethodCallException('Cannot make stream from empty string');
31
        }
32
        
33
        $this->stream = (new Ustring($string))->getIterator();
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \Hoa\Ustring\Ustrin...string))->getIterator() of type object<ArrayIterator> is incompatible with the declared type array of property $stream.

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..

Loading history...
34
        $this->pointerAtStart = true;
35
        $this->pointerAtEnd = false;
36
    }
37
38
    /**
39
     * Current char of stream
40
     * @return string
41
     */
42
    public function current() : string
43
    {
44
        return current($this->stream);
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function ord() : int 
51
    {
52
        return ord($this->current());
53
    }
54
55
    /**
56
     * Current char of stream as AsciiChar
57
     * @return AsciiChar
58
     * @throws \LogicException
59
     * @throws \InvalidArgumentException
60
     */
61
    public function currentAscii() : AsciiChar
62
    {
63
        return AsciiChar::get($this->ord());
64
    }
65
66
    /**
67
     * Position in stream of current char
68
     * @return int
69
     */
70
    public function position() : int
71
    {
72
        return key($this->stream);
73
    }
74
75
    /**
76
     * go to next char in stream
77
     */
78
    public function next()
79
    {
80
        $this->pointerAtStart = false;
81
        $this->pointerAtEnd = next($this->stream) === false;
82
    }
83
84
    /**
85
     * go to previous char of stream
86
     */
87
    public function previous()
88
    {
89
        $this->pointerAtEnd = false;
90
        $this->pointerAtStart = prev($this->stream) === false;
91
    }
92
93
    /**
94
     * go to start of stream
95
     */
96
    public function start()
97
    {
98
        reset($this->stream);
99
    }
100
101
    /**
102
     * is start of stream
103
     * @return bool
104
     */
105
    public function isStart() : bool
106
    {
107
        return $this->pointerAtStart;
108
    }
109
110
    /**
111
     * go to end of stream
112
     */
113
    public function end()
114
    {
115
        end($this->stream);
116
    }
117
118
    /**
119
     * is end of stream
120
     * @return bool
121
     */
122
    public function isEnd() : bool
123
    {
124
        return $this->pointerAtEnd;
125
    }
126
127
    /**
128
     * ignore chars in stream while it is white space
129
     * @throws \InvalidArgumentException
130
     * @throws \LogicException
131
     */
132
    public function ignoreWhitespace()
133
    {
134
        ignoreWhitespace:
135
        if (! $this->isEnd() && $this->currentAscii()->isWhiteSpace()) {
136
            $this->next();
137
            goto ignoreWhitespace;
138
        }
139
    }
140
141
    /**
142
     * ignore chars in stream while it is horizontal space
143
     * @throws \LogicException
144
     * @throws \InvalidArgumentException
145
     */
146
    public function ignoreHorizontalSpace()
147
    {
148
        ignoreHorizontalSpace:
149
        if (!$this->isEnd() && $this->currentAscii()->isHorizontalSpace()) {
150
            $this->next();
151
            goto ignoreHorizontalSpace;
152
        }
153
    }
154
155
    /**
156
     * ignore chars in stream while it is vertical space
157
     * @throws \InvalidArgumentException
158
     * @throws \LogicException
159
     */
160
    public function ignoreVerticalSpace()
161
    {
162
        ignoreHorizontalSpace:
163
        if (! $this->isEnd() && $this->currentAscii()->isVerticalSpace()) {
164
            $this->next();
165
            goto ignoreHorizontalSpace;
166
        }
167
    }
168
169
    /**
170
     * @var array
171
     */
172
    private $stream;
173
174
    /**
175
     * if next returns false it member will be true, else false
176
     * @var bool
177
     */
178
    private $pointerAtEnd;
179
180
    /**
181
     * if prev returns false it member will be true, else false
182
     * @var bool
183
     */
184
    private $pointerAtStart;
185
}