Skip to content

Commit 162e9d6

Browse files
committed
support non-empty-array<Product>.
If `non-empty-array` is specified, change the dependency arrow caption from `*` to `1..*` #55
1 parent 88e5f43 commit 162e9d6

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
### Features
4+
5+
* support non-empty-array<Product>. If `non-empty-array` is specified, change the dependency arrow caption from `*` to `1..*` #55
6+
37
## v1.2.4 (2024-01-08)
48

59
### Bug fix

src/DiagramElement/ArrowDependency.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,27 @@ public function toString(PhpClass $toClass): string
1414
{
1515
if (strpos($this->getTo()->getName(), '[]') !== false) {
1616
// ex. Product[]
17-
return $this->getExpression($toClass);
17+
return $this->getExpression($toClass, false);
1818
}
1919
if (strpos($this->getTo()->getName(), 'array<') === 0) {
2020
// ex. array<Product> or array<int, Product>
21-
return $this->getExpression($toClass);
21+
return $this->getExpression($toClass, false);
22+
}
23+
if (strpos($this->getTo()->getName(), 'non-empty-array<') === 0) {
24+
// ex. non-empty-array<Product> or non-empty-array<int, Product>
25+
return $this->getExpression($toClass, true);
2226
}
2327
return sprintf(' %s %s %s', $this->getFrom()->getClassNameAlias(), $this->figure, $toClass->getClassNameAlias());
2428
}
2529

26-
private function getExpression(PhpClass $toClass): string
30+
private function getExpression(PhpClass $toClass, bool $nonEmpty): string
2731
{
28-
return sprintf(' %s "1" %s "*" %s', $this->getFrom()->getClassNameAlias(), $this->figure, str_replace('[]', '', $toClass->getClassNameAlias()));
32+
return sprintf(
33+
' %s "1" %s "%s" %s',
34+
$this->getFrom()->getClassNameAlias(),
35+
$this->figure,
36+
$nonEmpty ? '1..*' : '*',
37+
str_replace('[]', '', $toClass->getClassNameAlias())
38+
);
2939
}
3040
}

test/RelationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ public function testGetRelations_array_expression_in_doc(): void
135135
$rel = new Relation($entries, $options);
136136
$relations = $rel->getRelations();
137137

138-
$this->assertSame(1, count($relations), 'count');
139-
$this->assertSame(' product_Product "1" ..> "*" product_Tag', $relations[0], 'relation 1');
138+
$this->assertSame(2, count($relations), 'count');
139+
$this->assertSame(' product_Product "1" ..> "*" product_Tag', $relations[0], 'relation *');
140+
$this->assertSame(' product_Product "1" ..> "1..*" product_Tag', $relations[1], 'relation 1..*');
140141
}
141142

142143
public function testGetRelations_extends1(): void

test/fixtures/array-expression-in-doc/product/Product.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class Product {
55
/** @var array<Tag> */
66
private array $tags = [];
77

8+
/** @var non-empty-array<Tag> */
9+
private array $nonEmptytags;
10+
811
/**
912
* @return array<Tag>
1013
*/

0 commit comments

Comments
 (0)