|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Talentify\ValueObject\Linguistics; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
use Talentify\ValueObject\StringUtils; |
|
10
|
|
|
use Talentify\ValueObject\ValueObject; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_639-1 |
|
14
|
|
|
* @see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes |
|
15
|
|
|
*/ |
|
16
|
|
|
final class LanguageISO6391 implements JsonSerializable, ValueObject |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $name; |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $isoCode; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string|null $isoCode a representation of one of this codes: https://en.wikipedia.org/wiki/Language_code |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(string $name, ?string $isoCode = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setName($name); |
|
29
|
|
|
$this->setIsoCode($isoCode); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setName(string $name) : void |
|
33
|
|
|
{ |
|
34
|
|
|
$normalized = StringUtils::removeSpaces($name); |
|
35
|
|
|
if (empty($normalized) || strlen($normalized) < 2) { |
|
36
|
|
|
throw new InvalidArgumentException("The value '$name' is not a valid language name"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (strlen($normalized) === 2) { |
|
40
|
|
|
$this->setIsoCode($normalized); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->name = StringUtils::convertCaseToTitle($normalized); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setIsoCode(?string $isoCode) : void |
|
47
|
|
|
{ |
|
48
|
|
|
if ($isoCode === null) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$normalized = StringUtils::removeSpaces($isoCode); |
|
53
|
|
|
if (empty($normalized) || strlen($normalized) !== 2) { |
|
54
|
|
|
throw new InvalidArgumentException("The value '$isoCode' is not a valid language iso-code"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->isoCode = StringUtils::convertCaseToLower($normalized); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getName() : string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getIsoCode() : ?string |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->isoCode; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function equals(?ValueObject $object) : bool |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$object instanceof self) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->name === $object->getName() && |
|
77
|
|
|
$this->isoCode === $object->getIsoCode(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function __toString() : string |
|
81
|
|
|
{ |
|
82
|
|
|
return sprintf('%s (%s)', $this->name, $this->isoCode); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed[] |
|
89
|
|
|
*/ |
|
90
|
|
|
public function jsonSerialize() : array |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
'name' => $this->name, |
|
94
|
|
|
'code' => $this->isoCode, |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|