-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SearchAttributeKey value objects
- Loading branch information
Showing
9 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<bool> | ||
* @psalm-immutable | ||
*/ | ||
final class BoolValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::Bool->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
use DateTimeImmutable; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<DateTimeImmutable> | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<float> | ||
* @psalm-immutable | ||
*/ | ||
final class FloatValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::Float->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<int> | ||
* @psalm-immutable | ||
*/ | ||
final class IntValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::Int->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<list<string>> | ||
* @psalm-immutable | ||
*/ | ||
final class KeywordListValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::KeywordList->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<string> | ||
* @psalm-immutable | ||
*/ | ||
final class KeywordValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::Keyword->value; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/DataConverter/SearchAttributes/SearchAttributeKey.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-covariant TValue | ||
* @psalm-immutable | ||
*/ | ||
abstract class SearchAttributeKey implements \JsonSerializable | ||
{ | ||
/** | ||
* @param non-empty-string $key | ||
* @param TValue $value | ||
*/ | ||
final protected function __construct( | ||
public readonly string $key, | ||
public readonly mixed $value, | ||
) {} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
*/ | ||
public static function bool(string $key, bool $value): BoolValue | ||
{ | ||
return new BoolValue($key, $value); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
*/ | ||
public static function integer(string $key, int $value): IntValue | ||
{ | ||
return new IntValue($key, $value); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
*/ | ||
public static function float(string $key, float $value): FloatValue | ||
{ | ||
return new FloatValue($key, $value); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
*/ | ||
public static function keyword(string $key, string $value): KeywordValue | ||
{ | ||
return new KeywordValue($key, $value); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
*/ | ||
public static function string(string $key, string $value): StringValue | ||
{ | ||
return new StringValue($key, $value); | ||
} | ||
|
||
public static function datetime(string $key, \DateTimeInterface|string $value): DatetimeValue | ||
{ | ||
return new DatetimeValue($key, match (true) { | ||
Check failure on line 64 in src/DataConverter/SearchAttributes/SearchAttributeKey.php
|
||
\is_string($value) => new \DateTimeImmutable($value), | ||
$value instanceof \DateTimeImmutable => $value, | ||
default => \DateTimeImmutable::createFromInterface($value), | ||
}); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $key | ||
* @param iterable<scalar> $value | ||
*/ | ||
public static function keywordList(string $key, iterable $value): KeywordListValue | ||
{ | ||
/** @var list<string> $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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
/** | ||
* @template-extends SearchAttributeKey<string> | ||
* @psalm-immutable | ||
*/ | ||
final class StringValue extends SearchAttributeKey | ||
{ | ||
protected function getType(): string | ||
{ | ||
return ValueType::String->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\DataConverter\SearchAttributes; | ||
|
||
enum ValueType: string | ||
{ | ||
case Bool = 'bool'; | ||
case Float = 'float64'; | ||
case Int = 'int'; | ||
case Keyword = 'keyword'; | ||
case KeywordList = 'keyword_list'; | ||
case String = 'string'; | ||
case Datetime = 'datetime'; | ||
} |