Passed
Push — master ( 1e77ab...d0f64e )
by Matthew
06:39
created

Person::intId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Mirrors pydantic model for deseralisation in a PHP context.
4
 * For adding display related helper functions.
5
 * @package TheyWorkForYou
6
 */
7
8
declare(strict_types=1);
9
10
namespace MySociety\TheyWorkForYou\DataClass\Regmem;
11
12
use MySociety\TheyWorkForYou\DataClass\BaseModel;
13
14
use InvalidArgumentException;
15
16
class Person extends BaseModel {
17
    public string $chamber;
18
    public string $language;
19
    public string $person_id;
20
    public string $person_name;
21
    public string $published_date;
22
    public CategoryList $categories;
23
24
25
    public function intId(): int {
26
        // extract the last part of the person_id, which is the integer id
27
        $parts = explode('/', $this->person_id);
28
        return (int) end($parts);
29
    }
30
31
    public function allEntryIds(): array {
32
        $entryIds = [];
33
        foreach ($this->categories as $category) {
34
            foreach ($category->entries as $entry) {
35
                $entryIds[] = $entry->id;
36
                foreach ($entry->sub_entries as $subEntry) {
37
                    $entryIds[] = $subEntry->id;
38
                }
39
            }
40
        }
41
        return $entryIds;
42
    }
43
44
45
    public function displayChamber(): string {
46
        switch ($this->chamber) {
47
            case 'house-of-commons':
48
                return 'House of Commons';
49
            case 'welsh-parliament':
50
                return 'Senedd';
51
            case 'scottish-parliament':
52
                return 'Scottish Parliament';
53
            case 'northern-ireland-assembly':
54
                return 'Northern Ireland Assembly';
55
            default:
56
                return 'Unknown Chamber';
57
        }
58
    }
59
60
    public function getCategoryFromId(string $categoryId): Category {
61
        foreach ($this->categories as $category) {
62
            if ($category->category_id === $categoryId) {
63
                return $category;
64
            }
65
        }
66
        throw new InvalidArgumentException("Category $categoryId not found in register");
67
    }
68
}
69