Completed
Push — master ( 272924...57ae86 )
by Tobias
02:12
created

Country::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3.0416
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Model;
14
15
use Geocoder\Exception\InvalidArgument;
16
17
/**
18
 * A Country has either a name or a code. A Country will never be without data.
19
 *
20
 * @author William Durand <[email protected]>
21
 */
22
final class Country
23
{
24
    /**
25
     * @var string|null
26
     */
27
    private $name;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $code;
33
34
    /**
35
     * @param string $name
36
     * @param string $code
37
     */
38 6
    public function __construct(string $name = null, string $code = null)
39
    {
40 6
        if (null === $name && null === $code) {
41
            throw new InvalidArgument('A country must have either a name or a code');
42
        }
43
44 6
        $this->name = $name;
45 6
        $this->code = $code;
46 6
    }
47
48
    /**
49
     * Returns the country name.
50
     *
51
     * @return string|null
52
     */
53 6
    public function getName()
54
    {
55 6
        return $this->name;
56
    }
57
58
    /**
59
     * Returns the country ISO code.
60
     *
61
     * @return string|null
62
     */
63 6
    public function getCode()
64
    {
65 6
        return $this->code;
66
    }
67
68
    /**
69
     * Returns a string with the country name.
70
     *
71
     * @return string
72
     */
73
    public function __toString(): string
74
    {
75
        return $this->getName() ?: '';
76
    }
77
}
78