|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Internal\Declaration; |
| 6 | + |
| 7 | +/** |
| 8 | + * Temporal entity name validator. |
| 9 | + * |
| 10 | + * @internal |
| 11 | + */ |
| 12 | +final class EntityNameValidator |
| 13 | +{ |
| 14 | + public const COMMON_BUILTIN_PREFIX = '__temporal_'; |
| 15 | + public const QUERY_TYPE_STACK_TRACE = "__stack_trace"; |
| 16 | + public const ENHANCED_QUERY_TYPE_STACK_TRACE = "__enhanced_stack_trace"; |
| 17 | + |
| 18 | + /** |
| 19 | + * @throws \InvalidArgumentException |
| 20 | + */ |
| 21 | + public static function validateWorkflow(string $name): void |
| 22 | + { |
| 23 | + self::validateCommonPrefix($name, 'Workflow name'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @throws \InvalidArgumentException |
| 28 | + */ |
| 29 | + public static function validateQueryMethod(string $name): void |
| 30 | + { |
| 31 | + if ($name === self::QUERY_TYPE_STACK_TRACE || $name === self::ENHANCED_QUERY_TYPE_STACK_TRACE) { |
| 32 | + throw new \InvalidArgumentException( |
| 33 | + "The Query method name `$name` is reserved for built-in functionality and cannot be used.", |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + self::validateCommonPrefix($name, 'Query method'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @throws \InvalidArgumentException |
| 42 | + */ |
| 43 | + public static function validateSignalMethod(string $name): void |
| 44 | + { |
| 45 | + self::validateCommonPrefix($name, 'Signal method'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @throws \InvalidArgumentException |
| 50 | + */ |
| 51 | + public static function validateUpdateMethod(string $name): void |
| 52 | + { |
| 53 | + self::validateCommonPrefix($name, 'Update method'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @throws \InvalidArgumentException |
| 58 | + */ |
| 59 | + public static function validateActivity(string $name): void |
| 60 | + { |
| 61 | + self::validateCommonPrefix($name, 'Activity type'); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @throws \InvalidArgumentException |
| 66 | + */ |
| 67 | + public static function validateTaskQueue(string $name): void |
| 68 | + { |
| 69 | + self::validateCommonPrefix($name, 'Task Queue name'); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Throws an exception if the name starts with the common built-in prefix. |
| 74 | + * |
| 75 | + * @throws \InvalidArgumentException |
| 76 | + */ |
| 77 | + private static function validateCommonPrefix(string $name, string $target): void |
| 78 | + { |
| 79 | + \str_starts_with($name, self::COMMON_BUILTIN_PREFIX) and throw new \InvalidArgumentException( |
| 80 | + \sprintf( |
| 81 | + "%s must not start with the internal prefix `%s`.", |
| 82 | + $target, |
| 83 | + self::COMMON_BUILTIN_PREFIX, |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments