Completed
Push — master ( a293a3...292fca )
by X
02:23
created

Source::peek()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Input for parser
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser Input
9
 */
10
class Source
11
{
12
    /** @var string $str given string to deserialize */
13
    private $str;
14
15
    /** @var int $length given string length */
16
    private $length;
17
18
    /** @var int $current current position of parser */
19
    private $current;
20
21
    /**
22
     * constructor
23
     *
24
     * @param string $str parser input
25
     * @throws \InvalidArgumentException
26
     */
27 110
    public function __construct($str)
28
    {
29 110
        if (!is_string($str)) {
30 1
            throw new \InvalidArgumentException('expected string, but got: ' . gettype($str));
31
        }
32 109
        $this->str = $str;
33 109
        $this->length = strlen($str);
34 109
        $this->current = 0;
35 109
    }
36
37
    /**
38
     * throw error with currnt position
39
     *
40
     * @return void
41
     * @throws UnserializeFailedException
42
     */
43 67
    public function triggerError()
44
    {
45 67
        $bytes = strlen($this->str);
46 67
        throw new UnserializeFailedException("unserialize(): Error at offset {$this->current} of {$bytes} bytes");
47
    }
48
49
    /**
50
     * consume given string if it is as expected
51
     *
52
     * @param string  $expected expected string
53
     * @param integer $length   length of $expected
54
     * @return void
55
     * @throws UnserializeFailedException
56
     */
57 26
    public function consume($expected, $length)
58
    {
59 26
        if (strpos($this->str, $expected, $this->current) !== $this->current) {
60 5
            return $this->triggerError();
61
        }
62 21
        $this->current += $length;
63 21
    }
64
65
    /**
66
     * read givin length substring
67
     *
68
     * @param integer $length length to read
69
     * @return string
70
     * @throws UnserializeFailedException
71
     */
72 25
    public function read($length)
73
    {
74 25
        if ($length < 0) {
75 1
            return $this->triggerError();
76
        }
77 24
        if ($this->current + $length > $this->length) {
78 1
            return $this->triggerError();
79
        }
80
81 23
        $this->current += $length;
82 23
        return substr($this->str, $this->current - $length, $length);
83
    }
84
85
    /**
86
     * return matching string for given regexp
87
     *
88
     * @param string $regexp Regular Expression for expected substring
89
     * @return array
90
     */
91 109
    public function match($regexp)
92
    {
93 109
        if (!preg_match($regexp, $this->str, $matches, 0, $this->current)) {
94 62
            return $this->triggerError();
95
        }
96
97 52
        $this->current += strlen($matches[0]);
98 52
        array_shift($matches);
99 52
        return $matches;
100
    }
101
}
102