From 0f98b14319f352dd972fb12d9e5a97a15acd81ef Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 24 Dec 2024 00:57:04 +0400 Subject: [PATCH] Add SearchAttributeKey value objects --- .../SearchAttributes/BoolValue.php | 17 +++ .../SearchAttributes/DatetimeValue.php | 24 +++++ .../SearchAttributes/FloatValue.php | 17 +++ .../SearchAttributes/IntValue.php | 17 +++ .../SearchAttributes/KeywordListValue.php | 17 +++ .../SearchAttributes/KeywordValue.php | 17 +++ .../SearchAttributes/SearchAttributeKey.php | 100 ++++++++++++++++++ .../SearchAttributes/StringValue.php | 17 +++ .../SearchAttributes/ValueType.php | 16 +++ 9 files changed, 242 insertions(+) create mode 100644 src/DataConverter/SearchAttributes/BoolValue.php create mode 100644 src/DataConverter/SearchAttributes/DatetimeValue.php create mode 100644 src/DataConverter/SearchAttributes/FloatValue.php create mode 100644 src/DataConverter/SearchAttributes/IntValue.php create mode 100644 src/DataConverter/SearchAttributes/KeywordListValue.php create mode 100644 src/DataConverter/SearchAttributes/KeywordValue.php create mode 100644 src/DataConverter/SearchAttributes/SearchAttributeKey.php create mode 100644 src/DataConverter/SearchAttributes/StringValue.php create mode 100644 src/DataConverter/SearchAttributes/ValueType.php diff --git a/src/DataConverter/SearchAttributes/BoolValue.php b/src/DataConverter/SearchAttributes/BoolValue.php new file mode 100644 index 00000000..b5be0623 --- /dev/null +++ b/src/DataConverter/SearchAttributes/BoolValue.php @@ -0,0 +1,17 @@ + + * @psalm-immutable + */ +final class BoolValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::Bool->value; + } +} diff --git a/src/DataConverter/SearchAttributes/DatetimeValue.php b/src/DataConverter/SearchAttributes/DatetimeValue.php new file mode 100644 index 00000000..473186e2 --- /dev/null +++ b/src/DataConverter/SearchAttributes/DatetimeValue.php @@ -0,0 +1,24 @@ + + * @psalm-immutable + */ +final class DatetimeValue extends SearchAttributeKey +{ + public function getValue(): string + { + return $this->value->format(\DateTimeImmutable::RFC3339); + } + + protected function getType(): string + { + return ValueType::Datetime->value; + } +} diff --git a/src/DataConverter/SearchAttributes/FloatValue.php b/src/DataConverter/SearchAttributes/FloatValue.php new file mode 100644 index 00000000..97c450ab --- /dev/null +++ b/src/DataConverter/SearchAttributes/FloatValue.php @@ -0,0 +1,17 @@ + + * @psalm-immutable + */ +final class FloatValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::Float->value; + } +} diff --git a/src/DataConverter/SearchAttributes/IntValue.php b/src/DataConverter/SearchAttributes/IntValue.php new file mode 100644 index 00000000..e6b42a03 --- /dev/null +++ b/src/DataConverter/SearchAttributes/IntValue.php @@ -0,0 +1,17 @@ + + * @psalm-immutable + */ +final class IntValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::Int->value; + } +} diff --git a/src/DataConverter/SearchAttributes/KeywordListValue.php b/src/DataConverter/SearchAttributes/KeywordListValue.php new file mode 100644 index 00000000..3377a500 --- /dev/null +++ b/src/DataConverter/SearchAttributes/KeywordListValue.php @@ -0,0 +1,17 @@ +> + * @psalm-immutable + */ +final class KeywordListValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::KeywordList->value; + } +} diff --git a/src/DataConverter/SearchAttributes/KeywordValue.php b/src/DataConverter/SearchAttributes/KeywordValue.php new file mode 100644 index 00000000..188d94f5 --- /dev/null +++ b/src/DataConverter/SearchAttributes/KeywordValue.php @@ -0,0 +1,17 @@ + + * @psalm-immutable + */ +final class KeywordValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::Keyword->value; + } +} diff --git a/src/DataConverter/SearchAttributes/SearchAttributeKey.php b/src/DataConverter/SearchAttributes/SearchAttributeKey.php new file mode 100644 index 00000000..804995c0 --- /dev/null +++ b/src/DataConverter/SearchAttributes/SearchAttributeKey.php @@ -0,0 +1,100 @@ + new \DateTimeImmutable($value), + $value instanceof \DateTimeImmutable => $value, + default => \DateTimeImmutable::createFromInterface($value), + }); + } + + /** + * @param non-empty-string $key + * @param iterable $value + */ + public static function keywordList(string $key, iterable $value): KeywordListValue + { + /** @var list $values */ + $values = []; + foreach ($value as $item) { + $values[] = (string) $item; + } + + return new KeywordListValue($key, $values); + } + + public function jsonSerialize(): array + { + return [ + 'type' => $this->getType(), + 'value' => $this->getValue(), + ]; + } + + abstract protected function getType(): string; + + protected function getValue(): mixed + { + return $this->value; + } +} diff --git a/src/DataConverter/SearchAttributes/StringValue.php b/src/DataConverter/SearchAttributes/StringValue.php new file mode 100644 index 00000000..348e1fa7 --- /dev/null +++ b/src/DataConverter/SearchAttributes/StringValue.php @@ -0,0 +1,17 @@ + + * @psalm-immutable + */ +final class StringValue extends SearchAttributeKey +{ + protected function getType(): string + { + return ValueType::String->value; + } +} diff --git a/src/DataConverter/SearchAttributes/ValueType.php b/src/DataConverter/SearchAttributes/ValueType.php new file mode 100644 index 00000000..baa1dbcc --- /dev/null +++ b/src/DataConverter/SearchAttributes/ValueType.php @@ -0,0 +1,16 @@ +