Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link styles and node links #7

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ on:
push:
branches:
- 'master'
schedule:
- cron: '20 */8 * * *'
# schedule:
# - cron: '20 */8 * * *'

env:
COLUMNS: 120
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down Expand Up @@ -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<br>/\\!@#$%^&*()_+><\' " '));

$subGraph2
Expand All @@ -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
Expand All @@ -73,6 +73,7 @@ graph TB;
alone-->C;
end
linkStyle default interpolate basis;
linkStyle 3 stroke:blue,stroke-width:4px;
```


Expand Down Expand Up @@ -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, ...
13 changes: 12 additions & 1 deletion src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
}
Expand Down
52 changes: 43 additions & 9 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}

Expand All @@ -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";
}
}
15 changes: 11 additions & 4 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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";
}

/**
Expand Down
16 changes: 8 additions & 8 deletions tests/FlowchartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down