Country::create()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace LeKoala\GeoTools\Models;
4
5
/**
6
 * A global country model
7
 */
8
class Country
9
{
10
    /**
11
     * Uppercased country code
12
     * @var string
13
     */
14
    protected $code;
15
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
21
    public function __construct(string $code = null, string $name = null)
22
    {
23
        if ($code) {
24
            $code = strtoupper($code);
25
        }
26
        $this->code = $code;
27
        $this->name = $name;
28
    }
29
30
    /**
31
     * Create from a given source (array, pairs)
32
     *
33
     * Country::create('be,Belgium')
34
     * Country::create('be','Belgium')
35
     * Country::create(['be','Belgium'])
36
     *
37
     * @param mixed $source
38
     * @param array<string> $more
39
     * @return self
40
     */
41
    public static function create($source, ...$more)
42
    {
43
        if (!is_array($source)) {
44
            $source = explode(',', $source);
45
        }
46
        if (!empty($more)) {
47
            $source = array_merge($source, $more);
48
        }
49
        return new self($source[0], $source[1]);
50
    }
51
52
    /**
53
     * Get the uppercased country code
54
     */
55
    public function getCode(): ?string
56
    {
57
        return $this->code;
58
    }
59
60
    /**
61
     * Set the country code
62
     */
63
    public function setCode(string $code): self
64
    {
65
        $this->code = strtoupper($code);
66
        return $this;
67
    }
68
69
    /**
70
     * Get the name of the country
71
     */
72
    public function getName(): ?string
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * Set the name of the country
79
     */
80
    public function setName(string $name): self
81
    {
82
        $this->name = $name;
83
        return $this;
84
    }
85
}
86