Skip to content

Commit c9a4c9b

Browse files
HP-1631: fixed CustomerHydrator and created CustomerStateHydrator
1 parent dfb7231 commit c9a4c9b

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/customer/CustomerState.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace hiqdev\php\billing\customer;
44

5-
enum CustomerState: string
5+
class CustomerState
66
{
7-
case BLOCKED = 'blocked';
8-
case DELETED = 'deleted';
9-
case NEW = 'new';
10-
case OK = 'ok';
7+
public const BLOCKED = 'blocked';
8+
9+
public const DELETED = 'deleted';
10+
11+
public const NEW = 'new';
12+
13+
public const OK = 'ok';
14+
15+
private function __construct(protected string $state = self::NEW)
16+
{
17+
}
18+
19+
public function getName(): string
20+
{
21+
return $this->state;
22+
}
1123

1224
public static function isDeleted(CustomerInterface $customer): bool
1325
{
@@ -16,6 +28,23 @@ public static function isDeleted(CustomerInterface $customer): bool
1628

1729
public static function deleted(): CustomerState
1830
{
19-
return self::DELETED;
31+
return new self(self::DELETED);
32+
}
33+
34+
public static function fromString(string $name): self
35+
{
36+
$allowedStates = [
37+
self::BLOCKED,
38+
self::DELETED,
39+
self::NEW,
40+
self::OK,
41+
];
42+
foreach ($allowedStates as $state) {
43+
if ($state === $name) {
44+
return new self($state);
45+
}
46+
}
47+
48+
throw new \Exception("wrong customer state '$name'");
2049
}
2150
}

0 commit comments

Comments
 (0)