Completed
Push — master ( b7052b...e758c6 )
by Luis
05:32 queued 02:56
created

NodeLabelBuilder::removeNewLinesFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
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
class NodeLabelBuilder
18
{
19
    /** @var TemplateEngine */
20
    private $engine;
21
22
    /** @var HtmlLabelStyle */
23
    private $style;
24
25 18
    public function __construct(TemplateEngine $engine, HtmlLabelStyle $style)
26
    {
27 18
        $this->engine = $engine;
28 18
        $this->style = $style;
29 18
    }
30
31 12
    public function labelForClass(ClassDefinition $class): string
32
    {
33 12
        return $this->buildLabel('class.html.twig', [
34 12
            'class' => $class,
35 12
            'style' => $this->style,
36
        ]);
37
    }
38
39 6
    public function labelForInterface(InterfaceDefinition $interface): string
40
    {
41 6
        return $this->buildLabel('interface.html.twig', [
42 6
            'interface' => $interface,
43 6
            'style' => $this->style,
44
        ]);
45
    }
46
47 18
    private function buildLabel(string $template, array $options): string
48
    {
49
        try {
50 18
            return "<{$this->removeNewLinesFrom($this->engine->render($template, $options))}>";
51 3
        } catch (LoaderError | RuntimeError | SyntaxError $e) {
52 3
            throw new NodeLabelError($e);
53
        }
54
    }
55
56 15
    private function removeNewLinesFrom(string $label): string
57
    {
58 15
        return trim(preg_replace('/\s\s+|\n/', '', $label));
59
    }
60
}
61