Passed
Push — master ( 1d0ea2...0f9d68 )
by Kacper
05:34
created

CliFormatter::format()   A

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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Highlighter
7
 *
8
 * Copyright (C) 2016, Some right reserved.
9
 *
10
 * @author Kacper "Kadet" Donat <[email protected]>
11
 *
12
 * Contact with author:
13
 * Xmpp: [email protected]
14
 * E-mail: [email protected]
15
 *
16
 * From Kadet with love.
17
 */
18
19
namespace Kadet\Highlighter\Formatter;
20
21
use Kadet\Highlighter\Parser\Token\Token;
22
use Kadet\Highlighter\Parser\Tokens;
23
use Kadet\Highlighter\Utils\ArrayHelper;
24
use Kadet\Highlighter\Utils\Console;
25
26
/**
27
 * Class CliFormatter
28
 *
29
 * @package Kadet\Highlighter\Formatter
30
 */
31
class CliFormatter extends AbstractFormatter implements FormatterInterface
32
{
33
    private $_styles = [];
34
35 38
    public function __construct(array $options = [])
36
    {
37 38
        parent::__construct(array_replace_recursive([
38 38
            'styles' => include __DIR__ . '/../Styles/Cli/Default.php'
39
        ], $options));
40
41 38
        $this->_styles = $this->_options['styles'];
42 38
    }
43
44 1
    public function format(Tokens $tokens)
45
    {
46 1
        return parent::format($tokens) . Console::reset();
47
    }
48
49 1
    protected function token(Token $token)
50
    {
51 1
        $style = ArrayHelper::resolve($this->_styles, $token->name);
52
53 1
        if ($style === null) {
54
            return null;
55
        }
56
57 1
        return $token->isStart()
58 1
            ? Console::open(is_callable($style) ? $style($token) : $style)
59 1
            : Console::close();
60
    }
61
62
    protected function formatLineStart($line)
63
    {
64
        return str_pad($line, 5, ' ', STR_PAD_LEFT) . ' | ' . Console::set(Console::current());
65
    }
66
67
    protected function formatLineEnd($line)
68
    {
69
        return Console::reset();
70
    }
71
}
72