Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

Text::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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