Completed
Pull Request — master (#10)
by
unknown
01:49
created

LanguageISO6391::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
    private 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
    private 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