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

Commit c0bfccc

Browse files
Implement Command::asString() method
1 parent 52d6da9 commit c0bfccc

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

src/domain/command/Command.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
namespace example\caledonia\domain;
3+
4+
/**
5+
* @no-named-arguments
6+
*/
7+
abstract readonly class Command
8+
{
9+
/**
10+
* @return non-empty-string
11+
*/
12+
abstract public function asString(): string;
13+
}

src/domain/command/PurchaseGoodCommand.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php declare(strict_types=1);
22
namespace example\caledonia\domain;
33

4+
use function sprintf;
5+
46
/**
57
* @no-named-arguments
68
*/
7-
final readonly class PurchaseGoodCommand
9+
final readonly class PurchaseGoodCommand extends Command
810
{
911
private Good $good;
1012

@@ -34,4 +36,16 @@ public function amount(): int
3436
{
3537
return $this->amount;
3638
}
39+
40+
/**
41+
* @return non-empty-string
42+
*/
43+
public function asString(): string
44+
{
45+
return sprintf(
46+
'Purchase %d %s',
47+
$this->amount,
48+
$this->good->value,
49+
);
50+
}
3751
}

src/domain/command/SellGoodCommand.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php declare(strict_types=1);
22
namespace example\caledonia\domain;
33

4+
use function sprintf;
5+
46
/**
57
* @no-named-arguments
68
*/
7-
final readonly class SellGoodCommand
9+
final readonly class SellGoodCommand extends Command
810
{
911
private Good $good;
1012

@@ -34,4 +36,16 @@ public function amount(): int
3436
{
3537
return $this->amount;
3638
}
39+
40+
/**
41+
* @return non-empty-string
42+
*/
43+
public function asString(): string
44+
{
45+
return sprintf(
46+
'Sell %d %s',
47+
$this->amount,
48+
$this->good->value,
49+
);
50+
}
3751
}

tests/unit/domain/command/PurchaseGoodCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public function testHasAmount(): void
2727

2828
$this->assertSame(1, $command->amount());
2929
}
30+
31+
public function testCanBeRepresentedAsString(): void
32+
{
33+
$command = new PurchaseGoodCommand(Good::Bread, 1);
34+
35+
$this->assertSame('Purchase 1 bread', $command->asString());
36+
}
3037
}

tests/unit/domain/command/SellGoodCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public function testHasAmount(): void
2727

2828
$this->assertSame(1, $command->amount());
2929
}
30+
31+
public function testCanBeRepresentedAsString(): void
32+
{
33+
$command = new SellGoodCommand(Good::Bread, 1);
34+
35+
$this->assertSame('Sell 1 bread', $command->asString());
36+
}
3037
}

0 commit comments

Comments
 (0)