diff --git a/src/ClassDiagramRenderer/ClassDiagram.php b/src/ClassDiagramRenderer/ClassDiagram.php index f2ab4a7..eba0979 100644 --- a/src/ClassDiagramRenderer/ClassDiagram.php +++ b/src/ClassDiagramRenderer/ClassDiagram.php @@ -22,8 +22,6 @@ public function addNode(Node $node): self { $this->nodes[] = $node; - Node::sortNodes($this->nodes); - return $this; } @@ -31,13 +29,14 @@ 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) { diff --git a/src/ClassDiagramRenderer/Node/Node.php b/src/ClassDiagramRenderer/Node/Node.php index d1d669f..38ecd38 100644 --- a/src/ClassDiagramRenderer/Node/Node.php +++ b/src/ClassDiagramRenderer/Node/Node.php @@ -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 @@ -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()), ]; } diff --git a/src/ClassDiagramRenderer/Node/Nodes.php b/src/ClassDiagramRenderer/Node/Nodes.php index 44717df..ec15871 100644 --- a/src/ClassDiagramRenderer/Node/Nodes.php +++ b/src/ClassDiagramRenderer/Node/Nodes.php @@ -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;