Comparison.php
1.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace ACP\Search\RequestHandler;
use AC;
use AC\Exception;
use AC\ListScreenFactory;
use AC\ListScreenRepository\Storage;
use AC\Request;
use AC\Response;
use AC\Type\ListScreenId;
use ACP\Controller;
use ACP\Search;
use ACP\Search\Searchable;
use DomainException;
class Comparison extends Controller {
/**
* @var AC\ListScreen;
*/
protected $list_screen;
public function __construct( Storage $storage, Request $request, ListScreenFactory $list_screen_factory ) {
parent::__construct( $request );
$id = $request->get( 'layout' );
$list_key = (string) $request->get( 'list_screen', '' );
if ( ListScreenId::is_valid_id( $id ) ) {
$this->list_screen = $storage->find( new ListScreenId( $id ) );
} else if ( $list_key && $list_screen_factory->can_create( $list_key ) ) {
$this->list_screen = $list_screen_factory->create( $list_key );
}
if ( ! $this->list_screen instanceof AC\ListScreen ) {
throw Exception\RequestException::parameters_invalid();
}
}
public function get_options_action() {
$response = new Response\Json();
$column_name = (string) $this->request->filter( 'column', null, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$column = $this->list_screen->get_column_by_name( $column_name );
if ( ! $column instanceof Searchable ) {
$response->error();
}
$comparison = $column->search();
switch ( true ) {
case $comparison instanceof Search\Comparison\RemoteValues :
$options = $comparison->get_values();
$has_more = false;
break;
case $comparison instanceof Search\Comparison\SearchableValues :
$options = $comparison->get_values(
$this->request->filter( 'searchterm' ),
$this->request->filter( 'page', null, FILTER_SANITIZE_NUMBER_INT )
);
$has_more = ! $options->is_last_page();
break;
default :
throw new DomainException( 'Invalid Comparison type found.' );
}
$select = new AC\Helper\Select\Response( $options, $has_more );
$response
->set_parameters( $select() )
->success();
}
}