class-wc-connect-service-schemas-validator.php
5.66 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
<?php
if ( ! class_exists( 'WC_Connect_Service_Schemas_Validator' ) ) {
class WC_Connect_Service_Schemas_Validator {
/**
* Validates the overall passed services object (all service types and all services therein)
*
* @param object $services
*
* @return WP_Error|true
*/
public function validate_service_schemas( $service_schemas ) {
if ( ! is_object( $service_schemas ) ) {
return new WP_Error(
'outermost_container_not_object',
'Malformed service schemas. Outermost container is not an object.'
);
}
if ( ! isset( $service_schemas->shipping ) || ! is_array( $service_schemas->shipping ) ) {
return new WP_Error(
'service_type_not_ref_array',
'Malformed service schemas. \'shipping\' does not reference an array.'
);
}
$service_counter = 0;
foreach ( $service_schemas->shipping as $service_schema ) {
if ( ! is_object( $service_schema ) ) {
return new WP_Error(
'service_not_ref_object',
sprintf(
'Malformed service schema. Service type \'shipping\' [%d] does not reference an object.',
$service_counter
)
);
}
$result = $this->validate_service_schema( 'shipping', $service_counter, $service_schema );
if ( is_wp_error( $result ) ) {
return $result;
}
$service_counter ++;
}
if ( ! isset( $service_schemas->boxes ) || ! is_object( $service_schemas->boxes ) ) {
return new WP_Error(
'boxes_not_object',
'Malformed service schemas. \'boxes\' is not an object.'
);
}
return true;
}
/**
* Validates a particular service schema, especially the parts of the service that WC relies
* on like id, method_title, method_description, etc
*
* @param string $service_type
* @param integer $service_counter
* @param object $service
*
* @return WP_Error|true
*/
protected function validate_service_schema( $service_type, $service_counter, $service_schema ) {
$required_properties = array(
'id' => 'string',
'method_description' => 'string',
'method_title' => 'string',
'service_settings' => 'object',
'form_layout' => 'array',
);
foreach ( $required_properties as $required_property => $required_property_type ) {
if ( ! property_exists( $service_schema, $required_property ) ) {
return new WP_Error(
'required_service_property_missing',
sprintf(
'Malformed service schema. Service type \'%s\' [%d] does not include a required \'%s\' property.',
$service_type,
$service_counter,
$required_property
)
);
}
$property_type = gettype( $service_schema->$required_property );
if ( $required_property_type !== $property_type ) {
return new WP_Error(
'required_service_property_wrong_type',
sprintf(
'Malformed service schema. Service type \'%s\' [%d] property \'%s\' is a %s. Was expecting a %s.',
$service_type,
$service_counter,
$service_schema->$required_property,
$property_type,
$required_property_type
)
);
}
}
return $this->validate_service_schema_settings( $service_schema->id, $service_schema->service_settings );
}
/**
* Validates a particular service's service settings schema, especially the parts of the
* service settings that WC relies on like type, required and properties
*
* @param string $service_id
* @param object $service_settings
*
* @return WP_Error|true
*/
protected function validate_service_schema_settings( $service_id, $service_settings ) {
$required_properties = array(
'type' => 'string',
'required' => 'array',
'properties' => 'object',
);
foreach ( $required_properties as $required_property => $required_property_type ) {
if ( ! property_exists( $service_settings, $required_property ) ) {
return new WP_Error(
'service_settings_missing_required_property',
sprintf(
'The settings part of a service schema is malformed. Service \'%s\' service_settings do not include a required \'%s\' property.',
$service_id,
$required_property
)
);
}
$property_type = gettype( $service_settings->$required_property );
if ( $required_property_type !== $property_type ) {
return new WP_Error(
'service_settings_property_wrong_type',
sprintf(
"The settings part of a service schema is malformed. Service '%s' service_setting property '%s' is a %s. Was expecting a %s.",
$service_id,
$required_property,
$property_type,
$required_property_type
)
);
}
}
$result = $this->validate_service_settings_required_properties( $service_id, $service_settings->properties );
if ( is_wp_error( $result ) ) {
return $result;
}
return true;
}
/**
* Validates a particular service's schema's required properties, especially the parts of the
* properties that WC relies on and title
*
* @param string $service_id
* @param object $service_settings_properties
*
* @return WP_Error|true
*/
protected function validate_service_settings_required_properties( $service_id, $service_settings_properties ) {
$required_properties = array(
'title',
);
foreach ( $required_properties as $required_property ) {
if ( ! property_exists( $service_settings_properties, $required_property ) ) {
return new WP_Error(
'service_properties_missing_required_property',
sprintf(
"The properties part of a service schema is malformed. Service '%s' service_settings properties do not include a required '%s' property.",
$service_id,
$required_property
)
);
}
}
return true;
}
}
}