Completed
Push — develop ( 98f3b3...87537f )
by Alexandru
01:09
created

County::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 9
nc 6
nop 1
crap 5
1
<?php
2
/**
3
 * Copyright 2017 Alexandru Guzinschi <[email protected]>
4
 *
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
namespace Vimishor\Cnp;
9
10
use Gentle\Embeddable\Embeddable;
11
12
/**
13
 * @author Alexandru Guzinschi <[email protected]>
14
 */
15
final class County extends Embeddable
16
{
17
    /** @var array */
18
    private static $counties = [
19
        '01' => 'Alba',
20
        '02' => 'Arad',
21
        '03' => 'Arges',
22
        '04' => 'Bacau',
23
        '05' => 'Bihor',
24
        '06' => 'Bistrita-Nasaud',
25
        '07' => 'Botosani',
26
        '08' => 'Brasov',
27
        '09' => 'Braila',
28
        '10' => 'Buzau',
29
        '11' => 'Caras-Severin',
30
        '12' => 'Cluj',
31
        '13' => 'Constanta',
32
        '14' => 'Covasna',
33
        '15' => 'Dambovita',
34
        '16' => 'Dolj',
35
        '17' => 'Galati',
36
        '18' => 'Gorj',
37
        '19' => 'Harghita',
38
        '20' => 'Hunedoara',
39
        '21' => 'Ialomita',
40
        '22' => 'Iasi',
41
        '23' => 'Ilfov',
42
        '24' => 'Maramures',
43
        '25' => 'Mehedinti',
44
        '26' => 'Mures',
45
        '27' => 'Neamt',
46
        '28' => 'Olt',
47
        '29' => 'Prahova',
48
        '30' => 'Satu Mare',
49
        '31' => 'Salaj',
50
        '32' => 'Sibiu',
51
        '33' => 'Suceava',
52
        '34' => 'Teleorman',
53
        '35' => 'Timis',
54
        '36' => 'Tulcea',
55
        '37' => 'Vaslui',
56
        '38' => 'Valcea',
57
        '39' => 'Vrancea',
58
        '40' => 'Bucuresti',
59
        '41' => 'Bucuresti/Sectorul 1',
60
        '42' => 'Bucuresti/Sectorul 2',
61
        '43' => 'Bucuresti/Sectorul 3', // was merged with district 2 in 1979 => district 2
62
        '44' => 'Bucuresti/Sectorul 4', // district 3 since 1979
63
        '45' => 'Bucuresti/Sectorul 5', // district 4 since 1979
64
        '46' => 'Bucuresti/Sectorul 6', // district 5 since 1979
65
        '47' => 'Bucuresti/Sectorul 7', // district 6 since 1979
66
        '48' => 'Bucuresti/Sectorul 8', // was merged with district 1 in 1979 => district 1
67
        '51' => 'Calarasi',
68
        '52' => 'Giurgiu'
69
    ];
70
71
    /**
72
     * @param string|int $county
73
     *
74
     * @throws \InvalidArgumentException
75
     */
76 66
    public function __construct($county)
77
    {
78 66
        if (is_int($county)) {
79 26
            $county = (string)$county;
80 13
        }
81
82 66
        if (!is_string($county) && !ctype_digit($county)) {
83 6
            throw new \InvalidArgumentException(sprintf('Expected string or integer and got %s', gettype($county)));
84
        }
85
86 60
        $county = str_pad((string)$county, 2, 0, STR_PAD_LEFT);
87
88 60
        if (!array_key_exists($county, self::$counties)) {
89 12
            throw new \InvalidArgumentException(sprintf('%s is not a known county.', $county));
90
        }
91
92 48
        $this->value = $county;
93 48
    }
94
95
    /**
96
     * @access public
97
     * @return string
98
     */
99 12
    public function getName()
100
    {
101 12
        return self::$counties[$this->value];
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 12
    public function equals(Embeddable $object)
108
    {
109 12
        return get_class($object) === 'Vimishor\Cnp\County' && $this->value === (string)$object;
110
    }
111
}
112