@@ -124,30 +124,6 @@ public static function mapClass(
124124 }
125125 }
126126
127- /**
128- * Try mapping the class onto the value,
129- * If mapping failed due to the invalid oneOf or anyOf types,
130- * throw ApiException
131- *
132- * @param array $json value to be verified against the types
133- * @param string $classname name of the class to map
134- * @param string $namespace namespace name for the model classes, Default: global namespace
135- *
136- * @throws InvalidArgumentException
137- */
138- public static function verifyClass (
139- array $ json ,
140- string $ classname ,
141- string $ namespace = 'Square\Models '
142- ) {
143- try {
144- $ value = empty ($ json ) ? new stdClass () : json_decode (json_encode ($ json ));
145- self ::getJsonMapper ()->mapClass ($ value , "$ namespace \\$ classname " );
146- } catch (Exception $ e ) {
147- throw new InvalidArgumentException ($ e ->getMessage ());
148- }
149- }
150-
151127 /**
152128 * Map the types onto the value,
153129 * If mapping failed due to the invalid oneOf or anyOf types,
@@ -179,35 +155,43 @@ public static function mapTypes(
179155 }
180156
181157 /**
182- * Try mapping the types onto the value,
183- * If mapping failed due to the invalid oneOf or anyOf types,
184- * throw InvalidArgumentException
158+ * Checks if type of the given value is present in the type group, also updates the value when
159+ * $serializationMethods for the value's type are given.
185160 *
186- * @param mixed $value value to be verified against the types
187- * @param string $types types to be mapped in format OneOf(...) or AnyOf(...)
188- * @param string[] $serializationMethods Specify methods required for serialization instead of json_encode,
189- * should be a string path to the accessible method along with the type,
190- * separated by a space.
191- * @param string[] $facMethods Specify if any methods are required to map this value into any type
192- * @param string $namespace namespace name for the model classes, Default: global namespace
161+ * @param mixed $value value to be verified against the types
162+ * @param string $types types to be mapped in format OneOf(...) or AnyOf(...)
163+ * @param string[] $serializationMethods Specify methods required for serialization of specific types in
164+ * in the type group, should be an array in the format:
165+ * ['path/to/method argumentType', ...]. Default: []
193166 *
194167 * @return mixed
195168 * @throws InvalidArgumentException
196169 */
197170 public static function verifyTypes (
198171 $ value ,
199172 string $ types ,
200- array $ serializationMethods = [],
201- array $ facMethods = [],
202- string $ namespace = 'Square\Models '
173+ array $ serializationMethods = []
203174 ) {
204175 try {
205- $ value = self ::applySerializationMethods ($ value , $ serializationMethods );
206- self ::getJsonMapper ()->mapFor (json_decode (json_encode ($ value )), $ types , $ namespace , $ facMethods );
176+ return self ::getJsonMapper ()->checkTypeGroupFor ($ types , $ value , $ serializationMethods );
207177 } catch (Exception $ e ) {
208178 throw new InvalidArgumentException ($ e ->getMessage ());
209179 }
210- return $ value ;
180+ }
181+
182+ /**
183+ * Serialize any given mixed value.
184+ *
185+ * @param mixed $value Any value to be serialized
186+ *
187+ * @return string|null serialized value
188+ */
189+ public static function serialize ($ value ): ?string
190+ {
191+ if (is_string ($ value ) || is_null ($ value )) {
192+ return $ value ;
193+ }
194+ return json_encode ($ value );
211195 }
212196
213197 /**
@@ -235,93 +219,6 @@ public static function checkValueInEnum($value, string $enumName, array $enumVal
235219 }
236220 }
237221
238- /**
239- * Extract type from any given value.
240- *
241- * @param mixed $value should be an array to be checked for inner type
242- * @param string $start string to be appended at the start of the extracted type, Default: ''
243- * @param string $end string to be appended at the end of the extracted type, Default: ''
244- *
245- * @return string Returns the type that could be mapped on the given value.
246- */
247- private static function getType ($ value , string $ start = '' , string $ end = '' ): string
248- {
249- if (is_array ($ value )) {
250- if (self ::isAssociative ($ value )) {
251- // if value is associative array
252- $ start .= 'array<string, ' ;
253- $ end = '> ' . $ end ;
254- } else {
255- // if value is indexed array
256- if (empty ($ value )) {
257- return 'array ' ;
258- }
259- $ end = '[] ' . $ end ;
260- }
261- return self ::getType (array_pop ($ value ), $ start , $ end );
262- } elseif (is_object ($ value )) {
263- $ type = get_class ($ value ); // returns full path of class
264- $ slashPos = strrpos ($ type , '\\' );
265- if ($ slashPos !== false ) {
266- $ slashPos ++; // to get the type after last slash
267- } else {
268- $ slashPos = 0 ; // if did not have any slashes
269- }
270- $ type = substr ($ type , $ slashPos );
271- return $ start . $ type . $ end ;
272- }
273- return $ start . gettype ($ value ) . $ end ;
274- }
275-
276- /**
277- * Apply serialization methods onto any given value.
278- *
279- * @param mixed $value Any value to be serialized
280- * @param string[] $serializationMethods Specify methods required for serialization instead of json_encode,
281- * should be a string path to the accessible method along with the type,
282- * separated by a space.
283- * @return mixed value after applying serialization method if applicable
284- */
285- public static function applySerializationMethods ($ value , array $ serializationMethods )
286- {
287- $ type = empty ($ serializationMethods ) ? null : self ::getType ($ value );
288- $ error = null ;
289- foreach ($ serializationMethods as $ method ) {
290- $ method = explode (' ' , $ method );
291- try {
292- if (is_callable ($ method [0 ]) && $ type == $ method [1 ]) {
293- return call_user_func ($ method [0 ], $ value );
294- }
295- } catch (\Throwable $ e ) {
296- $ error = $ e ;
297- }
298- }
299- if (is_array ($ value )) {
300- return array_map (function ($ v ) use ($ serializationMethods ) {
301- return self ::applySerializationMethods ($ v , $ serializationMethods );
302- }, $ value );
303- }
304- if (isset ($ error )) {
305- throw new InvalidArgumentException ($ error ->getMessage ());
306- }
307- return $ value ;
308- }
309-
310- /**
311- * Serialize any given mixed value.
312- *
313- * @param mixed $value Any value to be serialized
314- *
315- * @return string|null serialized value
316- */
317- public static function serialize ($ value ): ?string
318- {
319- if (is_string ($ value ) || is_null ($ value )) {
320- return $ value ;
321- }
322- return json_encode ($ value );
323- }
324-
325222 /**
326223 * Deserialize a Json string
327224 *
0 commit comments