-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathContext.php
190 lines (169 loc) · 4.25 KB
/
Context.php
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
<?php
declare(strict_types=1);
namespace Sentry\Context;
/**
* This class stores generic information that will be attached to an event
* being sent.
*
* @author Stefano Arlandini <[email protected]>
*
* @psalm-template T
*
* @template-implements \ArrayAccess<string, T>
* @template-implements \IteratorAggregate<string, T>
*/
class Context implements \ArrayAccess, \JsonSerializable, \IteratorAggregate
{
/**
* This constant defines the alias used for the user context.
*
* @deprecated To be removed in 3.0 because unused
*/
public const CONTEXT_USER = 'user';
/**
* This constant defines the alias used for the runtime context.
*
* @deprecated To be removed in 3.0 because unused
*/
public const CONTEXT_RUNTIME = 'runtime';
/**
* This constant defines the alias used for the tags context.
*
* @deprecated To be removed in 3.0 because unused
*/
public const CONTEXT_TAGS = 'tags';
/**
* This constant defines the alias used for the extra context.
*
* @deprecated To be removed in 3.0 because unused
*/
public const CONTEXT_EXTRA = 'extra';
/**
* This constant defines the alias used for the server OS context.
*
* @deprecated To be removed in 3.0 because unused
*/
public const CONTEXT_SERVER_OS = 'server_os';
/**
* @var array The data stored in this object
*
* @psalm-var array<string, T>
*/
protected $data = [];
/**
* Constructor.
*
* @param array $data The initial data to store
*
* @psalm-param array<string, T> $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}
/**
* Merges the given data with the existing one, recursively or not, according
* to the value of the `$recursive` parameter.
*
* @param array $data The data to merge
* @param bool $recursive Whether to merge the data recursively or not
*
* @psalm-param array<string, T> $data
*/
public function merge(array $data, bool $recursive = false): void
{
$this->data = $recursive ? array_merge_recursive($this->data, $data) : array_merge($this->data, $data);
}
/**
* Sets each element of the array to the value of the corresponding key in
* the given input data.
*
* @param array $data The data to set
*
* @psalm-param array<string, T> $data
*/
public function setData(array $data): void
{
foreach ($data as $index => $value) {
$this->data[$index] = $value;
}
}
/**
* Replaces all the data contained in this object with the given one.
*
* @param array $data The data to set
*
* @psalm-param array<string, T> $data
*/
public function replaceData(array $data): void
{
$this->data = $data;
}
/**
* Clears the entire data contained in this object.
*/
public function clear(): void
{
$this->data = [];
}
/**
* Checks whether the object is not storing any data.
*/
public function isEmpty(): bool
{
return empty($this->data);
}
/**
* Returns an array representation of the data stored by the object.
*
* @psalm-return array<string, T>
*/
public function toArray(): array
{
return $this->data;
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
{
return isset($this->data[$offset]) || \array_key_exists($offset, $this->data);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->data[$offset];
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value): void
{
$this->data[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void
{
unset($this->data[$offset]);
}
/**
* {@inheritdoc}
*
* @psalm-return array<string, T>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->data);
}
}