Completed
Push — master ( 9667e2...b50d22 )
by X
24s
created

NumberStringParser::isSign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * parser for number
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser for number, return result as string
9
 *
10
 * <number> = ["-" / "+"] 1*DIGIT
11
 * note that leading zeros are valid (e.g. "0089" is valid)
12
 */
13
class NumberStringParser implements ParserInterface
14
{
15
    /**
16
     * parse given `$source` as number
17
     *
18
     * @param Source $source parser input
19
     * @return string
20
     */
21 26
    public function parse(Source $source)
22
    {
23 26
        $parser = new OptionalSignParser();
24 26
        list($result, $source) = $parser->parse($source);
25
26 26
        $hasDigit = false;
27 26
        while (ctype_digit($source->peek())) {
28 23
            $hasDigit = true;
29 23
            $result .= $source->peek();
30 23
            $source->next();
31 23
        }
32 26
        if (!$hasDigit) {
33 3
            return $source->triggerError();
34
        }
35
36 23
        return [$result, $source];
37
    }
38
}
39