Completed
Push — 2.0-dev ( 0f31c9...13891e )
by Takehiro
03:05
created

LqlObject::setColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
    public function __construct()
58
    {
59
        $this->reset();
60
    }
61
62
    public function setTable($table)
63
    {
64
        if (!is_string($table)) {
65
            throw new InvalidArgumentException("Argument 1 must be a string.");
66
        }
67
68
        $this->table = trim($table);
69
    }
70
71
    public function setColumns($columns)
72
    {
73
        if (!is_array($columns)) {
74
            throw new InvalidArgumentException("Argument 1 must be an array.");
75
        }
76
        $this->columns = $columns;
77
    }
78
79
    public function appendColumns($column)
80
    {
81
        if (!is_string($column)) {
82
            throw new InvalidArgumentException("Argument 1 must be a string.");
83
        }
84
        $this->columns[] = trim($column);
85
    }
86
87
    public function appendArrayQuery($array)
88
    {
89
        if (!is_string($array)) {
90
            throw new InvalidArgumentException("Argument 1 must be a array.");
91
        }
92
        $this->queries = array_merge($this->queries, $array);
93
    }
94
95
    public function appendStringQuery($name, $value)
96
    {
97
        if (!is_string($value)) {
98
            throw new InvalidArgumentException("Argument 1 must be a string.");
99
        }
100
        $this->queries[] = sprintf("%s: %s\n", $name, trim($value));
101
    }
102
103
    public function appendIntegerQuery($name, $value)
104
    {
105
        if (!is_int($value)) {
106
            throw new InvalidArgumentException("Argument 1 must be an integer.");
107
        }
108
        $this->queries[] = sprintf("%s: %d\n", $name, $value);
109
    }
110
111
    public function appendNoValueQuery($name)
112
    {
113
        $this->queries[] = sprintf("%s:\n", $name);
114
    }
115
116
    public function appendParameter($parameter)
117
    {
118
        if (!is_string($parameter)) {
119
            throw new InvalidArgumentException("Argument 1 must be a string.");
120
        }
121
        if (trim($parameter) !== "") {
122
            $this->queries[] = $this->formatEnding($parameter);
123
        }
124
    }
125
126
    public function setHeader($boolean)
127
    {
128
        if (!is_bool($boolean)) {
129
            throw new InvalidArgumentException("Argument 1 must be a boolean.");
130
        }
131
        if ($boolean === true) {
132
            $this->headers = "ColumnHeaders: on\n";
133
        } else {
134
            $this->headers = "ColumnHeaders: off\n";
135
        }
136
    }
137
138
    public function setOutputFormat($outputFormat)
139
    {
140
        if (!is_string($outputFormat)) {
141
            throw new InvalidArgumentException("Argument 1 must be a string.");
142
        }
143
        $this->outputFormat = sprintf("OutputFormat: %s\n", $outputFormat);
144
    }
145
146
    public function setLimit($limit)
147
    {
148
        if (!is_int($limit)) {
149
            throw new InvalidArgumentException("Argument 1 must be an integer.");
150
        }
151
        $this->limit = sprintf("Limit: %d\n", $limit);
152
    }
153
154
    public function setAuthUser($authUser)
155
    {
156
        if (!is_string($authUser)) {
157
            throw new InvalidArgumentException("Argument 1 must be a string.");
158
        }
159
        $this->authUser = sprintf("AuthUser: %s\n", trim($authUser));
160
    }
161
162
    /**
163
     * 初期化
164
     *
165
     * @return \Kwkm\MkLiveStatusClient\Lql
166
     */
167
    public function reset()
168
    {
169
        $this->queries = array();
170
        $this->table = null;
171
        $this->columns = array();
172
        $this->authUser = '';
173
        $this->limit = '';
174
175
        $this->setHeader(true);
176
        $this->setOutputFormat('json');
177
    }
178
179
    /**
180
     * 終端の改行文字の付与
181
     * @param string $string 改行文字を付与する文字列
182
     * @return string           改行文字が付与された文字列
183
     */
184
    protected function formatEnding($string)
185
    {
186
        if ($string[strlen($string) - 1] !== "\n") {
187
            $string .= "\n";
188
        }
189
190
        return $string;
191
    }
192
193
    public function build()
194
    {
195
        $request = sprintf("GET %s\n", $this->table)
196
            . $this->getColumnsField()
197
            . $this->getQueriesFiled()
198
            . $this->getOutputFormatFiled()
199
            . $this->authUser
200
            . $this->limit
201
            . "ResponseHeader: fixed16\n"
202
            . "\n";
203
204
        return $request;
205
    }
206
207
    private function getColumnsField()
208
    {
209
        if (count($this->columns) !== 0) {
210
            return sprintf("Columns: %s\n", implode(' ', $this->columns)) . $this->headers;
211
        }
212
213
        return '';
214
    }
215
216
    private function getQueriesFiled()
217
    {
218
        if (!is_null($this->queries)) {
219
            return implode('', $this->queries);
220
        }
221
222
        return '';
223
    }
224
225
    private function getOutputFormatFiled()
226
    {
227
        if (!is_null($this->outputFormat)) {
228
            return $this->outputFormat;
229
        }
230
231
        return '';
232
    }
233
}