Completed
Push — master ( 48b451...90ac5a )
by Amine
47s queued 43s
created

OutTransformer::alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
        'color' => '38;5;$2m',
25
        'background' => '48;5;$2m',
26
        'reset' => '0m',
27
        'bold'  => '1m',
28
        'underline' => '4m'
29
    ];
30
31
    protected $aliases = [];
32
33
    public function alias(string $name, string $value) {
34
        $this->aliases[$name] = $value;
35
        return $this;
36
    }
37
38
    public function transform(string $text) : string
39
    {
40
        foreach ($this->aliases as $name => $value) {
41
            $text = str_replace($name, $value, $text);
42
        }
43
44
        foreach (self::CONTROLS as $name => $value) {
45
            $text = preg_replace("/<{$name}(:([^>]*))?>/", self::CSI . $value, $text);
46
        }
47
48
        return $text;
49
    }
50
}
51