Skip to content

Commit 3ed3b4d

Browse files
committed
[PHP] [User Modelling Example] Replace Data Value with Object for Locale final state where all clients call to the new API
1 parent 8df5112 commit 3ed3b4d

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

examples/php/php-user_modelling-02_value_objects/src/User.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ final class User
99
private Locale $locale;
1010
private Age $age;
1111

12-
public function __construct(string $locale, Age $age)
12+
private function __construct(Locale $locale, Age $age)
1313
{
14-
$this->locale = new Locale($locale);
14+
$this->locale = $locale;
1515
$this->age = $age;
1616
}
1717

18-
public static function signUp(Locale $locale, Age $age)
18+
public static function signUp(Locale $locale, Age $age): User
1919
{
20-
return new self($locale->value(), $age);
20+
// …
21+
// Any kind of additional logic such as recording domain events, checking overall Aggregate consistency, etc.
22+
// …
23+
return new self($locale, $age);
2124
}
2225

2326
public function understandSpanish(): bool

examples/php/php-user_modelling-02_value_objects/tests/UserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ public function should_detect_spanish_from_spain_as_spanish_language(): void
2222
/** @test */
2323
public function should_not_detect_english_from_great_britain_as_spanish_language(): void
2424
{
25-
$user = new User("en_GB", new Age(20));
25+
$user = User::signUp(new Locale("en_GB"), new Age(20));
2626

2727
self::assertFalse($user->understandSpanish());
2828
}
2929

3030
/** @test */
3131
public function should_detect_underage_users_until_18_years_old(): void
3232
{
33-
$user = new User("en_GB", new Age(17));
33+
$user = User::signUp(new Locale("en_GB"), new Age(17));
3434

3535
self::assertTrue($user->isUnderage());
3636
}
3737

3838
/** @test */
3939
public function should_not_detect_underage_users_starting_from_18_years_old(): void
4040
{
41-
$user = new User("en_GB", new Age(18));
41+
$user = User::signUp(new Locale("en_GB"), new Age(18));
4242

4343
self::assertFalse($user->isUnderage());
4444
}

0 commit comments

Comments
 (0)