-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathAttributeBag.php
More file actions
74 lines (63 loc) · 1.54 KB
/
AttributeBag.php
File metadata and controls
74 lines (63 loc) · 1.54 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
<?php
declare(strict_types=1);
namespace Sentry\Attributes;
/**
* @phpstan-import-type AttributeValue from Attribute
* @phpstan-import-type AttributeSerialized from Attribute
*/
class AttributeBag implements \JsonSerializable
{
/**
* @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 get(string $key): ?Attribute
{
return $this->attributes[$key] ?? null;
}
/**
* @return array<string, Attribute>
*/
public function all(): array
{
return $this->attributes;
}
/**
* @return array<string, AttributeSerialized>
*/
public function toArray(): array
{
return array_map(static function (Attribute $attribute) {
return $attribute->jsonSerialize();
}, $this->attributes);
}
/**
* @return array<string, AttributeSerialized>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* @return array<string, AttributeValue>
*/
public function toSimpleArray(): array
{
return array_map(static function (Attribute $attribute) {
return $attribute->getValue();
}, $this->attributes);
}
}