|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MySociety\TheyWorkForYou\DataClass\Regmem; |
|
6
|
|
|
|
|
7
|
|
|
use MySociety\TheyWorkForYou\DataClass\BaseModel; |
|
8
|
|
|
use MySociety\TheyWorkForYou\DataClass\DataFrame; |
|
9
|
|
|
|
|
10
|
|
|
class Detail extends BaseModel { |
|
11
|
|
|
public string $source; |
|
12
|
|
|
public ?string $slug = null; |
|
13
|
|
|
public ?string $display_as = null; |
|
14
|
|
|
public ?string $common_key = null; |
|
15
|
|
|
public ?string $description = null; |
|
16
|
|
|
public ?string $type = null; |
|
17
|
|
|
public $value = null; |
|
18
|
|
|
public AnnotationList $annotations; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
public function has_value(): bool { |
|
22
|
|
|
// if not null or empty string when removing trailing whitespace |
|
23
|
|
|
|
|
24
|
|
|
// if string, trim and return false if empty |
|
25
|
|
|
if (is_string($this->value)) { |
|
26
|
|
|
return trim($this->value) !== ''; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $this->value !== null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function as_df(): DataFrame { |
|
34
|
|
|
// convert group of sub-details to a DataFrame |
|
35
|
|
|
if ($this->type !== 'container') { |
|
36
|
|
|
throw new \Exception("Cannot convert non-container detail to DataFrame"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$rows = []; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($this->value as $details_array) { |
|
42
|
|
|
$row = []; |
|
43
|
|
|
foreach ($details_array as $detail) { |
|
44
|
|
|
$row[$detail["display_as"]] = $detail["value"]; |
|
45
|
|
|
} |
|
46
|
|
|
$rows[] = $row; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return new DataFrame($rows); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Iterator<Detail>| |
|
54
|
|
|
*/ |
|
55
|
|
|
public function sub_details(): \Iterator { |
|
56
|
|
|
// iterate through all subdetails under a detail |
|
57
|
|
|
$items = new \ArrayIterator(); |
|
58
|
|
|
|
|
59
|
|
|
if (!$this->type === 'container') { |
|
|
|
|
|
|
60
|
|
|
return $items; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->value as $detail_group) { |
|
65
|
|
|
foreach ($detail_group as $detail) { |
|
66
|
|
|
$detail_obj = self::fromArray($detail); |
|
67
|
|
|
$items[] = $detail_obj; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $items; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|