Text::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Terminal;
4
5
use InvalidArgumentException;
6
use Vanderlee\Comprehend\Core\Context;
7
use Vanderlee\Comprehend\Parser\Parser;
8
9
/**
10
 * Description of TextParser.
11
 *
12
 * @author Martijn
13
 */
14
class Text extends Parser
15
{
16
    use CaseSensitiveTrait;
17
18
    private $text;
19
    private $length;
20
21 51
    public function __construct($text)
22
    {
23 51
        $this->text = $text;
24
25 51
        $this->length = mb_strlen($text);
26 51
        if ($this->length <= 0) {
27 1
            throw new InvalidArgumentException('Empty argument');
28
        }
29 50
    }
30
31 128
    protected function parse(&$input, $offset, Context $context)
32
    {
33 128
        $this->pushCaseSensitivityToContext($context);
34
35 128
        $text = $context->handleCase($this->text);
36 128
        for ($c = 0; $c < $this->length; $c++) {
37 128
            if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) {
38 42
                $this->popCaseSensitivityFromContext($context);
39
40 42
                return $this->failure($input, $offset, $c);
41
            }
42
        }
43
44 103
        $this->popCaseSensitivityFromContext($context);
45
46 103
        return $this->success($input, $offset, $this->length);
47
    }
48
49 99
    public function __toString()
50
    {
51 99
        return '"' . $this->text . '"';
52
    }
53
}
54