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
|
<?php /** * Class Event. * * @package AmpProject\AmpWP */
namespace AmpProject\AmpWP\Instrumentation;
use AmpProject\AmpWP\Exception\InvalidEventProperties;
/** * A server-timing event. * * @package AmpProject\AmpWP * @since 2.0 * @internal */ class Event {
/** * Event name. * * @var string */ protected $name;
/** * Event description. * * @var string|null */ protected $description;
/** * Additional properties of the event. * * @var string[] */ protected $properties;
/** * Event constructor. * * @param string $name Event name. * @param string|null $description Optional. Event description. * @param string[]|null $properties Optional. Additional properties for the * event. */ public function __construct( $name, $description = null, $properties = [] ) { $this->name = $name; $this->description = $description; $this->properties = (array) $properties; }
/** * Get the name of the event. * * @return string Event name. */ public function get_name() { return $this->name; }
/** * Get the description of the event. * * @return string Event description. */ public function get_description() { return $this->description ?: ''; }
/** * Add additional properties to the event. * * @param string[] $properties Properties to add. * @throws InvalidEventProperties When the type of $properties or its * elements is off. */ public function add_properties( $properties ) { if ( ! is_array( $properties ) ) { throw InvalidEventProperties::from_invalid_type( $properties ); }
foreach ( $properties as $key => $value ) { if ( ! is_string( $key ) ) { throw InvalidEventProperties::from_invalid_element_key_type( $key ); }
if ( ! is_scalar( $value ) ) { throw InvalidEventProperties::from_invalid_element_value_type( $value ); }
$this->properties[ $key ] = $value; } }
/** * Get the server timing header string. * * @return string Server timing header string representing this event. */ public function get_header_string() { $property_strings = [];
foreach ( $this->properties as $property => $value ) { if ( is_float( $value ) ) { $property_strings[] = sprintf( ';%s="%.1f"', addslashes( $property ), $value ); } else { $property_strings[] = sprintf( ';%s="%s"', addslashes( $property ), addslashes( $value ) ); } }
$event_string = addslashes( $this->get_name() );
$description = $this->get_description(); if ( ! empty( $description ) ) { $event_string = sprintf( '%s;desc="%s"', $event_string, addslashes( $description ) ); }
return $event_string . implode( $property_strings ); } }
|