bound1ess / mermaid-php
PHP adapter for the Mermaid project by @knsv (github.com/knsv/mermaid).
Installs: 7 395
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 1
Open Issues: 0
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2024-12-17 05:07:23 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/create
file, it will produceexample.html
file in your project root directory. - Now set up a PHP development server by running this command:
php -S localhost:8000
. - View the
example.html
file in your browser by visitinglocalhost:8000/example.html
.
API
-
Bound1ess\MermaidPhp\Printer
__construct($testMode = false)
$testMode
: when set totrue
,Printer
will produce code that is invalid, but much easier to test.
printGraph(Graph $graph, $wrapInDiv = false)
$graph
: an instance ofBound1ess\MermaidPhp\Graph
is 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
,setDirection
method 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\Node
is expected.- This method will return nothing (void).
addNodes(Node $node, ...)
addLink(Link $link)
$link
: an instance ofBound1ess\MermaidPhp\Link
is 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).
addClasses
dynamic
: as manyBound1ess\MermaidPhp\NodeClass
instances 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
,setText
method 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
. Ifnull
is passed,Node::SQUARE_EDGE
will 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).
attachClasses
dynamic
: as manystring
s 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\Node
is expected.$anotherNode
: an instance ofBound1ess\MermaidPhp\Node
is 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
: ifnull
is 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).