summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php')
-rw-r--r--vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php58
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php b/vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php
new file mode 100644
index 000000000..e9a1f7334
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Attribute/AttributeValidator.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Attribute;
+
+class AttributeValidator implements AttributeValidatorInterface
+{
+ private const PRIMITIVES = [
+ 'string',
+ 'integer',
+ 'double',
+ 'boolean',
+ ];
+ private const NUMERICS = [
+ 'double',
+ 'integer',
+ ];
+
+ /**
+ * Validate whether a value is a primitive, or a homogeneous array of primitives (treating int/double as equivalent).
+ * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/common/README.md#attribute
+ */
+ public function validate($value): bool
+ {
+ if (is_array($value)) {
+ return $this->validateArray($value);
+ }
+
+ return in_array(gettype($value), self::PRIMITIVES);
+ }
+
+ private function validateArray(array $value): bool
+ {
+ if ($value === []) {
+ return true;
+ }
+ $type = gettype(reset($value));
+ if (!in_array($type, self::PRIMITIVES)) {
+ return false;
+ }
+ foreach ($value as $v) {
+ if (in_array(gettype($v), self::NUMERICS) && in_array($type, self::NUMERICS)) {
+ continue;
+ }
+ if (gettype($v) !== $type) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public function getInvalidMessage(): string
+ {
+ return 'attribute with non-primitive or non-homogeneous array of primitives dropped';
+ }
+}