| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Attributes; |
| 5 |
|
| 6 |
/** |
| 7 |
* @phpstan-import-type AttributeValue from Attribute |
| 8 |
*/ |
| 9 |
class AttributeBag |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var array<string, Attribute> |
| 13 |
*/ |
| 14 |
private $attributes = []; |
| 15 |
/** |
| 16 |
* @param mixed $value |
| 17 |
*/ |
| 18 |
public function set(string $key, $value) : self |
| 19 |
{ |
| 20 |
$attribute = $value instanceof Attribute ? $value : Attribute::tryFromValue($value); |
| 21 |
if ($attribute !== null) { |
| 22 |
$this->attributes[$key] = $attribute; |
| 23 |
} |
| 24 |
return $this; |
| 25 |
} |
| 26 |
/** |
| 27 |
* @param mixed $value |
| 28 |
*/ |
| 29 |
public function setUnlessNull(string $key, $value) : void |
| 30 |
{ |
| 31 |
if ($value !== null) { |
| 32 |
self::set($key, $value); |
| 33 |
} |
| 34 |
} |
| 35 |
public function get(string $key) : ?Attribute |
| 36 |
{ |
| 37 |
return $this->attributes[$key] ?? null; |
| 38 |
} |
| 39 |
public function forget(string $key) : self |
| 40 |
{ |
| 41 |
unset($this->attributes[$key]); |
| 42 |
return $this; |
| 43 |
} |
| 44 |
/** |
| 45 |
* @return array<string, Attribute> |
| 46 |
*/ |
| 47 |
public function all() : array |
| 48 |
{ |
| 49 |
return $this->attributes; |
| 50 |
} |
| 51 |
/** |
| 52 |
* Get a simplified representation of the attributes as a key-value array, main purpose is for logging output. |
| 53 |
* |
| 54 |
* @return array<string, AttributeValue> |
| 55 |
*/ |
| 56 |
public function toSimpleArray() : array |
| 57 |
{ |
| 58 |
return \array_map(static function (Attribute $attribute) { |
| 59 |
return $attribute->getValue(); |
| 60 |
}, $this->attributes); |
| 61 |
} |
| 62 |
} |
| 63 |
|