-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
84 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Finite\Dumper; | ||
|
||
use Finite\State; | ||
|
||
interface Dumper | ||
{ | ||
/** | ||
* @param enum-string<State> $stateEnum | ||
*/ | ||
public function dump(string $stateEnum): string; | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Finite\Dumper; | ||
|
||
use Finite\State; | ||
|
||
class MermaidDumper implements Dumper | ||
{ | ||
/** | ||
* @param enum-string<State> $stateEnum | ||
*/ | ||
public function dump(string $stateEnum): string | ||
{ | ||
$output = [ | ||
'---', | ||
'title: ' . $stateEnum, | ||
'---', | ||
'stateDiagram-v2', | ||
]; | ||
|
||
foreach ($stateEnum::getTransitions() as $transition) { | ||
foreach ($transition->getSourceStates() as $state) { | ||
$output[] = sprintf( | ||
' %s --> %s: %s', | ||
$state->value, | ||
$transition->getTargetState()->value, | ||
$transition->getName() | ||
); | ||
} | ||
} | ||
|
||
return implode(PHP_EOL, $output); | ||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
|
||
namespace Finite\Tests\Dumper; | ||
|
||
use Finite\Dumper\MermaidDumper; | ||
use Finite\Tests\E2E\SimpleArticleState; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MermaidDumperTest extends TestCase | ||
{ | ||
public function test_it_dumps(): void | ||
{ | ||
$this->assertSame( | ||
<<<MERMAID | ||
--- | ||
title: Finite\Tests\E2E\SimpleArticleState | ||
--- | ||
stateDiagram-v2 | ||
draft --> published: publish | ||
reported --> published: clear | ||
disabled --> published: clear | ||
published --> reported: report | ||
reported --> disabled: disable | ||
published --> disabled: disable | ||
MERMAID, | ||
(new MermaidDumper)->dump(SimpleArticleState::class) | ||
); | ||
} | ||
} |
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