format(\DateTimeInterface::ATOM); } if (is_array($data)) { return array_map([self::class, 'sanitizeForSerialization'], $data); } if (is_object($data)) { $values = []; if (self::isModel($data)) { foreach ($data::openAPITypes() as $property => $type) { $getter = $data::getters()[$property]; $value = $data->$getter(); if (null !== $value || ($data::isNullable($property) && $data->isNullableSetToNull($property)) ) { $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value); } } } else { foreach (get_object_vars($data) as $property => $value) { $values[$property] = self::sanitizeForSerialization($value); } } return (object) $values; } return $data; } /** * Serialize a model into a plain associative array (JSON shape). * Convenience for logging contexts and tests; requests should use * sanitizeForSerialization() directly to preserve empty-object vs * empty-array distinctions. */ public static function toArray(object $model): array { $encoded = wp_json_encode(self::sanitizeForSerialization($model)); return is_string($encoded) ? (array) json_decode($encoded, true) : []; } /** * Used by generated setters to render values in validation messages. */ public static function toString(mixed $value): string { if ($value instanceof \DateTimeInterface) { return $value->format(\DateTimeInterface::ATOM); } if (is_bool($value)) { return $value ? 'true' : 'false'; } return (string) $value; } /** * Hydrate a decoded JSON structure (from json_decode, assoc or object) * into a generated model, an array of models, or a primitive. * * Supports the type strings used in the generated models' openAPITypes(): * primitives, "Type[]" lists, and fully qualified model class names. * File and binary types are intentionally not supported. * * @template T * @param mixed $data decoded JSON value * @param string $type target type, e.g. 'int' or '\Fqcn\Model\Order[]' * @return mixed */ public static function deserialize(mixed $data, string $type): mixed { if (null === $data) { return null; } if (str_ends_with($type, '[]')) { $itemType = substr($type, 0, -2); $values = is_object($data) ? get_object_vars($data) : (array) $data; return array_map( static fn ($item) => self::deserialize($item, $itemType), $values ); } switch ($type) { case 'string': return (string) $data; case 'int': case 'integer': return (int) $data; case 'float': case 'double': case 'number': return (float) $data; case 'bool': case 'boolean': return (bool) $data; case 'mixed': case 'object': return $data; } $class = ltrim($type, '\\'); if (! class_exists($class) || ! method_exists($class, 'attributeMap')) { return $data; } $values = is_object($data) ? get_object_vars($data) : $data; if (! is_array($values)) { return $data; } $instance = new $class(); $types = $class::openAPITypes(); $setters = $class::setters(); foreach ($class::attributeMap() as $property => $jsonKey) { if (! array_key_exists($jsonKey, $values)) { continue; } $setter = $setters[$property]; $instance->$setter(self::deserialize($values[$jsonKey], $types[$property])); } return $instance; } /** * Generated models from any sub-namespace (Commerce, Contacts) implement * their own package-local ModelInterface, so detect them structurally. */ private static function isModel(object $data): bool { return method_exists($data, 'openAPITypes') && method_exists($data, 'attributeMap') && method_exists($data, 'isNullableSetToNull'); } }