Completed
Push — master ( edcce7...6aac40 )
by Kacper
04:23
created

CliFormatter::token()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

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