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
|
|
|
|