-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathAttributeBag.php
More file actions
75 lines (61 loc) · 1.52 KB
/
AttributeBag.php
File metadata and controls
75 lines (61 loc) · 1.52 KB
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
<?php
declare(strict_types=1);
namespace Sentry\Attributes;
/**
* @phpstan-import-type AttributeValue from Attribute
*/
class AttributeBag
{
/**
* @var array<string, Attribute>
*/
private $attributes = [];
/**
* @param mixed $value
*/
public function set(string $key, $value): self
{
$attribute = $value instanceof Attribute
? $value
: Attribute::tryFromValue($value);
if ($attribute !== null) {
$this->attributes[$key] = $attribute;
}
return $this;
}
public function __clone()
{
$attributes = [];
foreach ($this->attributes as $key => $attribute) {
$attributes[$key] = clone $attribute;
}
$this->attributes = $attributes;
}
public function get(string $key): ?Attribute
{
return $this->attributes[$key] ?? null;
}
public function forget(string $key): self
{
unset($this->attributes[$key]);
return $this;
}
/**
* @return array<string, Attribute>
*/
public function all(): array
{
return $this->attributes;
}
/**
* Get a simplified representation of the attributes as a key-value array, main purpose is for logging output.
*
* @return array<string, AttributeValue>
*/
public function toSimpleArray(): array
{
return array_map(static function (Attribute $attribute) {
return $attribute->getValue();
}, $this->attributes);
}
}