StyleManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 4
c 4
b 2
f 0
lcom 0
cbo 2
dl 0
loc 66
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __callstatic() 0 15 1
A style() 0 10 2
A getKnownStyles() 0 11 1
1
<?php
2
3
namespace Matks\Vivian\Style;
4
5
use Matks\Vivian\Output\TextElement;
6
use Exception;
7
8
/**
9
 * Style manager
10
 *
11
 * @link http://ascii-table.com/ansi-escape-sequences-vt-100.php
12
 */
13
class StyleManager
14
{
15
    const BOLD      = 'bold';
16
    const UNDERLINE = 'underline';
17
    const BLINK     = 'blink';
18
    const INVISIBLE = 'invisible';
19
20
    const BASH_BOLD      = 1;
21
    const BASH_UNDERLINE = 4;
22
    const BASH_BLINK     = 5;
23
    const BASH_INVISIBLE = 8;
24
25
    /**
26
     * Static calls interface
27
     */
28
    public static function __callstatic($name, $params)
29
    {
30 1
        $knownStyles = static::getKnownStyles();
31 1
        $styleID     = $knownStyles[$name];
32
33 1
        $style = static::style($styleID);
34
35
        // target string is expected to be:
36 1
        $targetString = $params[0][0];
37
38 1
        $element = new TextElement($targetString);
39 1
        $element->addStyle($style);
40
41 1
        return $element->render();
42
    }
43
44
    /**
45
     * Format given string in chosen style
46
     *
47
     * @param int $styleID
48
     *
49
     * @return Style
50
     */
51
    public static function style($styleID)
52
    {
53 1
        if (!in_array($styleID, static::getKnownStyles())) {
54 1
            throw new Exception("Unknown style ID $styleID");
55
        }
56
57 1
        $style = new Style($styleID);
58
59 1
        return $style;
60
    }
61
62
    /**
63
     * Get allowed styles
64
     *
65
     * @return string[]
66
     */
67
    public static function getKnownStyles()
68
    {
69
        $styles = array(
70 1
            static::BOLD      => static::BASH_BOLD,
71 1
            static::UNDERLINE => static::BASH_UNDERLINE,
72 1
            static::BLINK     => static::BASH_BLINK,
73 1
            static::INVISIBLE => static::BASH_INVISIBLE,
74 1
        );
75
76 1
        return $styles;
77
    }
78
}
79