File tree Expand file tree Collapse file tree 3 files changed +30
-8
lines changed
examples/php/php-user_modelling-02_value_objects Expand file tree Collapse file tree 3 files changed +30
-8
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace CodelyTv \UserModelling ;
6+
7+ final class Age
8+ {
9+ private const UNDERAGE_UNTIL_AGE = 18 ;
10+
11+ private int $ value ;
12+
13+ public function __construct (int $ value )
14+ {
15+ $ this ->value = $ value ;
16+ }
17+
18+ public function isUnderage (): bool
19+ {
20+ return $ this ->value < self ::UNDERAGE_UNTIL_AGE ;
21+ }
22+ }
Original file line number Diff line number Diff line change 77final class User
88{
99 private const SPANISH_LANGUAGE = "es " ;
10- private const UNDERAGE_UNTIL_AGE = 18 ;
1110
1211 private string $ locale ;
13- private int $ age ;
12+ private Age $ age ;
1413
15- public function __construct (string $ locale , int $ age )
14+ public function __construct (string $ locale , Age $ age )
1615 {
1716 $ this ->locale = $ locale ;
1817 $ this ->age = $ age ;
@@ -27,6 +26,6 @@ public function understandSpanish(): bool
2726
2827 public function isUnderage (): bool
2928 {
30- return $ this ->age < self :: UNDERAGE_UNTIL_AGE ;
29+ return $ this ->age -> isUnderage () ;
3130 }
3231}
Original file line number Diff line number Diff line change 44
55namespace CodelyTv \UserModelling \Test ;
66
7+ use CodelyTv \UserModelling \Age ;
78use CodelyTv \UserModelling \User ;
89use PHPUnit \Framework \TestCase ;
910
@@ -12,31 +13,31 @@ final class UserTest extends TestCase
1213 /** @test */
1314 public function should_detect_spanish_from_spain_as_spanish_language (): void
1415 {
15- $ user = new User ("es_ES " , 20 );
16+ $ user = new User ("es_ES " , new Age ( 20 ) );
1617
1718 self ::assertTrue ($ user ->understandSpanish ());
1819 }
1920
2021 /** @test */
2122 public function should_not_detect_english_from_great_britain_as_spanish_language (): void
2223 {
23- $ user = new User ("en_GB " , 20 );
24+ $ user = new User ("en_GB " , new Age ( 20 ) );
2425
2526 self ::assertFalse ($ user ->understandSpanish ());
2627 }
2728
2829 /** @test */
2930 public function should_detect_underage_users_until_18_years_old (): void
3031 {
31- $ user = new User ("en_GB " , 17 );
32+ $ user = new User ("en_GB " , new Age ( 17 ) );
3233
3334 self ::assertTrue ($ user ->isUnderage ());
3435 }
3536
3637 /** @test */
3738 public function should_not_detect_underage_users_starting_from_18_years_old (): void
3839 {
39- $ user = new User ("en_GB " , 18 );
40+ $ user = new User ("en_GB " , new Age ( 18 ) );
4041
4142 self ::assertFalse ($ user ->isUnderage ());
4243 }
You can’t perform that action at this time.
0 commit comments