bound1ess / mermaid-php
PHP adapter for the Mermaid project by @knsv (github.com/knsv/mermaid).
Installs: 7 396
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/bound1ess/mermaid-php
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2025-11-04 09:42:48 UTC
README
First, make yourself familiar with the brilliant Mermaid project.
Basically, MermaidPhp provides you a nice and clean interface to generate compatible with Mermaid code.
Now (if you want to give it a try), lets review the installation process.
Installation
In order to install MermaidPhp, you need Composer.
Run the following command in your project root directory:
composer require bound1ess/mermaid-php:~1.0
Ok, now include vendor/autoload.php file in your project (if you have not done this yet)
and you are all set to go!
Example of usage
This PHP code:
use Bound1ess\MermaidPhp\Graph, Bound1ess\MermaidPhp\Printer, Bound1ess\MermaidPhp\Node, Bound1ess\MermaidPhp\Link; $graph = new Graph('from left to right'); $graph->addNodes( $a = new Node('A', ['Hard edge']), $b = new Node('B', ['Round edge', Node::ROUND_EDGE]), $c = new Node('C', ['Decision', Node::RHOMBUS]), $d = new Node('D', ['Result one']), $e = new Node('E', ['Result two']) ); $graph->addLinks( new Link($a, $b, 'Link text'), new Link($b, $c), new Link($c, $d, 'One'), new Link($c, $e, 'Two') ); $code = (new Printer)->printGraph($graph);
Will produce something like this:
If you want to see this example in your browser, do the following:
- Execute
vendor/bound1ess/php-mermaid/examples/createfile, it will produceexample.htmlfile in your project root directory. - Now set up a PHP development server by running this command:
php -S localhost:8000. - View the
example.htmlfile in your browser by visitinglocalhost:8000/example.html.
API
-
Bound1ess\MermaidPhp\Printer__construct($testMode = false)$testMode: when set totrue,Printerwill produce code that is invalid, but much easier to test.
printGraph(Graph $graph, $wrapInDiv = false)$graph: an instance ofBound1ess\MermaidPhp\Graphis expected.$wrapInDiv: when set totrue, the code produced will be wrapped in a<div>element with the classmermaid.- This method will return a string.
-
Bound1ess\MermaidPhp\Graph__construct($direction = null)$direction: if notnull,setDirectionmethod will be called.
setDirection($direction)$direction: valid values areNode::TOP_BOTTOM,Node::BOTTOM_TOP,Node::LEFT_RIGHT,Node::RIGHT_LEFT,from top to bottom,from bottom to top,from left to right,from right to left.- This method will return nothing (void).
- This method can throw an instance of
Bound1ess\MermaidPhp\Exceptions\InvalidDirectionException.
addNode(Node $node)$node: an instance ofBound1ess\MermaidPhp\Nodeis expected.- This method will return nothing (void).
addNodes(Node $node, ...)addLink(Link $link)$link: an instance ofBound1ess\MermaidPhp\Linkis expected.- This method will return nothing (void).
addLinks(Link $link, ...)addClass(NodeClass $class)$class: an instance ofBound1ess\MermaidPhp\NodeClass.- This method will return nothing (void).
addClassesdynamic: as manyBound1ess\MermaidPhp\NodeClassinstances as you need.- This method will return nothing (void).
-
Bound1ess\MermaidPhp\Node__construct($id, array $settings = null)$id: the node id (should be a string).$settings: if notnull,setTextmethod will be called.
setText($text, $style = null)$text: the node text (should be a string).$style: valid values areNode::ROUND_EDGE,Node::SQUARE_EDGE,Node::CIRCLE,Node::RHOMBUS,Node::ASYMETRIC_SHAPE. Ifnullis passed,Node::SQUARE_EDGEwill be used.- This method can throw an instance of
Bound1ess\MermaidPhp\Exceptions\InvalidStyleException. - This method will return nothing (void).
attachClass($name)$name: the class name as astring.- This method will return nothing (void).
attachClassesdynamic: as manystrings as you need.- This method will return nothing (void).
-
Bound1ess\MermaidPhp\Link__construct(Node $node, Node $anotherNode, $text = null, $isOpen = false)$node: an instance ofBound1ess\MermaidPhp\Nodeis expected.$anotherNode: an instance ofBound1ess\MermaidPhp\Nodeis expected.$text: the text on link (should be a string ornull).$isOpen: whether the link should be open or with arrow head (should be a boolean, the default value isfalse).
setText($text)$text: the text on link (should be a string).- This method will return nothing (void).
isOpen($newValue = null)$newValue: ifnullis passed, this method will return the current value, otherwise new value will be set (should be a boolean value).- This method will return a boolean value or nothing (void).
-
Bound1ess\MermaidPhp\NodeClass__construct($name)$name: the class name as astring.
add($property, $value)$property: the property name as astring.$value: the property value as astring.- This method will return the class instance itself.
License information
This project is licensed under the MIT license (see the license file for detailed information).