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

JBZoo / Mermaid-PHP / 6395852076

26 Sep 2023 07:15PM UTC coverage: 90.214%. Remained the same
6395852076

push

github

web-flow
Add Timeline Diagram (#15)

48 of 48 new or added lines in 3 files covered. (100.0%)

295 of 327 relevant lines covered (90.21%)

45.3 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\ERDiagram\ERDiagram;
20
use JBZoo\MermaidPHP\Timeline\Timeline;
21

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

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

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

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

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

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

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

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

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

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

89
            $debugCode,
68✔
90

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

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

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

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

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

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

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

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

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

143
        return "https://mermaid-js.github.io/mermaid-live-editor/#/edit/{$params}";
×
144
    }
145
}
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

© 2026 Coveralls, Inc