OutTransformer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 11 3
A alias() 0 3 1
1
<?php namespace Tarsana\Command\Console;
2
3
use Tarsana\Command\Interfaces\Console\TransformerInterface;
4
5
class OutTransformer implements TransformerInterface {
6
7
    const CSI = "\033[";
8
9
    const CONTROLS = [
10
        'up'    => '$2A',
11
        'down'  => '$2B',
12
        'right' => '$2C',
13
        'left'  => '$2D',
14
        'nextLine' => '$2E',
15
        'prevLine' => '$2F',
16
        'column'   => '$2G',
17
18
        'clearBefore' => '1J',
19
        'clearAfter'  => 'J',
20
        'clearLine'   => '2K',
21
        'clearAll'    => '3J',
22
        'clear'       => '2J',
23
24
        'save' => 's',
25
        'load' => 'u',
26
27
        'color' => '38;5;$2m',
28
        'background' => '48;5;$2m',
29
        'reset' => '0m',
30
        'bold'  => '1m',
31
        'underline' => '4m'
32
    ];
33
34
    protected $aliases = [];
35
36
    public function alias(string $name, string $value) {
37
        $this->aliases[$name] = $value;
38
        return $this;
39
    }
40
41
    public function transform(string $text) : string
42
    {
43
        foreach ($this->aliases as $name => $value) {
44
            $text = str_replace($name, $value, $text);
45
        }
46
47
        foreach (self::CONTROLS as $name => $value) {
48
            $text = preg_replace("/<{$name}(:([^>]*))?>/", self::CSI . $value, $text);
49
        }
50
51
        return $text;
52
    }
53
}
54