diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 760e0ca..1196377 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,8 +20,8 @@ on: push: branches: - 'master' - schedule: - - cron: '20 */8 * * *' +# schedule: +# - cron: '20 */8 * * *' env: COLUMNS: 120 diff --git a/README.md b/README.md index a276ec4..53ebe19 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # JBZoo / Mermaid-PHP -[![Build Status](https://travis-ci.org/JBZoo/Mermaid-PHP.svg?branch=master)](https://travis-ci.org/JBZoo/Mermaid-PHP) [![Coverage Status](https://coveralls.io/repos/JBZoo/Mermaid-PHP/badge.svg)](https://coveralls.io/github/JBZoo/Mermaid-PHP) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Mermaid-PHP/coverage.svg)](https://shepherd.dev/github/JBZoo/Mermaid-PHP) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jbzoo/mermaid-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jbzoo/mermaid-php/?branch=master) [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/mermaid-php/badge)](https://www.codefactor.io/repository/github/jbzoo/mermaid-php/issues) [![PHP Strict Types](https://img.shields.io/badge/strict__types-%3D1-brightgreen)](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict) +[![Build Status](https://travis-ci.org/JBZoo/Mermaid-PHP.svg?branch=master)](https://travis-ci.org/JBZoo/Mermaid-PHP) [![Coverage Status](https://coveralls.io/repos/JBZoo/Mermaid-PHP/badge.svg)](https://coveralls.io/github/JBZoo/Mermaid-PHP) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Mermaid-PHP/coverage.svg)](https://shepherd.dev/github/JBZoo/Mermaid-PHP) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jbzoo/mermaid-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jbzoo/mermaid-php/?branch=master) [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/mermaid-php/badge)](https://www.codefactor.io/repository/github/jbzoo/mermaid-php/issues) [![PHP Strict Types](https://img.shields.io/badge/strict__types-%3D1-brightgreen)](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict) [![Stable Version](https://poser.pugx.org/jbzoo/mermaid-php/version)](https://packagist.org/packages/jbzoo/mermaid-php) [![Latest Unstable Version](https://poser.pugx.org/jbzoo/mermaid-php/v/unstable)](https://packagist.org/packages/jbzoo/mermaid-php) [![Dependents](https://poser.pugx.org/jbzoo/mermaid-php/dependents)](https://packagist.org/packages/jbzoo/mermaid-php/dependents?order_by=downloads) [![GitHub Issues](https://img.shields.io/github/issues/jbzoo/mermaid-php)](https://github.com/JBZoo/Mermaid-PHP/issues) [![Total Downloads](https://poser.pugx.org/jbzoo/mermaid-php/downloads)](https://packagist.org/packages/jbzoo/mermaid-php/stats) [![GitHub License](https://img.shields.io/github/license/jbzoo/mermaid-php)](https://github.com/JBZoo/Mermaid-PHP/blob/master/LICENSE) @@ -28,11 +28,11 @@ $subGraph1 ->addNode($nodeB = new Node('B', 'Round edge', Node::ROUND)) ->addNode($nodeA = new Node('A', 'Hard edge', Node::SQUARE)) ->addNode($nodeC = new Node('C', 'Decision', Node::CIRCLE)) - ->addNode($nodeD = new Node('D', 'Result one', Node::SQUARE)) + ->addNode($nodeD = new Node('D', 'Result one', Node::SQUARE, "https://mermaid-js.github.io/")) ->addLink(new Link($nodeE, $nodeD)) ->addLink(new Link($nodeB, $nodeC)) ->addLink(new Link($nodeC, $nodeD, 'A double quote:"')) - ->addLink(new Link($nodeC, $nodeE, 'A dec char:♥')) + ->addLink(new Link($nodeC, $nodeE, 'A dec char:♥', Link::ARROW, 'stroke:blue,stroke-width:4px')) ->addLink(new Link($nodeA, $nodeB, ' Link text
/\\!@#$%^&*()_+><\' " ')); $subGraph2 @@ -48,7 +48,7 @@ $htmlCode = $graph->renderHtml([ 'show-zoom' => true ]); // Get result as HTML code for debugging -echo $graph->getLiveEditorUrl(); // Get link to live editor +echo $graph->getLiveEditorUrl(); // Get link to live editor ``` ### Result @@ -73,6 +73,7 @@ graph TB; alone-->C; end linkStyle default interpolate basis; +linkStyle 3 stroke:blue,stroke-width:4px; ``` @@ -100,6 +101,6 @@ MIT - [Composer-Graph](https://github.com/JBZoo/Composer-Graph) - Dependency graph visualization of composer.json based on mermaid-js. - [Utils](https://github.com/JBZoo/Utils) - Collection of useful PHP functions, mini-classes, and snippets for every day. - [Image](https://github.com/JBZoo/Image) - Package provides object-oriented way to manipulate with images as simple as possible. -- [Data](https://github.com/JBZoo/Data) - Extended implementation of ArrayObject. Use files as config/array. +- [Data](https://github.com/JBZoo/Data) - Extended implementation of ArrayObject. Use files as config/array. - [Retry](https://github.com/JBZoo/Retry) - Tiny PHP library providing retry/backoff functionality with multiple backoff strategies and jitter support. - [SimpleTypes](https://github.com/JBZoo/SimpleTypes) - Converting any values and measures - money, weight, exchange rates, length, ... diff --git a/src/Graph.php b/src/Graph.php index 6a57a3a..b4b8dfa 100644 --- a/src/Graph.php +++ b/src/Graph.php @@ -107,8 +107,11 @@ public function render(bool $isMainGraph = true, int $shift = 0): string if (count($this->links) > 0) { $tmp = []; - foreach ($this->links as $link) { + $i = 0; + foreach ($this->links as $idx => $link) { $tmp[] = $spacesSub . $link; + $this->links[$idx]->setIndex($i); + $i++; } if ($this->params['abc_order']) { sort($tmp); @@ -129,6 +132,14 @@ public function render(bool $isMainGraph = true, int $shift = 0): string } } + if (count($this->links) > 0) { + foreach ($this->links as $link) { + if ($style = $link->getStyle()) { + $result[] = $spaces . $style . ';'; + } + } + } + if (!$isMainGraph) { $result[] = "{$spaces}end"; } diff --git a/src/Link.php b/src/Link.php index 3cd6aed..4f64051 100644 --- a/src/Link.php +++ b/src/Link.php @@ -48,25 +48,37 @@ class Link /** * @var int */ - protected $style = self::ARROW; + protected $type = self::ARROW; /** * @var string */ protected $text = ''; + /** + * @var ?string + */ + protected $style = ''; + + /** + * @var int|null + */ + protected $index = null; + /** * @param Node $sourceNode * @param Node $targetNode * @param string $text - * @param int $style + * @param int $type + * @param string|null $style */ - public function __construct(Node $sourceNode, Node $targetNode, string $text = '', int $style = self::ARROW) + public function __construct(Node $sourceNode, Node $targetNode, string $text = '', int $type = self::ARROW, ?string $style = null) { $this->sourceNode = $sourceNode; $this->targetNode = $targetNode; + $this->style = $style; $this->setText($text); - $this->setStyle($style); + $this->setType($type); } /** @@ -80,12 +92,23 @@ public function setText(string $text): Link } /** - * @param int $style + * @param int $type * @return Link */ - public function setStyle(int $style): Link + public function setType(int $type): Link { - $this->style = $style; + $this->type = $type; + return $this; + } + + /** + * The link is assigned an index at render time, which is used in getStyle() + * @param int $index + * @return Link + */ + public function setIndex(int $index): Link + { + $this->index = $index; return $this; } @@ -94,11 +117,22 @@ public function setStyle(int $style): Link */ public function __toString() { - $line = self::TEMPLATES[$this->style][0]; + $line = self::TEMPLATES[$this->type][0]; if ($this->text) { - $line = sprintf(self::TEMPLATES[$this->style][1], Helper::escape($this->text)); + $line = sprintf(self::TEMPLATES[$this->type][1], Helper::escape($this->text)); } return "{$this->sourceNode->getId()}{$line}{$this->targetNode->getId()};"; } + + /** + * @return string|null + */ + public function getStyle(): ?string + { + if (is_null($this->index) || is_null($this->style)) { + return null; + } + return "linkStyle $this->index $this->style"; + } } diff --git a/src/Node.php b/src/Node.php index d2595e4..51ebb97 100644 --- a/src/Node.php +++ b/src/Node.php @@ -57,17 +57,24 @@ class Node */ protected $form = self::ROUND; + /** + * @var string|null + */ + protected $url = null; + /** * Node constructor. * @param string $identifier * @param string $title * @param string $form + * @param string|null $url */ - public function __construct(string $identifier, string $title = '', string $form = self::ROUND) + public function __construct(string $identifier, string $title = '', string $form = self::ROUND, string $url = null) { $this->identifier = static::isSafeMode() ? Helper::getId($identifier) : $identifier; $this->setTitle($title ?: $identifier); $this->setForm($form); + $this->url = $url; } /** @@ -118,12 +125,12 @@ public function getId(): string */ public function __toString() { + $url = $this->url ? "click $this->identifier \" $this->url\";" : ''; if ($this->title) { /* @phan-suppress-next-line PhanPluginPrintfVariableFormatString */ - return $this->identifier . sprintf((string)$this->form, Helper::escape($this->title)) . ';'; + return $this->identifier . sprintf((string)$this->form, Helper::escape($this->title)) . ";$url"; } - - return "{$this->identifier};"; + return "{$this->identifier};$url"; } /** diff --git a/tests/FlowchartTest.php b/tests/FlowchartTest.php index 6ccd031..3221b24 100644 --- a/tests/FlowchartTest.php +++ b/tests/FlowchartTest.php @@ -113,16 +113,16 @@ public function testLinkRendering() $link = new Link($nodeA, $nodeB); isSame('A-->B;', (string)$link); - isSame('A-->B;', (string)$link->setStyle(Link::ARROW)); - isSame('A --- B;', (string)$link->setStyle(Link::LINE)); - isSame('A ==> B;', (string)$link->setStyle(Link::THICK)); - isSame('A-.->B;', (string)$link->setStyle(Link::DOTTED)); + isSame('A-->B;', (string)$link->setType(Link::ARROW)); + isSame('A --- B;', (string)$link->setType(Link::LINE)); + isSame('A ==> B;', (string)$link->setType(Link::THICK)); + isSame('A-.->B;', (string)$link->setType(Link::DOTTED)); $link->setText('This is the text'); - isSame('A-->|"This is the text"|B;', (string)$link->setStyle(Link::ARROW)); - isSame('A---|"This is the text"|B;', (string)$link->setStyle(Link::LINE)); - isSame('A == "This is the text" ==> B;', (string)$link->setStyle(Link::THICK)); - isSame('A-. "This is the text" .-> B;', (string)$link->setStyle(Link::DOTTED)); + isSame('A-->|"This is the text"|B;', (string)$link->setType(Link::ARROW)); + isSame('A---|"This is the text"|B;', (string)$link->setType(Link::LINE)); + isSame('A == "This is the text" ==> B;', (string)$link->setType(Link::THICK)); + isSame('A-. "This is the text" .-> B;', (string)$link->setType(Link::DOTTED)); } public function testNotFoundNode()