Content
2 months ago
Contracts
2 months ago
Core
2 months ago
JsonRpc
2 months ago
Lifecycle
2 months ago
Protocol
2 months ago
Tasks
2 months ago
Traits
2 months ago
AbstractDataTransferObject.php
2 months ago
AbstractEnum.php
2 months ago
McpConstants.php
2 months ago
AbstractEnum.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Common; |
| 6 | |
| 7 | /** |
| 8 | * Base class for PHP 7.4 enum emulation. |
| 9 | * |
| 10 | * @mcp-version 2025-11-25 |
| 11 | */ |
| 12 | abstract class AbstractEnum |
| 13 | { |
| 14 | /** @var string */ |
| 15 | private string $value; |
| 16 | |
| 17 | /** @var array<string, static> */ |
| 18 | private static array $instances = []; |
| 19 | |
| 20 | /** |
| 21 | * @param string $value |
| 22 | */ |
| 23 | private function __construct(string $value) |
| 24 | { |
| 25 | $this->value = $value; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Creates an instance from a value. |
| 30 | * |
| 31 | * @param string $value |
| 32 | * @return static |
| 33 | * @throws \InvalidArgumentException |
| 34 | */ |
| 35 | public static function from(string $value): self |
| 36 | { |
| 37 | $values = static::values(); |
| 38 | if (!in_array($value, $values, true)) { |
| 39 | throw new \InvalidArgumentException( |
| 40 | sprintf('Invalid enum value: %s. Valid values: %s', $value, implode(', ', $values)) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | $key = static::class . '::' . $value; |
| 45 | if (!isset(self::$instances[$key])) { |
| 46 | /** @phpstan-ignore new.static (Intentional: private constructor prevents subclass override) */ |
| 47 | self::$instances[$key] = new static($value); |
| 48 | } |
| 49 | |
| 50 | return self::$instances[$key]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates an instance from a value, or null if invalid. |
| 55 | * |
| 56 | * @param string $value |
| 57 | * @return static|null |
| 58 | */ |
| 59 | public static function tryFrom(string $value): ?self |
| 60 | { |
| 61 | try { |
| 62 | return static::from($value); |
| 63 | } catch (\InvalidArgumentException $e) { |
| 64 | return null; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns all valid values for this enum. |
| 70 | * |
| 71 | * @return string[] |
| 72 | */ |
| 73 | abstract public static function values(): array; |
| 74 | |
| 75 | /** |
| 76 | * Returns all cases as instances. |
| 77 | * |
| 78 | * @return static[] |
| 79 | */ |
| 80 | public static function cases(): array |
| 81 | { |
| 82 | return array_map(fn(string $value) => static::from($value), static::values()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the enum value. |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | public function getValue(): string |
| 91 | { |
| 92 | return $this->value; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Converts to string. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function __toString(): string |
| 101 | { |
| 102 | return $this->value; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Compares with another instance. |
| 107 | * |
| 108 | * @param self $other |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function equals(self $other): bool |
| 112 | { |
| 113 | return $this->value === $other->value && static::class === get_class($other); |
| 114 | } |
| 115 | } |
| 116 |