|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WordPress\AiClient\Common; |
| 6 | + |
| 7 | +use BadMethodCallException; |
| 8 | +use InvalidArgumentException; |
| 9 | +use ReflectionClass; |
| 10 | + |
| 11 | +/** |
| 12 | + * Abstract base class for enum-like behavior in PHP 7.4 |
| 13 | + * |
| 14 | + * This class provides enum-like functionality for PHP versions that don't support native enums. |
| 15 | + * Child classes should define uppercase snake_case constants for enum values. |
| 16 | + * |
| 17 | + * @example |
| 18 | + * class PersonEnum extends AbstractEnum { |
| 19 | + * public const FIRST_NAME = 'first'; |
| 20 | + * public const LAST_NAME = 'last'; |
| 21 | + * } |
| 22 | + * |
| 23 | + * // Usage: |
| 24 | + * $enum = PersonEnum::firstName(); // Creates instance with value 'first' |
| 25 | + * $enum->isFirstName(); // Returns true |
| 26 | + * $enum->equals('first'); // Returns true |
| 27 | + * $enum->is(PersonEnum::firstName()); // Returns true |
| 28 | + */ |
| 29 | +abstract class AbstractEnum |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var string|int|float The value of the enum instance |
| 33 | + */ |
| 34 | + private $value; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var array<string, array<string, string|int|float>> Cache for reflection data |
| 38 | + */ |
| 39 | + private static $cache = []; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor is private to ensure instances are created through static methods |
| 43 | + * |
| 44 | + * @param string|int|float $value The enum value |
| 45 | + */ |
| 46 | + private function __construct($value) |
| 47 | + { |
| 48 | + $this->value = $value; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the value of the enum instance |
| 53 | + * |
| 54 | + * @return string|int|float |
| 55 | + */ |
| 56 | + public function getValue() |
| 57 | + { |
| 58 | + return $this->value; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check if this enum has the same value as the given value |
| 63 | + * |
| 64 | + * @param string|int|float|self $other The value or enum to compare |
| 65 | + * @return bool |
| 66 | + */ |
| 67 | + public function equals($other): bool |
| 68 | + { |
| 69 | + if ($other instanceof self) { |
| 70 | + return $this->is($other); |
| 71 | + } |
| 72 | + |
| 73 | + return $this->value === $other; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Check if this enum is the same instance type and value as another enum |
| 78 | + * |
| 79 | + * @param self $other The other enum to compare |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + public function is(self $other): bool |
| 83 | + { |
| 84 | + return get_class($this) === get_class($other) && $this->value === $other->getValue(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get all valid values for this enum |
| 89 | + * |
| 90 | + * @return array<string, string|int|float> |
| 91 | + */ |
| 92 | + public static function getValues(): array |
| 93 | + { |
| 94 | + return self::getConstants(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Check if a value is valid for this enum |
| 99 | + * |
| 100 | + * @param string|int|float $value The value to check |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + public static function isValidValue($value): bool |
| 104 | + { |
| 105 | + return in_array($value, self::getValues(), true); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Create an enum instance from a value |
| 110 | + * |
| 111 | + * @param string|int|float $value The enum value |
| 112 | + * @return static |
| 113 | + * @throws InvalidArgumentException If the value is not valid |
| 114 | + */ |
| 115 | + public static function fromValue($value): self |
| 116 | + { |
| 117 | + if (!self::isValidValue($value)) { |
| 118 | + throw new InvalidArgumentException( |
| 119 | + sprintf('Invalid value "%s" for enum %s', (string) $value, static::class) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + $className = static::class; |
| 124 | + return new $className($value); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get all constants for this enum class |
| 129 | + * |
| 130 | + * @return array<string, string|int|float> |
| 131 | + */ |
| 132 | + protected static function getConstants(): array |
| 133 | + { |
| 134 | + $className = static::class; |
| 135 | + |
| 136 | + if (!isset(self::$cache[$className])) { |
| 137 | + $reflection = new ReflectionClass($className); |
| 138 | + $constants = $reflection->getConstants(); |
| 139 | + |
| 140 | + // Filter to only include uppercase snake_case constants |
| 141 | + $enumConstants = []; |
| 142 | + foreach ($constants as $name => $value) { |
| 143 | + if ( |
| 144 | + preg_match('/^[A-Z][A-Z0-9_]*$/', $name) |
| 145 | + && (is_string($value) || is_int($value) || is_float($value)) |
| 146 | + ) { |
| 147 | + $enumConstants[$name] = $value; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + self::$cache[$className] = $enumConstants; |
| 152 | + } |
| 153 | + |
| 154 | + return self::$cache[$className]; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Handle dynamic method calls for enum creation and checking |
| 159 | + * |
| 160 | + * @param string $name The method name |
| 161 | + * @param array<mixed> $arguments The method arguments |
| 162 | + * @return bool |
| 163 | + * @throws BadMethodCallException If the method doesn't exist |
| 164 | + */ |
| 165 | + public function __call(string $name, array $arguments) |
| 166 | + { |
| 167 | + // Handle is* methods |
| 168 | + if (strpos($name, 'is') === 0) { |
| 169 | + $constantName = self::camelCaseToConstant(substr($name, 2)); |
| 170 | + $constants = self::getConstants(); |
| 171 | + |
| 172 | + if (isset($constants[$constantName])) { |
| 173 | + return $this->value === $constants[$constantName]; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + throw new BadMethodCallException( |
| 178 | + sprintf('Method %s::%s does not exist', static::class, $name) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Handle static method calls for enum creation |
| 184 | + * |
| 185 | + * @param string $name The method name |
| 186 | + * @param array<mixed> $arguments The method arguments |
| 187 | + * @return static |
| 188 | + * @throws BadMethodCallException If the method doesn't exist |
| 189 | + */ |
| 190 | + public static function __callStatic(string $name, array $arguments) |
| 191 | + { |
| 192 | + $constantName = self::camelCaseToConstant($name); |
| 193 | + $constants = self::getConstants(); |
| 194 | + |
| 195 | + if (isset($constants[$constantName])) { |
| 196 | + $className = static::class; |
| 197 | + return new $className($constants[$constantName]); |
| 198 | + } |
| 199 | + |
| 200 | + throw new BadMethodCallException( |
| 201 | + sprintf('Method %s::%s does not exist', static::class, $name) |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Convert camelCase to CONSTANT_CASE |
| 207 | + * |
| 208 | + * @param string $camelCase The camelCase string |
| 209 | + * @return string The CONSTANT_CASE version |
| 210 | + */ |
| 211 | + private static function camelCaseToConstant(string $camelCase): string |
| 212 | + { |
| 213 | + $snakeCase = preg_replace('/([a-z])([A-Z])/', '$1_$2', $camelCase); |
| 214 | + if ($snakeCase === null) { |
| 215 | + return strtoupper($camelCase); |
| 216 | + } |
| 217 | + return strtoupper($snakeCase); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * String representation of the enum |
| 222 | + * |
| 223 | + * @return string |
| 224 | + */ |
| 225 | + public function __toString(): string |
| 226 | + { |
| 227 | + return (string) $this->value; |
| 228 | + } |
| 229 | +} |
0 commit comments