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

Language   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 55
rs 10
c 1
b 0
f 1
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A equals() 0 8 3
A __construct() 0 8 2
A __toString() 0 3 1
A getCode() 0 3 1
A jsonSerialize() 0 5 1
A getName() 0 3 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