Skip to content

Commit e445bba

Browse files
committed
[PHP] [User Modelling Example] Replace Data Value with Object for Age
1 parent 43d8d12 commit e445bba

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
final 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
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CodelyTv\UserModelling\Test;
66

7+
use CodelyTv\UserModelling\Age;
78
use CodelyTv\UserModelling\User;
89
use 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
}

0 commit comments

Comments
 (0)