Response.php
963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace AC\Helper\Select;
final class Response {
/**
* @var Options
*/
private $options;
/**
* @var bool
*/
private $more;
public function __construct( Options $options, bool $more = false ) {
$this->options = $options;
$this->more = $more;
}
private function parse_options( array $options ): array {
$results = [];
foreach ( $options as $option ) {
switch ( true ) {
case $option instanceof OptionGroup:
$results[] = [
'text' => $option->get_label(),
'children' => $this->parse_options( $option->get_options() ),
];
break;
case $option instanceof Option:
$results[] = [
'id' => $option->get_value(),
'text' => $option->get_label(),
];
break;
}
}
return $results;
}
public function __invoke() {
return [
'results' => $this->parse_options( $this->options->get_copy() ),
'pagination' => [
'more' => $this->more,
],
];
}
}