Passed
Branch master (f3f280)
by Maciej
03:18
created

Http::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * Highlighter
5
 *
6
 * Copyright (C) 2016, Some right reserved.
7
 *
8
 * @author Kacper "Kadet" Donat <[email protected]>
9
 *
10
 * Contact with author:
11
 * Xmpp: [email protected]
12
 * E-mail: [email protected]
13
 *
14
 * From Kadet with love.
15
 */
16
17
namespace Kadet\Highlighter\Language;
18
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Parser\Rule;
21
use Kadet\Highlighter\Parser\Token\LanguageToken;
22
use Kadet\Highlighter\Parser\TokenFactory;
23
24
class Http extends GreedyLanguage
25
{
26
    private $_embeddedFactory;
27
28 1
    public function __construct(array $options)
29
    {
30 1
        parent::__construct($options);
31
32 1
        $this->_embeddedFactory = new TokenFactory(LanguageToken::class);
33 1
    }
34
35
    /**
36
     * Tokenization rules setup
37
     */
38 1
    public function setupRules()
39
    {
40 1
        $this->rules->addMany([
41 1
            'number.status'   => new Rule(new RegexMatcher('/^HTTP\/.+\s(\d+)/')),
42 1
            'constant.status' => new Rule(new RegexMatcher('/^HTTP\/.+\s+\d+\s+(.+?)\R/')),
43
44 1
            'call.method' => new Rule(new RegexMatcher('/^(\w+).*HTTP\//')),
45 1
            'string.path' => new Rule(new RegexMatcher('/^\w+\s(.*?)\sHTTP\//')),
46
47 1
            'symbol.version' => new Rule(new RegexMatcher('/^.*(HTTP\/\S+)/')),
48
49 1
            'symbol.header' => new Rule(new RegexMatcher('/^([\w-]+:)/m')),
50
        ]);
51 1
    }
52
53 1
    public function tokenize($source, $additional = [], $offset = 0, $embedded = false)
54
    {
55 1
        $split = preg_split('/\R\R/', $source, 2);
56
57 1
        $http = $split[0];
58 1
        if (isset($split[1]) && $payload = $split[1]) {
59 1
            if (preg_match('/Content-Type: ([^;\r\n]*)/', $http, $matches)) {
60 1
                $mime = $matches[1];
61
            } else {
62
                $mime = 'text/plain';
63
            }
64
65 1
            $injected = self::byMime($mime);
66 1
            $language = $this->_embeddedFactory->create('language.' . $injected->getIdentifier(), [
67 1
                'pos'    => strlen($source) - strlen($payload) + $offset,
68 1
                'length' => strlen($payload),
69 1
                'inject' => $injected,
70 1
                'rule'   => new Rule(null, [
71 1
                    'language' => $this,
72 1
                    'priority' => 900
73
                ])
74
            ]);
75 1
            $language->setValid(true);
76
77 1
            $additional = array_merge(
78 1
                $additional,
79 1
                $injected->tokenize($payload, [], $language->pos, Language::EMBEDDED_BY_PARENT)->getArrayCopy(),
80 1
                [$language, $language->getEnd()]
81
            );
82
        }
83
84 1
        return parent::tokenize($source, $additional, $offset, $embedded);
85
    }
86
87
88
    /**
89
     * Unique language identifier, for example 'php'
90
     *
91
     * @return string
92
     */
93 1
    public function getIdentifier()
94
    {
95 1
        return 'http';
96
    }
97
98
    public static function getMetadata()
99
    {
100
        return [
101
            'name'      => ['http'],
102
        ];
103
    }
104
}
105