Passed
Push — master ( 1f61ad...9dcb8e )
by
unknown
49s queued 10s
created

Language::equals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 1
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\ValueObject;
10
11
/**
12
 * @see https://en.wikipedia.org/wiki/Language
13
 */
14
final class Language implements JsonSerializable, ValueObject
15
{
16
    /** @var string */
17
    private $name;
18
    /** @var string */
19
    private $code;
20
21
    /**
22
     * @param string|null $code a representation of one of this codes: https://en.wikipedia.org/wiki/Language_code
23
     */
24
    public function __construct(string $name, ?string $code = null)
25
    {
26
        if ($name === '') {
27
            throw new InvalidArgumentException('Name should not be empty string');
28
        }
29
30
        $this->name = $name;
31
        $this->code = $code;
32
    }
33
34
    public function getName() : string
35
    {
36
        return $this->name;
37
    }
38
39
    public function getCode() : ?string
40
    {
41
        return $this->code;
42
    }
43
44
    public function equals(?ValueObject $object) : bool
45
    {
46
        if (!$object instanceof self) {
47
            return false;
48
        }
49
50
        return $this->name === $object->getName() &&
51
            $this->code === $object->getCode();
52
    }
53
54
    public function __toString() : string
55
    {
56
        return sprintf('%s (%s)', $this->name, $this->code);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @return mixed[]
63
     */
64
    public function jsonSerialize() : array
65
    {
66
        return [
67
            'name' => $this->name,
68
            'code' => $this->code,
69
        ];
70
    }
71
}
72