Debian Codegraph @

includes/libs/telemetry/OtlpSerializer.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Telemetry;
3
4 /**
5 * Utility class for serializing data in OTLP JSON format.
6 *
7 * @since 1.43
8 * @internal
9 */
10 class OtlpSerializer {
11 /**
12 * Map of PHP types to their corresponding type name used in the OTLP JSON format.
13 */
14 private const TYPE_MAP = [
15 'string' => 'stringValue',
16 'integer' => 'intValue',
17 'boolean' => 'boolValue',
18 'double' => 'doubleValue',
19 'array' => 'arrayValue'
20 ];
21
22 /**
23 * Serialize an associative array into the format expected by the OTLP JSON format.
24 * @param array $keyValuePairs The associative array to serialize
25 * @return array
26 */
27 public static function serializeKeyValuePairs( array $keyValuePairs ): array {
28 $serialized = [];
29
30 foreach ( $keyValuePairs as $key => $value ) {
31 $type = gettype( $value );
32
33 if ( isset( self::TYPE_MAP[$type] ) ) {
34 $serialized[] = [
35 'key' => $key,
36 'value' => [ self::TYPE_MAP[$type] => $value ]
37 ];
38 }
39 }
40
41 return $serialized;
42 }
43 }