-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-entity.php
More file actions
212 lines (184 loc) · 4.67 KB
/
Copy pathclass-entity.php
File metadata and controls
212 lines (184 loc) · 4.67 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* The admin-specific functionality of the plugin.
*
* @package Enable_Mastodon_Apps
*/
namespace Enable_Mastodon_Apps\Entity;
use Enable_Mastodon_Apps\Mastodon_API;
/**
* Class Entity
*/
abstract class Entity implements \JsonSerializable {
/**
* The types of variables that are expected.
*
* Example:
* ```
* protected $types = array(
* 'id' => 'string',
* 'created_at' => 'DateTime', // DateTime is the PHP class.
* 'text' => 'string',
* 'language' => 'string?', // ? = Optional.
* 'in_reply_to_id' => 'string??', // ?? = Nullable.
* );
*
* @var array
*/
protected $types;
/**
* Provide the data that should be serialized as JSON.
*
* The annotation is necessary for PHP 5.6 compatibility, when we are PHP 7+, we need to add mixed as the return type.
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
$array = array();
foreach ( $this->types as $var => $type ) {
if ( is_wp_error( $this->$var ) ) {
continue;
}
$object = rtrim( $type, '?' );
$nullable = preg_match( '/\?\?$/', $type );
$optional = preg_match( '/\?$/', $type );
if ( ! isset( $this->$var ) && $nullable ) {
$array[ $var ] = null;
continue;
}
if ( in_array( $object, array( 'string', 'int', 'bool', 'array' ) ) ) {
if ( ! isset( $this->$var ) ) {
if ( $optional ) {
continue;
}
return array(
'error' => 'Required variable is missing: ' . $var,
);
}
$valid = $this->validate( $var );
if ( is_wp_error( $valid ) ) {
return array(
'error' => $var . ': ' . $valid->get_error_message(),
);
}
settype( $this->$var, $object );
$array[ $var ] = $this->__get( $var );
continue;
}
if ( 'DateTime' === $object ) {
if ( $this->__get( $var ) instanceof \DateTime ) {
$array[ $var ] = preg_replace( '/\+00:00$/', 'Z', $this->__get( $var )->format( 'Y-m-d\TH:i:s.000P' ) );
continue;
}
if ( $optional ) {
continue;
}
if ( $this->__get( $var ) ) {
return array(
'error' => 'Expected DateTime for ' . $var,
);
}
return array(
'error' => 'Required DateTime is missing: ' . $var,
);
}
if ( 'Date' === $object ) {
if ( $this->__get( $var ) instanceof \DateTime ) {
$array[ $var ] = $this->__get( $var )->format( 'Y-m-d' );
continue;
}
if ( $optional ) {
continue;
}
if ( $this->__get( $var ) ) {
return array(
'error' => 'Expected DateTime: ' . $var,
);
}
return array(
'error' => 'Required DateTime is missing: ' . $var,
);
}
if ( preg_match( '/array\[([^\]]+)\]/', $object, $matches ) ) {
$object = $matches[1];
if ( substr( $object, -1 ) === '?' ) {
$object = rtrim( $object, '?' );
$skippable = true;
}
if ( ! class_exists( '\\Enable_Mastodon_Apps\\Entity\\' . $object ) ) {
return array(
'error' => 'Invalid object type: ' . $object,
);
}
if ( ! is_array( $this->$var ) ) {
if ( $optional ) {
continue;
}
return array(
'error' => 'Required object is missing: ' . $var,
);
}
$array[ $var ] = array();
foreach ( $this->__get( $var ) as $key => $value ) {
if ( $value instanceof Entity ) {
$array[ $var ][ $key ] = $value->jsonSerialize();
if ( isset( $array[ $var ][ $key ]['error'] ) ) {
if ( $skippable ) {
unset( $array[ $var ][ $key ] );
continue;
}
return array(
'error' => $key . ': ' . $array[ $var ][ $key ]['error'],
);
}
continue;
}
}
$array[ $var ] = array_values( $array[ $var ] );
continue;
}
if ( class_exists( '\\Enable_Mastodon_Apps\\Entity\\' . $object ) ) {
if ( $this->$var instanceof Entity ) {
$array[ $var ] = $this->__get( $var )->jsonSerialize();
if ( isset( $array[ $var ]['error'] ) ) {
return $array[ $var ];
}
continue;
}
if ( $optional ) {
continue;
}
return array(
'error' => 'Required object is missing: ' . $var,
);
}
}
return $array;
}
public function is_valid() {
$array = $this->jsonSerialize();
if ( empty( $array['error'] ) ) {
return true;
}
Mastodon_API::set_last_error( get_called_class() . ': ' . $array['error'] );
return false;
}
public function __get( $name ) {
return $this->$name;
}
/**
* Validate the variable.
*
* @param string $key The variable to validate.
*
* @return bool|\WP_Error
*/
public function validate( $key ) {
if ( isset( $this->$key ) ) {
return true;
}
// We don't make this decision here.
return true;
}
}