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']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* created at |
29
|
|
|
* @param int $createdAt |
30
|
|
|
*/ |
31
|
|
|
protected $createdAt = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* get fields |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
abstract protected function getFields(): array; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* set created at |
42
|
|
|
* |
43
|
|
|
* @param int $createdAt |
44
|
|
|
* |
45
|
|
|
* @return this |
46
|
|
|
*/ |
47
|
9 |
|
public function setCreatedAt($createdAt) |
48
|
|
|
{ |
49
|
9 |
|
$this->createdAt = $createdAt; |
50
|
|
|
|
51
|
9 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* get created at |
56
|
|
|
* |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
9 |
|
public function getCreatedAt(): int |
60
|
|
|
{ |
61
|
9 |
|
return $this->createdAt; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* to json |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
9 |
|
public function toJson(): string |
70
|
|
|
{ |
71
|
9 |
|
return json_encode($this->toRequestParameters()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* to query string |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
9 |
|
public function toQueryString(): string |
80
|
|
|
{ |
81
|
9 |
|
return http_build_query($this->toRequestParameters()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* to request parameters |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
16 |
|
protected function toRequestParameters(): array |
90
|
|
|
{ |
91
|
16 |
|
$requestParameters = []; |
92
|
|
|
|
93
|
16 |
|
foreach (array_merge($this->getFields(), self::DEFAULT_FIELDS) as $field) { |
94
|
16 |
|
if (!is_null($value = $this->$field)) { |
95
|
16 |
|
$requestParameters[$this->toSnakeCase($field)] = $value; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
16 |
|
return $requestParameters; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* to snake case |
104
|
|
|
* |
105
|
|
|
* @param string $string |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
16 |
|
protected function toSnakeCase(string $string): string |
110
|
|
|
{ |
111
|
16 |
|
return mb_strtolower(preg_replace('/(.)(?=[A-Z])/', '$1_', $string)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|