-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Entity Relationship Diagrams (#14)
- Loading branch information
1 parent
afa2c94
commit 120c0d1
Showing
12 changed files
with
1,133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Mermaid-PHP. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Mermaid-PHP | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\MermaidPHP\ERDiagram; | ||
|
||
use JBZoo\MermaidPHP\ERDiagram\Entity\Entity; | ||
use JBZoo\MermaidPHP\ERDiagram\Relation\Relation; | ||
use JBZoo\MermaidPHP\Helper; | ||
use JBZoo\MermaidPHP\Render; | ||
|
||
class ERDiagram | ||
{ | ||
private const RENDER_SHIFT = 4; | ||
|
||
/** @var Entity[] */ | ||
protected array $entities = []; | ||
|
||
/** @var Relation[] */ | ||
protected array $relations = []; | ||
|
||
/** @var mixed[] */ | ||
protected array $params = [ | ||
'abc_order' => false, | ||
'title' => '', | ||
]; | ||
|
||
/** | ||
* @param mixed[] $params | ||
*/ | ||
public function __construct(array $params = []) | ||
{ | ||
$this->setParams($params); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->render(); | ||
} | ||
|
||
public function addEntity(Entity $entity): self | ||
{ | ||
$this->entities[$entity->getId()] = $entity; | ||
|
||
return $this; | ||
} | ||
|
||
public function addRelation(Relation $relation): self | ||
{ | ||
$this->relations[$relation->getId()] = $relation; | ||
|
||
return $this; | ||
} | ||
|
||
public function setParams(array $params): self | ||
{ | ||
$this->params = \array_merge($this->params, $params); | ||
|
||
return $this; | ||
} | ||
|
||
public function getParams(): array | ||
{ | ||
return $this->params; | ||
} | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.NPathComplexity) | ||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
*/ | ||
public function render(int $shift = 0): string | ||
{ | ||
$spacesSub = \str_repeat(' ', $shift + self::RENDER_SHIFT); | ||
|
||
$result = []; | ||
$params = $this->getParams(); | ||
|
||
if (isset($params['title']) && $params['title'] !== '') { | ||
$result[] = \sprintf('---%s---', \PHP_EOL . 'title: ' . $params['title'] . \PHP_EOL); | ||
} | ||
|
||
$result[] = 'erDiagram'; | ||
|
||
if (\count($this->relations) > 0) { | ||
$tmp = []; | ||
|
||
foreach ($this->relations as $relation) { | ||
$tmp[] = $spacesSub . $relation; | ||
} | ||
|
||
if ($this->params['abc_order'] === true) { | ||
\sort($tmp); | ||
} | ||
|
||
$result = \array_merge($result, $tmp); | ||
} | ||
|
||
$entitiesWithProps = \array_filter($this->entities, static fn (Entity $entity) => $entity->getProps() !== []); | ||
|
||
if (\count($entitiesWithProps) > 0) { | ||
$tmp = []; | ||
|
||
foreach ($entitiesWithProps as $entity) { | ||
$classEntity = $entity->renderProps(); | ||
if ($classEntity !== null) { | ||
$tmp[] = $spacesSub . $classEntity; | ||
} | ||
} | ||
|
||
if ($this->params['abc_order'] === true) { | ||
\sort($tmp); | ||
} | ||
|
||
$result = \array_merge($result, $tmp); | ||
} | ||
|
||
$result[] = ''; | ||
|
||
return \implode(\PHP_EOL, $result); | ||
} | ||
|
||
/** | ||
* @param array<string> $params | ||
*/ | ||
public function renderHtml(array $params = []): string | ||
{ | ||
return Render::html($this, $params); | ||
} | ||
|
||
public function getLiveEditorUrl(): string | ||
{ | ||
return Helper::getLiveEditorUrl($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Mermaid-PHP. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Mermaid-PHP | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\MermaidPHP\ERDiagram\Entity; | ||
|
||
use JBZoo\MermaidPHP\Helper; | ||
|
||
class Entity | ||
{ | ||
private static bool $safeMode = false; | ||
protected string $identifier = ''; | ||
protected string $title = ''; | ||
|
||
/** @var EntityProperty[] */ | ||
protected array $props = []; | ||
|
||
/** | ||
* @param EntityProperty[] $props | ||
*/ | ||
public function __construct(string $identifier, string $title = '', array $props = []) | ||
{ | ||
$this->identifier = static::isSafeMode() ? Helper::getId($identifier) : $identifier; | ||
$this->setTitle($title === '' ? $identifier : $title); | ||
$this->setProps($props); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if ($this->title !== '') { | ||
return Helper::escape($this->title); | ||
} | ||
|
||
return "{$this->identifier};"; | ||
} | ||
|
||
public function setTitle(string $title): self | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title === '' ? $this->getId() : $this->title; | ||
} | ||
|
||
/** | ||
* @return EntityProperty[] | ||
*/ | ||
public function getProps(): array | ||
{ | ||
return $this->props; | ||
} | ||
|
||
/** | ||
* @param EntityProperty[] $props | ||
*/ | ||
public function setProps(array $props): void | ||
{ | ||
$this->props = $props; | ||
} | ||
|
||
public function renderProps(): ?string | ||
{ | ||
$spaces = \str_repeat(' ', 4); | ||
|
||
$props = $this->getProps(); | ||
if ($props !== []) { | ||
$output = $this . ' {' . \PHP_EOL; | ||
|
||
foreach ($props as $prop) { | ||
/** @var \JBZoo\MermaidPHP\ERDiagram\Entity\EntityProperty $prop */ | ||
$output .= $spaces . $spaces . $prop . \PHP_EOL; | ||
} | ||
|
||
return $output . $spaces . '}'; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
public static function isSafeMode(): bool | ||
{ | ||
return self::$safeMode; | ||
} | ||
} |
Oops, something went wrong.