1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the KongAdminApi package. |
5
|
|
|
* |
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* abstract document |
16
|
|
|
* |
17
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractDocument implements DocumentInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* default fields |
23
|
|
|
* @const array DEFAULT_FIELDS |
24
|
|
|
*/ |
25
|
|
|
const DEFAULT_FIELDS = ['createdAt', 'size', 'offset']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* created at |
29
|
|
|
* @param int $createdAt |
30
|
|
|
*/ |
31
|
|
|
protected $createdAt = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* size |
35
|
|
|
* @param int $size |
36
|
|
|
*/ |
37
|
|
|
protected $size = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* offset |
41
|
|
|
* @param string $offset |
42
|
|
|
*/ |
43
|
|
|
protected $offset = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* get fields |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
abstract protected function getFields(): array; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* set created at |
54
|
|
|
* |
55
|
|
|
* @param int $createdAt |
56
|
|
|
* |
57
|
|
|
* @return this |
58
|
|
|
*/ |
59
|
9 |
|
public function setCreatedAt(int $createdAt) |
60
|
|
|
{ |
61
|
9 |
|
$this->createdAt = $createdAt; |
62
|
|
|
|
63
|
9 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* get created at |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
9 |
|
public function getCreatedAt(): int |
72
|
|
|
{ |
73
|
9 |
|
return $this->createdAt; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* set size |
78
|
|
|
* |
79
|
|
|
* @param int $size |
80
|
|
|
* |
81
|
|
|
* @return this |
82
|
|
|
*/ |
83
|
9 |
|
public function setSize(int $size) |
84
|
|
|
{ |
85
|
9 |
|
$this->size = $size; |
86
|
|
|
|
87
|
9 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* get size |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
9 |
|
public function getSize(): int |
96
|
|
|
{ |
97
|
9 |
|
return $this->size; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* set offset |
102
|
|
|
* |
103
|
|
|
* @param string $offset |
104
|
|
|
* |
105
|
|
|
* @return this |
106
|
|
|
*/ |
107
|
9 |
|
public function setOffset(string $offset) |
108
|
|
|
{ |
109
|
9 |
|
$this->offset = $offset; |
110
|
|
|
|
111
|
9 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* get offset |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
9 |
|
public function getOffset(): string |
120
|
|
|
{ |
121
|
9 |
|
return $this->offset; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* to json |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
9 |
|
public function toJson(): string |
130
|
|
|
{ |
131
|
9 |
|
return json_encode($this->toRequestParameters()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* to query string |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
9 |
|
public function toQueryString(): string |
140
|
|
|
{ |
141
|
9 |
|
return http_build_query($this->toRequestParameters()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* to request parameters |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
16 |
|
protected function toRequestParameters(): array |
150
|
|
|
{ |
151
|
16 |
|
$requestParameters = []; |
152
|
|
|
|
153
|
16 |
|
foreach (array_merge($this->getFields(), self::DEFAULT_FIELDS) as $field) { |
154
|
16 |
|
if (!is_null($value = $this->$field)) { |
155
|
16 |
|
$requestParameters[$this->toSnakeCase($field)] = $value; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
16 |
|
return $requestParameters; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* to snake case |
164
|
|
|
* |
165
|
|
|
* @param string $string |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
16 |
|
protected function toSnakeCase(string $string): string |
170
|
|
|
{ |
171
|
16 |
|
return mb_strtolower(preg_replace('/(.)(?=[A-Z])/', '$1_', $string)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|