• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Mermaid-PHP / 7634240466

21 Jan 2024 10:26PM UTC coverage: 89.693%. Remained the same
7634240466

push

github

web-flow
Fixed code style (#18)

1 of 2 new or added lines in 1 file covered. (50.0%)

7 existing lines in 2 files now uncovered.

409 of 456 relevant lines covered (89.69%)

34.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.06
/src/Render.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Mermaid-PHP.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Mermaid-PHP
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\MermaidPHP;
18

19
use JBZoo\MermaidPHP\ClassDiagram\ClassDiagram;
20
use JBZoo\MermaidPHP\ERDiagram\ERDiagram;
21
use JBZoo\MermaidPHP\Timeline\Timeline;
22

23
class Render
24
{
25
    public const THEME_DEFAULT = 'default';
26
    public const THEME_FOREST  = 'forest';
27
    public const THEME_DARK    = 'dark';
28
    public const THEME_NEUTRAL = 'neutral';
29

30
    public const DEFAULT_MERMAID_URL = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
31

32
    public static function html(ClassDiagram|ERDiagram|Graph|Timeline $graph, array $params = []): string
33
    {
34
        $theme     = (string)($params['theme'] ?? self::THEME_FOREST);
136✔
35
        $scriptUrl = (string)($params['mermaid_url'] ?? self::DEFAULT_MERMAID_URL);
136✔
36
        $showZoom  = (bool)($params['show-zoom'] ?? true);
136✔
37
        $isDebug   = (bool)($params['debug'] ?? false);
136✔
38

39
        $title     = (string)($params['title'] ?? '');
136✔
40
        $pageTitle = $title === '' ? $title : 'JBZoo - Mermaid Graph';
136✔
41

42
        /** @see https://mermaid-js.github.io/mermaid/#/mermaidAPI?id=loglevel */
43
        $mermaidParams = \json_encode([
136✔
44
            'startOnLoad' => true,
68✔
45
            'theme'       => $theme,
68✔
46
            'themeCSS'    => \implode(\PHP_EOL, [
136✔
47
                '.edgePath .path:hover {stroke-width:4px; cursor:pointer}',
68✔
48
                '.edgeLabel {border-radius:4px}',
68✔
49
                '.label {font-family:Source Sans Pro,Helvetica Neue,Arial,sans-serif;}',
68✔
50
            ]),
68✔
51
            'maxTextSize'         => 1000000, // almost no size limitation
68✔
52
            'loglevel'            => 'debug',
68✔
53
            'securityLevel'       => 'loose',
68✔
54
            'arrowMarkerAbsolute' => true,
68✔
55
            'flowchart'           => [
68✔
56
                'htmlLabels'     => true,
68✔
57
                'useMaxWidth'    => true,
68✔
58
                'diagramPadding' => 12,
68✔
59
                'curve'          => 'basis',
68✔
60
            ],
68✔
61
        ], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
136✔
62

63
        $debugCode = '';
136✔
64
        if ($isDebug) {
136✔
65
            $graphParams = \json_encode($graph->getParams(), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
136✔
66

67
            $debugCode .= '<hr>';
136✔
68
            $debugCode .= '<pre><code>' . \htmlentities((string)$graph) . '</code></pre>';
136✔
69
            $debugCode .= '<hr>';
136✔
70
            $debugCode .= "<pre><code>Params = {$graphParams}</code></pre>";
136✔
71
        }
72

73
        $html = [
136✔
74
            '<!DOCTYPE html>',
68✔
75
            '<html lang="en">',
68✔
76
            '<head>',
68✔
77
            '    <meta charset="utf-8">',
68✔
78
            "    <title>{$pageTitle}</title>",
136✔
79
            '   <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>',
68✔
80
            '<script type="module">',
68✔
81
            "        import mermaid from '{$scriptUrl}';",
136✔
82
            '</script>',
68✔
83
            '</head>',
68✔
84
            '<body>',
68✔
85

86
            $title !== '' ? "<h1>{$title}</h1><hr>" : '',
136✔
87

88
            '    <div class="mermaid" style="margin-top:20px;">' . $graph . '</div>',
136✔
89

90
            $debugCode,
68✔
91

92
            $showZoom ?
136✔
93
                "<input type=\"button\" class=\"btn btn-primary\" id=\"zoom\" value=\"Zoom In\">
68✔
94
                <script>
95
                     mermaid.initialize({$mermaidParams});
68✔
96
                     $(function () {
97
                        $('#zoom').click(() => {
98
                            $('.mermaid').removeAttr('data-processed');
99
                            $('.mermaid').width($('.mermaid svg').css('max-width'));
100
                        });
101
                     });
102
                </script>"
68✔
103
                : '',
136✔
104
            '<script>
68✔
105
                $(document).on("click", "path", e => {
106
                    e.currentTarget.style.stroke = e.currentTarget.style.stroke ? "" : "red";
107
                });
108
            </script>',
68✔
109
            '</body>',
68✔
110
            '</html>',
68✔
111
        ];
68✔
112

113
        return \implode(\PHP_EOL, $html);
136✔
114
    }
115

116
    public static function escape(string $text): string
117
    {
118
        $text = \trim($text);
×
119
        $text = \htmlentities($text);
×
120

121
        $text = \str_replace(['&', '#lt;', '#gt;'], ['#', '<', '>'], $text);
×
122

123
        return "\"{$text}\"";
×
124
    }
125

126
    public static function getId(string $userFriendlyId): string
127
    {
128
        return \md5($userFriendlyId);
×
129
    }
130

131
    public static function getLiveEditorUrl(ERDiagram|Graph|Timeline $graph): string
132
    {
133
        $json = \json_encode([
×
134
            'code'    => (string)$graph,
×
UNCOV
135
            'mermaid' => ['theme' => 'forest'],
UNCOV
136
        ]);
137

138
        if ($json === false) {
×
139
            throw new \RuntimeException('Can\'t encode JSON');
×
140
        }
141

142
        $params = \base64_encode($json);
×
143

144
        return "https://mermaid-js.github.io/mermaid-live-editor/#/edit/{$params}";
×
145
    }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc