FigletManager::getKnownFonts()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
namespace Matks\Vivian\Figlet;
4
5
use Packaged\Figlet\Figlet as FigletEngine;
6
use Exception;
7
8
/**
9
 * Figlet manager
10
 *
11
 * @link http://www.figlet.org/
12
 */
13
class FigletManager
14
{
15
    const FIGLET_FUNCTION_NAME_REGEX = '^(figlet)_([a-zA-Z]*)$';
16
17
    /**
18
     * Static calls interface
19
     */
20
    public static function __callstatic($name, $params)
21
    {
22
        $matches = static::isFigletCall($name);
23
24
        if (null === $matches) {
25
            throw new Exception("Unknown figlet function name $name");
26
        }
27
28
        $fontName = $matches[2];
29
30
        // target string is expected to be:
31
        $targetString = $params[0][0];
32
33
        return static::render($targetString, $fontName);
34
    }
35
36
    /**
37
     * Compute text banner from given string
38
     *
39
     * @param string $string
40
     * @param string $fontName
41
     *
42
     * @return string
43
     */
44
    public static function render($string, $fontName)
45
    {
46
        if (!in_array($fontName, static::getKnownFonts())) {
47
            throw new Exception("Error unknown font ID fontID");
48
        }
49
50
        $figletString = FigletEngine::create($string, $fontName);
51
52
        return $figletString;
53
    }
54
55
    /**
56
     * Analyse function name
57
     *
58
     * @param string $functionName
59
     *
60
     * @return array|bool
61
     */
62
    public static function isFigletCall($functionName)
63
    {
64
        $matches = array();
65
        $pattern = '#' . static::FIGLET_FUNCTION_NAME_REGEX . '#';
66
67
        if (!preg_match($pattern, $functionName, $matches)) {
68
            return false;
69
        }
70
71
        return $matches;
72
    }
73
74
    /**
75
     * Get provided fonts
76
     *
77
     * The fonts provided are the one packaged in vendor packaged/figlet
78
     *
79
     * @return string[]
80
     */
81
    public static function getKnownFonts()
82
    {
83
        $fontNames = array(
84
            'banner',
85
            'big',
86
            'block',
87
            'bubble',
88
            'digital',
89
            'ivrit',
90
            'lean',
91
            'mini',
92
            'script',
93
            'shadow',
94
            'slant',
95
            'small',
96
            'smscript',
97
            'smshadow',
98
            'smslant',
99
            'speed',
100
            'standard',
101
            'term'
102
        );
103
104
        return $fontNames;
105
    }
106
}
107