blob: a04acd110cbd449ec2819a93d493fb63bb1fb7a9 (
plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?php
namespace SMW\Elastic\QueryEngine;
/**
* @license GNU GPL v2+
* @since 3.0
*
* @author mwjames
*/
class Condition {
const TYPE_MUST = 'must';
const TYPE_SHOULD = 'should';
const TYPE_MUST_NOT = 'must_not';
const TYPE_FILTER = 'filter';
/**
* @var array
*/
private $parameters = [];
/**
* @var array
*/
private $logs = [];
/**
* @var string
*/
private $type = 'must';
/**
* @since 3.0
*
* @param Condition|array $parameters
*/
public function __construct( $parameters = [] ) {
$this->parameters = $parameters;
}
/**
* @since 3.0
*
* @param string $type
*/
public function type( $type ) {
$this->type = $type;
}
/**
* @since 3.0
*
* @return string
*/
public function getType() {
return $this->type;
}
/**
* @since 3.0
*
* @param mixed $log
*/
public function log( $log ) {
$this->logs[] = $log;
}
/**
* @since 3.0
*
* @return array
*/
public function getLogs() {
return $this->logs;
}
/**
* @since 3.0
*
* @return array
*/
public function toArray() {
$params = $this->params( $this->parameters, $this->logs );
if ( $this->type === '' || $this->type === null || $params === [] ) {
return $params;
}
return [ 'bool' => [ $this->type => $params ] ];
}
private function params( $params, &$logs ) {
$condition = $params;
if ( $condition instanceof Condition ) {
$params = $condition->toArray();
if ( ( $rlogs = $condition->getLogs() ) !== [] ) {
$logs[] = $rlogs;
}
}
if ( !is_array( $params ) ) {
return $params;
}
foreach ( $params as $k => $condition ) {
$params[$k] = $this->params( $condition, $logs );
}
return $params;
}
/**
* @since 3.0
*
* @return string
*/
public function __toString() {
return json_encode( $this->toArray(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
}
|