class ArrayAccessGetterSetter
Default implementation of {This type is used if no custom getter/setter is provided to {
Hierarchy
- class \OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter implements \OpenTelemetry\Context\Propagation\PropagationGetterInterface, \OpenTelemetry\Context\Propagation\PropagationSetterInterface
Expanded class hierarchy of ArrayAccessGetterSetter
See also
https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6… Getter and Setter.
PropagationGetterInterface} and {@see PropagationSetterInterface}.
TextMapPropagatorInterface::inject()} or {@see TextMapPropagatorInterface::extract()}.
2 files declare their use of ArrayAccessGetterSetter
- BaggagePropagator.php in vendor/
open-telemetry/ api/ Baggage/ Propagation/ BaggagePropagator.php - TraceContextPropagator.php in vendor/
open-telemetry/ api/ Trace/ Propagation/ TraceContextPropagator.php
File
-
vendor/
open-telemetry/ context/ Propagation/ ArrayAccessGetterSetter.php, line 22
Namespace
OpenTelemetry\Context\PropagationView source
final class ArrayAccessGetterSetter implements PropagationGetterInterface, PropagationSetterInterface {
private static ?self $instance = null;
/**
* Returns a singleton instance of `self` to avoid, multiple runtime allocations.
*/
public static function getInstance() : self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/** {@inheritdoc} */
public function keys($carrier) : array {
if ($this->isSupportedCarrier($carrier)) {
$keys = [];
foreach ($carrier as $key => $_) {
$keys[] = (string) $key;
}
return $keys;
}
throw new InvalidArgumentException(sprintf('Unsupported carrier type: %s.', get_debug_type($carrier)));
}
/** {@inheritdoc} */
public function get($carrier, string $key) : ?string {
if ($this->isSupportedCarrier($carrier)) {
$value = $carrier[$this->resolveKey($carrier, $key)] ?? null;
if (is_array($value) && $value) {
$value = $value[array_key_first($value)];
}
return is_string($value) ? $value : null;
}
throw new InvalidArgumentException(sprintf('Unsupported carrier type: %s. Unable to get value associated with key:%s', get_debug_type($carrier), $key));
}
/** {@inheritdoc} */
public function set(&$carrier, string $key, string $value) : void {
if ($key === '') {
throw new InvalidArgumentException('Unable to set value with an empty key');
}
if ($this->isSupportedCarrier($carrier)) {
if (($r = $this->resolveKey($carrier, $key)) !== $key) {
unset($carrier[$r]);
}
$carrier[$key] = $value;
return;
}
throw new InvalidArgumentException(sprintf('Unsupported carrier type: %s. Unable to set value associated with key:%s', get_debug_type($carrier), $key));
}
private function isSupportedCarrier($carrier) : bool {
return is_array($carrier) || $carrier instanceof ArrayAccess && $carrier instanceof Traversable;
}
private function resolveKey($carrier, string $key) : string {
if (isset($carrier[$key])) {
return $key;
}
foreach ($carrier as $k => $_) {
$k = (string) $k;
if (strcasecmp($k, $key) === 0) {
return $k;
}
}
return $key;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ArrayAccessGetterSetter::$instance | private static | property | ||
ArrayAccessGetterSetter::get | public | function | Gets the value of a given key from a carrier. | Overrides PropagationGetterInterface::get |
ArrayAccessGetterSetter::getInstance | public static | function | Returns a singleton instance of `self` to avoid, multiple runtime allocations. | |
ArrayAccessGetterSetter::isSupportedCarrier | private | function | ||
ArrayAccessGetterSetter::keys | public | function | Returns the list of all the keys in the carrier. | Overrides PropagationGetterInterface::keys |
ArrayAccessGetterSetter::resolveKey | private | function | ||
ArrayAccessGetterSetter::set | public | function | Set the value for a given key on the associated carrier. | Overrides PropagationSetterInterface::set |