|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Attributes; |
| 6 | + |
| 7 | +/** |
| 8 | + * @phpstan-type AttributeType 'string'|'boolean'|'integer'|'double' |
| 9 | + * @phpstan-type AttributeValue string|bool|int|float |
| 10 | + * @phpstan-type AttributeSerialized array{ |
| 11 | + * type: AttributeType, |
| 12 | + * value: AttributeValue |
| 13 | + * } |
| 14 | + */ |
| 15 | +class Attribute implements \JsonSerializable |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var AttributeType |
| 19 | + */ |
| 20 | + private $type; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var AttributeValue |
| 24 | + */ |
| 25 | + private $value; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param AttributeValue $value |
| 29 | + * @param AttributeType $type |
| 30 | + */ |
| 31 | + public function __construct($value, string $type) |
| 32 | + { |
| 33 | + $this->value = $value; |
| 34 | + $this->type = $type; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return AttributeType |
| 39 | + */ |
| 40 | + public function getType(): string |
| 41 | + { |
| 42 | + return $this->type; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return AttributeValue |
| 47 | + */ |
| 48 | + public function getValue() |
| 49 | + { |
| 50 | + return $this->value; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param mixed $value |
| 55 | + * |
| 56 | + * @throws \InvalidArgumentException thrown when the value cannot be serialized as an attribute |
| 57 | + */ |
| 58 | + public static function fromValue($value): self |
| 59 | + { |
| 60 | + $attribute = self::tryFromValue($value); |
| 61 | + |
| 62 | + if ($attribute === null) { |
| 63 | + throw new \InvalidArgumentException(\sprintf('Invalid attribute value, %s cannot be serialized', \gettype($value))); |
| 64 | + } |
| 65 | + |
| 66 | + return $attribute; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param mixed $value |
| 71 | + */ |
| 72 | + public static function tryFromValue($value): ?self |
| 73 | + { |
| 74 | + if ($value === null) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + if (\is_bool($value)) { |
| 79 | + return new self($value, 'boolean'); |
| 80 | + } |
| 81 | + |
| 82 | + if (\is_int($value)) { |
| 83 | + return new self($value, 'integer'); |
| 84 | + } |
| 85 | + |
| 86 | + if (\is_float($value)) { |
| 87 | + return new self($value, 'double'); |
| 88 | + } |
| 89 | + |
| 90 | + if (\is_string($value) || (\is_object($value) && method_exists($value, '__toString'))) { |
| 91 | + $stringValue = (string) $value; |
| 92 | + |
| 93 | + if (empty($stringValue)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return new self($stringValue, 'string'); |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return AttributeSerialized |
| 105 | + */ |
| 106 | + public function toArray(): array |
| 107 | + { |
| 108 | + return [ |
| 109 | + 'type' => $this->type, |
| 110 | + 'value' => $this->value, |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return AttributeSerialized |
| 116 | + */ |
| 117 | + public function jsonSerialize(): array |
| 118 | + { |
| 119 | + return $this->toArray(); |
| 120 | + } |
| 121 | + |
| 122 | + public function __toString(): string |
| 123 | + { |
| 124 | + return "{$this->value} ({$this->type})"; |
| 125 | + } |
| 126 | +} |
0 commit comments