Checkbox.php
1.79 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
<?php
namespace ACA\JetEngine\Search\Comparison;
use AC\Helper\Select\Options;
use ACP;
use ACP\Search\Operators;
use ACP\Search\Value;
class Checkbox extends ACP\Search\Comparison\Meta implements ACP\Search\Comparison\Values {
/**
* @var array
*/
private $choices;
/**
* @var bool
*/
private $value_is_array;
public function __construct( $meta_key, $meta_type, array $choices, $value_is_array ) {
parent::__construct( new Operators( [
Operators::EQ,
Operators::NEQ,
Operators::IS_EMPTY,
Operators::NOT_IS_EMPTY,
] ), $meta_key, $meta_type );
$this->choices = $choices;
$this->value_is_array = (bool) $value_is_array;
}
private function map_default_value( $operator, $value ) {
$serialized_value = serialize( $value->get_value() ) . serialize( $operator === Operators::EQ ? 'true' : 'false' );
return new Value( $serialized_value, $value->get_type() );
}
private function map_array_value( Value $value ) {
return new Value( serialize( $value->get_value() ), $value->get_type() );
}
/**
* @param string $operator
* @param Value $value
*
* @return array
*/
protected function get_meta_query( $operator, Value $value ) {
$operators = [
Operators::EQ => 'LIKE',
Operators::NEQ => 'LIKE',
];
if ( array_key_exists( $operator, $operators ) ) {
$mapped_value = $this->value_is_array ? $this->map_array_value( $value ) : $this->map_default_value( $operator, $value );
$comparison = new ACP\Search\Helper\MetaQuery\Comparison( $this->get_meta_key(), $operators[ $operator ], $mapped_value );
return $comparison();
}
$comparison = ACP\Search\Helper\MetaQuery\ComparisonFactory::create( $this->get_meta_key(), $operator, $value );
return $comparison();
}
public function get_values() {
return Options::create_from_array( $this->choices );
}
}