wppaste
WordPress

AbstractEnum

Since
0.1.0
Source
wp-includes/php-ai-client/src/Common/AbstractEnum.php:38
Abstract base class for enum-like behavior in PHP 7.4.

Description

This class provides enum-like functionality for PHP versions that don't support native enums.
Child classes should define uppercase snake_case constants for enum values.

Compatibility

WordPress
since 0.1.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 0.1.0.

Properties · 4

$valuestringprivate
$namestringprivate
$cacheWordPress\AiClient\Common\array<string,private static
$instancesWordPress\AiClient\Common\array<string,private static

Methods · 18

  • __construct()Constructor is private to ensure instances are created through static methods.
  • __get()Provides read-only access to properties.
  • __set()Prevents property modification.
  • from()Creates an enum instance from a value, throws exception if invalid.
  • tryFrom()Tries to create an enum instance from a value, returns null if invalid.
  • cases()Gets all enum cases.
  • equals()Checks if this enum has the same value as the given value.
  • is()Checks if this enum is the same instance type and value as another enum.
  • getValues()Gets all valid values for this enum.
  • isValidValue()Checks if a value is valid for this enum.
  • getInstance()Gets or creates a singleton instance for the given value and name.
  • getConstants()Gets all constants for this enum class.
  • determineClassEnumerations()Determines the class enumerations by reflecting on class constants.
  • __call()Handles dynamic method calls for enum checking.
  • __callStatic()Handles static method calls for enum creation.
  • camelCaseToConstant()Converts camelCase to CONSTANT_CASE.
  • __toString()Returns string representation of the enum.
  • jsonSerialize()Converts the enum to a JSON-serializable format.

Source code

abstract class AbstractEnum implements JsonSerializable{    /**     * @var string The value of the enum instance.     */    private string $value;    /**     * @var string The name of the enum constant.     */    private string $name;    /**     * @var array<string, array<string, string>> Cache for reflection data.     */    private static array $cache = [];    /**     * @var array<string, array<string, self>> Cache for enum instances.     */    private static array $instances = [];    /**     * Constructor is private to ensure instances are created through static methods.     *     * @since 0.1.0     *     * @param string $value The enum value.     * @param string $name The constant name.     */    final private function __construct(string $value, string $name)    {        $this->value = $value;        $this->name = $name;    }    /**     * Provides read-only access to properties.     *     * @since 0.1.0     *     * @param string $property The property name.     * @return mixed The property value.     * @throws BadMethodCallException If property doesn't exist.     */    final public function __get(string $property)    {        if ($property === 'value' || $property === 'name') {            return $this->{$property};        }        throw new BadMethodCallException(sprintf('Property %s::%s does not exist', static::class, $property));    }    /**     * Prevents property modification.     *     * @since 0.1.0     *     * @param string $property The property name.     * @param mixed $value The value to set.     * @throws BadMethodCallException Always, as enum properties are read-only.     */    final public function __set(string $property, $value): void    {        throw new BadMethodCallException(sprintf('Cannot modify property %s::%s - enum properties are read-only', static::class, $property));    }    /**     * Creates an enum instance from a value, throws exception if invalid.     *     * @since 0.1.0     *     * @param string $value The enum value.     * @return static The enum instance.     * @throws InvalidArgumentException If the value is not valid.     */    final public static function from(string $value): self    {        $instance = self::tryFrom($value);        if ($instance === null) {            throw new InvalidArgumentException(sprintf('%s is not a valid backing value for enum %s', $value, static::class));        }        return $instance;    }    /**     * Tries to create an enum instance from a value, returns null if invalid.     *

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/php-ai-client/src/Common/AbstractEnum.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.