PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / wordpress / php-mcp-schema / src / Common / AbstractEnum.php
ameliabooking / vendor / wordpress / php-mcp-schema / src / Common Last commit date
Content 1 month ago Contracts 1 month ago Core 1 month ago JsonRpc 1 month ago Lifecycle 1 month ago Protocol 1 month ago Tasks 1 month ago Traits 1 month ago AbstractDataTransferObject.php 1 month ago AbstractEnum.php 1 month ago McpConstants.php 1 month 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