Completed
Push — master ( b54cbd...d09870 )
by Luis
18:59
created

NodeLabelBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A removeNewLinesFrom() 0 3 1
A forClass() 0 5 1
A forInterface() 0 5 1
A buildLabel() 0 6 2
A __construct() 0 4 1
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Graphviz\Builders;
9
10
use PhUml\Code\ClassDefinition;
11
use PhUml\Code\InterfaceDefinition;
12
use Twig_Environment as TemplateEngine;
13
use Twig_Error_Loader as LoaderError;
14
use Twig_Error_Runtime as RuntimeError;
15
use Twig_Error_Syntax as SyntaxError;
16
17
/**
18
 * It creates an HTML table out of either a class or an interface.
19
 *
20
 * The table is used as a label for the nodes in the digraph use to create the class diagram
21
 */
22
class NodeLabelBuilder
23
{
24
    /** @var TemplateEngine */
25
    private $engine;
26
27
    /** @var HtmlLabelStyle */
28
    private $style;
29
30
    public function __construct(TemplateEngine $engine, HtmlLabelStyle $style)
31
    {
32
        $this->engine = $engine;
33
        $this->style = $style;
34
    }
35
36
    public function forClass(ClassDefinition $class): string
37
    {
38
        return $this->buildLabel('class.html.twig', [
39
            'class' => $class,
40
            'style' => $this->style,
41
        ]);
42
    }
43
44
    public function forInterface(InterfaceDefinition $interface): string
45
    {
46
        return $this->buildLabel('interface.html.twig', [
47
            'interface' => $interface,
48
            'style' => $this->style,
49
        ]);
50
    }
51
52
    private function buildLabel(string $template, array $options): string
53
    {
54
        try {
55
            return "<{$this->removeNewLinesFrom($this->engine->render($template, $options))}>";
56
        } catch (LoaderError | RuntimeError | SyntaxError $e) {
57
            throw new NodeLabelError($e);
58
        }
59
    }
60
61
    private function removeNewLinesFrom(string $label): string
62
    {
63
        return trim(preg_replace('/\s\s+|\n/', '', $label));
64
    }
65
}
66