Country::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace EveryPolitician\EveryPolitician;
4
5
// A class that represents a country from the countries.json file
6
class Country
7
{
8
    public $name;
9
    public $code;
10
    public $slug;
11
    protected $countryData;
12
13
    /**
14
     * Creates a new instance
15
     *
16
     * @param array $countryData Popolo country data
17
     */
18 51
    public function __construct($countryData)
19
    {
20 51
        $properties = ['name', 'code', 'slug'];
21 51
        foreach ($properties as $k) {
22 51
            $this->$k = $countryData[$k];
23 17
        }
24 51
        $this->countryData = $countryData;
25 51
    }
26
27
    /**
28
     * String representation of {@link Country}
29
     *
30
     * @return string
31
     */
32 6
    public function __toString()
33
    {
34 6
        return '<Country: '.$this->name.'>';
35
    }
36
37
    /**
38
     * Return all the {@link Legislature}s known for this {@link Country}
39
     *
40
     * A {@link Legislature} is a chamber of a parliament, e.g. the House of
41
     * Commons in the UK.
42
     *
43
     * @return Legislature[]
44
     */
45 27
    public function legislatures()
46
    {
47 27
        $legislatures = [];
48 27
        $legislaturesData = $this->countryData['legislatures'];
49 27
        foreach ($legislaturesData as $legislatureData) {
50 27
            $legislatures[] = new Legislature($legislatureData, $this);
51 9
        }
52 27
        return $legislatures;
53
    }
54
    /**
55
     * Return a {@link Legislature} in this {@link Country} from its slug
56
     *
57
     * @param string $legislatureSlug slug identifying a legislature
58
     *
59
     * @return Legislature
60
     */
61 15
    public function legislature($legislatureSlug)
62
    {
63 15
        foreach ($this->legislatures() as $legislature) {
64 15
            if ($legislature->slug == $legislatureSlug) {
65 14
                return $legislature;
66
            }
67 1
        }
68 3
        throw new Exceptions\NotFoundException("Couldn't find the legislature with slug '$legislatureSlug'");
69
    }
70
}
71