Completed
Push — master ( 658806...ce8777 )
by Andy
07:01
created

Legislature::popolo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace EveryPolitician\EveryPolitician;
4
5
use \DateTime;
6
use \EveryPolitician\EveryPoliticianPopolo\Popolo;
7
8
class Legislature
9
{
10
    public $name;
11
    public $slug;
12
    public $personCount;
13
    public $sha;
14
    public $statementCount;
15
    public $popoloUrl;
16
    public $lastmod;
17
    protected $legislatureData;
18
    protected $country;
19
20 26
    public function __construct($legislatureData, $country)
21
    {
22
        $propertyMappings = [
23 26
            'name' => 'name',
24 17
            'slug' => 'slug',
25 17
            'person_count' => 'personCount',
26 17
            'sha' => 'sha',
27 17
            'statement_count' => 'statementCount',
28
            'popolo_url' => 'popoloUrl'
29 17
        ];
30 26
        foreach ($propertyMappings as $k => $v) {
31 26
            $this->$v = array_key_exists($k, $legislatureData) ? $legislatureData[$k] : null;
32 17
        }
33 26
        $timestamp = $legislatureData['lastmod'];
34 26
        $this->lastmod = DateTime::createFromFormat('U', $timestamp);
35 26
        $this->legislatureData = $legislatureData;
36 26
        $this->country = $country;
37 26
    }
38
39 3
    public function __toString()
40
    {
41 3
        return '<Legislature: '.$this->name.' in '.$this->country->name.'>';
42
    }
43
44
    /**
45
     * Return the directory path in the everypolitician-data repository
46
     */
47 3
    public function directory()
48
    {
49 3
        $splitPath = explode('/', $this->legislatureData['sources_directory']);
50 3
        $splitPath = array_slice($splitPath, 1, 2);
51 3
        return implode('/', $splitPath);
52
    }
53
54
    public function popolo()
55
    {
56
        return Popolo::fromUrl($this->popoloUrl);
57
    }
58
59
    /**
60
     * Return all the known legislative periods for this legislature
61
     */
62 8
    public function legislativePeriods()
63
    {
64 8
        $legislativePeriods = [];
65 8
        foreach ($this->legislatureData['legislative_periods'] as $lpData) {
66 8
            $legislativePeriods[] = new LegislativePeriod($lpData, $this, $this->country);
67 5
        }
68 8
        return $legislativePeriods;
69
    }
70
}
71