Skip to content

Commit

Permalink
Merge pull request #9 from tasuku43/refactor-node
Browse files Browse the repository at this point in the history
refactor node
  • Loading branch information
tasuku43 authored Jun 24, 2024
2 parents 7a6c658 + ab91218 commit 8c33bef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/ClassDiagramRenderer/ClassDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ public function addNode(Node $node): self
{
$this->nodes[] = $node;

Node::sortNodes($this->nodes);

return $this;
}

public function addRelationships(Relationship ...$relationships): self
{
$this->relationships = [...$this->relationships, ...$relationships];

Relationship::sortRelationships($this->relationships);

return $this;
}

public function render(): string
{
Node::sortNodes($this->nodes);
Relationship::sortRelationships($this->relationships);

$output = "classDiagram\n";

foreach ($this->nodes as $node) {
Expand Down
29 changes: 12 additions & 17 deletions src/ClassDiagramRenderer/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,32 @@

abstract class Node
{
/** @var Node[] */
protected array $extends = [];

/** @var Node[] */
protected array $implements = [];

/** @var Node[] */
protected array $properties = [];
protected Nodes $extends;
protected Nodes $implements;
protected Nodes $properties;

public function __construct(protected string $name)
{
$this->extends = Nodes::empty();
$this->implements = Nodes::empty();
$this->properties = Nodes::empty();
}

abstract public function render(): string;

public function extends(Node $node): void
{
$this->extends[] = $node;
self::sortNodes($this->extends);
$this->extends->add($node);
}

public function implements(Node $node): void
{
$this->implements[] = $node;
self::sortNodes($this->implements);
$this->implements->add($node);
}

public function composition(Node $node): void
{
$this->properties[] = $node;
self::sortNodes($this->properties);
$this->properties->add($node);
}

public function nodeName(): string
Expand All @@ -54,9 +49,9 @@ public function nodeName(): string
public function relationships(): array
{
return [
...array_map(fn(Node $extendsNode) => new Inheritance($this, $extendsNode), $this->extends),
...array_map(fn(Node $implementsNode) => new Realization($this, $implementsNode), $this->implements),
...array_map(fn(Node $propertyNode) => new Composition($this, $propertyNode), $this->properties),
...array_map(fn(Node $extendsNode) => new Inheritance($this, $extendsNode), $this->extends->getAllNodes()),
...array_map(fn(Node $implementsNode) => new Realization($this, $implementsNode), $this->implements->getAllNodes()),
...array_map(fn(Node $propertyNode) => new Composition($this, $propertyNode), $this->properties->getAllNodes()),
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/ClassDiagramRenderer/Node/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function __construct()
$this->nodes = [];
}

public static function empty(): self
{
return new self();
}

public function add(Node $node): self
{
$this->nodes[$node->nodeName()] = $node;
Expand Down

0 comments on commit 8c33bef

Please sign in to comment.