Completed
Push — master ( c1b78a...571de1 )
by Andy
02:15
created

Country::__construct()   A

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 27
    public function __construct($countryData)
14
    {
15 27
        $properties = ['name', 'code', 'slug'];
16 27
        foreach ($properties as $k) {
17 27
            $this->$k = $countryData[$k];
18 18
        }
19 27
        $this->countryData = $countryData;
20 27
    }
21
22 3
    public function __toString()
23
    {
24 3
        return '<Country: '.$this->name.'>';
25
    }
26
27
    /**
28
     * Return all the legislatures known for this country
29
     *
30
     * A legislature is a chamber of a parliament, e.g. the House of
31
     * Commons in the UK.
32
     */
33 6
    public function legislatures()
34
    {
35 6
        $legislatures = [];
36 6
        $legislaturesData = $this->countryData['legislatures'];
37 6
        foreach ($legislaturesData as $legislatureData) {
38 6
            $legislatures[] = new Legislature($legislatureData, $this);
39 4
        }
40 6
        return $legislatures;
41
    }
42
43 6
    public function legislature($legislatureSlug)
44
    {
45 6
        foreach ($this->legislatures() as $l) {
46 6
            if ($l->slug == $legislatureSlug) {
47 4
                return $l;
48
            }
49 2
        }
50 3
        throw new Exceptions\NotFoundException("Couldn't find the legislature with slug '$legislatureSlug'");
51
    }
52
}
53