Skip to content

Commit 2fefa68

Browse files
committed
[PHP] [User Modelling Example] Replace Data Value with Object for Locale without modifying its API
1 parent e445bba commit 2fefa68

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\UserModelling;
6+
7+
final class Locale
8+
{
9+
private const SPANISH_LANGUAGE = "es";
10+
11+
private string $value;
12+
13+
public function __construct(string $value)
14+
{
15+
$this->value = $value;
16+
}
17+
18+
public function understandSpanish(): bool
19+
{
20+
$language = substr($this->value, 0, 2);
21+
22+
return $language === self::SPANISH_LANGUAGE;
23+
}
24+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66

77
final class User
88
{
9-
private const SPANISH_LANGUAGE = "es";
10-
11-
private string $locale;
9+
private Locale $locale;
1210
private Age $age;
1311

1412
public function __construct(string $locale, Age $age)
1513
{
16-
$this->locale = $locale;
14+
$this->locale = new Locale($locale);
1715
$this->age = $age;
1816
}
1917

2018
public function understandSpanish(): bool
2119
{
22-
$language = substr($this->locale, 0, 2);
23-
24-
return $language === self::SPANISH_LANGUAGE;
20+
return $this->locale->understandSpanish();
2521
}
2622

2723
public function isUnderage(): bool

0 commit comments

Comments
 (0)