1
|
|
|
<?php |
2
|
|
|
namespace Kwkm\MkLiveStatusClient; |
3
|
|
|
|
4
|
|
|
use \InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Lql Object |
8
|
|
|
* |
9
|
|
|
* @package Kwkm\MkLiveStatusClient |
10
|
|
|
* @author Takehiro Kawakami <[email protected]> |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
class LqlObject |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Lql query |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $queries; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Acquisition table |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $table; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Header |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $headers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Acquisition column |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $columns; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Output Format type |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $outputFormat; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Authentication username |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $authUser; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Acquisition number |
53
|
|
|
* @var integer; |
54
|
|
|
*/ |
55
|
|
|
protected $limit; |
56
|
|
|
|
57
|
57 |
|
public function __construct() |
58
|
|
|
{ |
59
|
57 |
|
$this->reset(); |
60
|
57 |
|
} |
61
|
|
|
|
62
|
43 |
|
public function setTable($table) |
63
|
|
|
{ |
64
|
43 |
|
if (!is_string($table)) { |
65
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
66
|
|
|
} |
67
|
|
|
|
68
|
42 |
|
$this->table = trim($table); |
69
|
42 |
|
} |
70
|
|
|
|
71
|
24 |
|
public function setColumns($columns) |
72
|
|
|
{ |
73
|
24 |
|
if (!is_array($columns)) { |
74
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be an array."); |
75
|
|
|
} |
76
|
23 |
|
$this->columns = $columns; |
77
|
23 |
|
} |
78
|
|
|
|
79
|
2 |
|
public function appendColumns($column) |
80
|
|
|
{ |
81
|
2 |
|
if (!is_string($column)) { |
82
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
83
|
|
|
} |
84
|
1 |
|
$this->columns[] = trim($column); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
33 |
|
public function appendArrayQuery(array $array) |
88
|
|
|
{ |
89
|
33 |
|
$this->queries = array_merge($this->queries, $array); |
90
|
33 |
|
} |
91
|
|
|
|
92
|
2 |
|
public function appendStringQuery($name, $value) |
93
|
|
|
{ |
94
|
2 |
|
if (!is_string($value)) { |
95
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
96
|
|
|
} |
97
|
1 |
|
$this->queries[] = sprintf("%s: %s\n", $name, trim($value)); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
2 |
|
public function appendIntegerQuery($name, $value) |
101
|
|
|
{ |
102
|
2 |
|
if (!is_int($value)) { |
103
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be an integer."); |
104
|
|
|
} |
105
|
1 |
|
$this->queries[] = sprintf("%s: %d\n", $name, $value); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function appendNoValueQuery($name) |
109
|
|
|
{ |
110
|
1 |
|
$this->queries[] = sprintf("%s:\n", $name); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
3 |
|
public function appendParameter($parameter) |
114
|
|
|
{ |
115
|
3 |
|
if (!is_string($parameter)) { |
116
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
117
|
|
|
} |
118
|
2 |
|
if (trim($parameter) !== "") { |
119
|
2 |
|
$this->queries[] = $this->formatEnding($parameter); |
120
|
2 |
|
} |
121
|
2 |
|
} |
122
|
|
|
|
123
|
57 |
|
public function setHeader($boolean) |
124
|
|
|
{ |
125
|
57 |
|
if (!is_bool($boolean)) { |
126
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a boolean."); |
127
|
|
|
} |
128
|
57 |
|
if ($boolean === true) { |
129
|
57 |
|
$this->headers = "ColumnHeaders: on\n"; |
130
|
57 |
|
} else { |
131
|
1 |
|
$this->headers = "ColumnHeaders: off\n"; |
132
|
|
|
} |
133
|
57 |
|
} |
134
|
|
|
|
135
|
57 |
|
public function setOutputFormat($outputFormat) |
136
|
|
|
{ |
137
|
57 |
|
if (!is_string($outputFormat)) { |
138
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
139
|
|
|
} |
140
|
57 |
|
$this->outputFormat = sprintf("OutputFormat: %s\n", $outputFormat); |
141
|
57 |
|
} |
142
|
|
|
|
143
|
2 |
|
public function setLimit($limit) |
144
|
|
|
{ |
145
|
2 |
|
if (!is_int($limit)) { |
146
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be an integer."); |
147
|
|
|
} |
148
|
1 |
|
$this->limit = sprintf("Limit: %d\n", $limit); |
149
|
1 |
|
} |
150
|
|
|
|
151
|
3 |
|
public function setAuthUser($authUser) |
152
|
|
|
{ |
153
|
3 |
|
if (!is_string($authUser)) { |
154
|
1 |
|
throw new InvalidArgumentException("Argument 1 must be a string."); |
155
|
|
|
} |
156
|
2 |
|
$this->authUser = sprintf("AuthUser: %s\n", trim($authUser)); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 初期化 |
161
|
|
|
* |
162
|
|
|
* @return \Kwkm\MkLiveStatusClient\Lql |
163
|
|
|
*/ |
164
|
57 |
|
public function reset() |
165
|
|
|
{ |
166
|
57 |
|
$this->queries = array(); |
167
|
57 |
|
$this->table = null; |
168
|
57 |
|
$this->columns = array(); |
169
|
57 |
|
$this->authUser = ''; |
170
|
57 |
|
$this->limit = ''; |
171
|
|
|
|
172
|
57 |
|
$this->setHeader(true); |
173
|
57 |
|
$this->setOutputFormat('json'); |
174
|
57 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 終端の改行文字の付与 |
178
|
|
|
* @param string $string 改行文字を付与する文字列 |
179
|
|
|
* @return string 改行文字が付与された文字列 |
180
|
|
|
*/ |
181
|
2 |
|
protected function formatEnding($string) |
182
|
|
|
{ |
183
|
2 |
|
if ($string[strlen($string) - 1] !== "\n") { |
184
|
2 |
|
$string .= "\n"; |
185
|
2 |
|
} |
186
|
|
|
|
187
|
2 |
|
return $string; |
188
|
|
|
} |
189
|
|
|
|
190
|
40 |
|
public function build() |
191
|
|
|
{ |
192
|
40 |
|
$request = sprintf("GET %s\n", $this->table) |
193
|
40 |
|
. $this->getColumnsField() |
194
|
40 |
|
. $this->getQueriesFiled() |
195
|
40 |
|
. $this->getOutputFormatFiled() |
196
|
40 |
|
. $this->authUser |
197
|
40 |
|
. $this->limit |
198
|
40 |
|
. "ResponseHeader: fixed16\n" |
199
|
40 |
|
. "\n"; |
200
|
|
|
|
201
|
40 |
|
return $request; |
202
|
|
|
} |
203
|
|
|
|
204
|
40 |
|
private function getColumnsField() |
205
|
|
|
{ |
206
|
40 |
|
if (count($this->columns) !== 0) { |
207
|
23 |
|
return sprintf("Columns: %s\n", implode(' ', $this->columns)) . $this->headers; |
208
|
|
|
} |
209
|
|
|
|
210
|
17 |
|
return ''; |
211
|
|
|
} |
212
|
|
|
|
213
|
40 |
|
private function getQueriesFiled() |
214
|
|
|
{ |
215
|
40 |
|
if (!is_null($this->queries)) { |
216
|
40 |
|
return implode('', $this->queries); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
40 |
|
private function getOutputFormatFiled() |
223
|
|
|
{ |
224
|
40 |
|
if (!is_null($this->outputFormat)) { |
225
|
40 |
|
return $this->outputFormat; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return ''; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|