PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / trunk
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). vtrunk
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / vendor / wordpress / php-mcp-schema / src / Common / AbstractEnum.php
commercebird / vendor / wordpress / php-mcp-schema / src / Common Last commit date
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