Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit a4f3552

Browse files
committed
feat: Update Relationship class constructor to public and implement custom sorting for relationships
1 parent 5a9a419 commit a4f3552

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/ClassDiagramRenderer/Node/Relationship/Relationship.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
abstract class Relationship
99
{
1010
protected const FORMAT = "%s %s %s: %s";
11-
public function __construct(protected Node $from, protected Node $to)
11+
public function __construct(public Node $from, public Node $to)
1212
{
1313
}
1414

src/ClassDiagramRenderer/Node/Relationship/Relationships.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function getAll(): array
3434

3535
public function sort(): void
3636
{
37-
Relationship::sortRelationships($this->relationships);
37+
usort($this->relationships, function (Relationship $a, Relationship $b) {
38+
$aKey = $a->from->nodeName() . ' ' . $a->to->nodeName();
39+
$bKey = $b->from->nodeName() . ' ' . $b->to->nodeName();
40+
return strcmp($aKey, $bKey);
41+
});
3842
}
3943

4044
public function filter(RenderOptions $options): self
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tasuku43\Tests\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Node;
8+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship\Dependency;
9+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship\Inheritance;
10+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship\Realization;
11+
use Tasuku43\MermaidClassDiagram\ClassDiagramRenderer\Node\Relationship\Relationships;
12+
13+
class RelationshipsTest extends TestCase
14+
{
15+
public function testSort(): void
16+
{
17+
$a = $this->mockNode('A');
18+
$b = $this->mockNode('B');
19+
$c = $this->mockNode('C');
20+
21+
$relationships = Relationships::empty();
22+
23+
// Intentionally add in non-sorted order by (from, to)
24+
$relationships->add(new Inheritance($c, $b)); // key: "C B"
25+
$relationships->add(new Dependency($a, $c)); // key: "A C"
26+
$relationships->add(new Realization($b, $a)); // key: "B A"
27+
28+
$relationships->sort();
29+
$sorted = $relationships->getAll();
30+
31+
$this->assertCount(3, $sorted);
32+
$this->assertInstanceOf(Dependency::class, $sorted[0]); // A C
33+
$this->assertSame('A', $sorted[0]->from->nodeName());
34+
$this->assertSame('C', $sorted[0]->to->nodeName());
35+
36+
$this->assertInstanceOf(Realization::class, $sorted[1]); // B A
37+
$this->assertSame('B', $sorted[1]->from->nodeName());
38+
$this->assertSame('A', $sorted[1]->to->nodeName());
39+
40+
$this->assertInstanceOf(Inheritance::class, $sorted[2]); // C B
41+
$this->assertSame('C', $sorted[2]->from->nodeName());
42+
$this->assertSame('B', $sorted[2]->to->nodeName());
43+
}
44+
45+
private function mockNode(string $name): Node
46+
{
47+
return new class($name) extends Node {
48+
public function render(): string
49+
{
50+
return "class {$this->name} {}";
51+
}
52+
};
53+
}
54+
}
55+

0 commit comments

Comments
 (0)