Completed
Push — master ( 9b98d4...f3f280 )
by Maciej
06:39
created

CliFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 42
ccs 14
cts 19
cp 0.7368
rs 10
c 3
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 3 1
A __construct() 0 7 1
A token() 0 11 4
A formatLineStart() 0 3 1
A formatLineEnd() 0 3 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\Formatter;
18
19
use Kadet\Highlighter\Parser\Token\Token;
20
use Kadet\Highlighter\Parser\Tokens;
21
use Kadet\Highlighter\Utils\ArrayHelper;
22
use Kadet\Highlighter\Utils\Console;
23
24
/**
25
 * Class CliFormatter
26
 *
27
 * @package Kadet\Highlighter\Formatter
28
 */
29
class CliFormatter extends AbstractFormatter implements FormatterInterface
30
{
31
    private $_styles = [];
32
33
    /**
34
     * CliFormatter constructor.
35
     */
36 37
    public function __construct(array $options = [])
37
    {
38 37
        parent::__construct(array_replace_recursive([
39 37
            'styles' => include __DIR__ . '/../Styles/Cli/Default.php'
40 37
        ], $options));
41
42 37
        $this->_styles = $this->_options['styles'];
43 37
    }
44
45 2
    public function format(Tokens $tokens)
46
    {
47 2
        return parent::format($tokens) . Console::reset();
48
    }
49
50 2
    protected function token(Token $token)
51
    {
52 2
        $style = ArrayHelper::resolve($this->_styles, $token->name);
53
54 2
        if ($style === null) {
55
            return null;
56
        }
57
58 2
        return $token->isStart()
59 2
            ? Console::open(is_callable($style) ? $style($token) : $style)
60 2
            : Console::close();
61
    }
62
63
    protected function formatLineStart($line)
64
    {
65
        return str_pad($line, 5, ' ', STR_PAD_LEFT) . ' | ' . Console::set(Console::current());
66
    }
67
68
    protected function formatLineEnd($line)
69
    {
70
        return Console::reset();
71
    }
72
}
73