3 * Copyright (c) 2014, Facebook, Inc.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the "hack" directory of this source tree.
12 * This file provides type information for some of HHVM's builtin classes.
14 * YOU SHOULD NEVER INCLUDE THIS FILE ANYWHERE!!!
19 * BuiltinEnum contains the utility methods provided by enums.
20 * Under the hood, an enum Foo will extend BuiltinEnum<Foo>.
22 * HHVM provides a native implementation for this class. The PHP class
23 * definition below is not actually used at run time; it is simply
24 * provided for the typechecker and for developer reference.
26 abstract class BuiltinEnum<+T> {
28 * Get the values of the public consts defined on this class,
29 * indexed by the string name of those consts.
31 * @return array ('CONST_NAME' => $value, ....)
34 final public static function getValues(): darray<string, T>;
37 * Get the names of all the const values, indexed by value. Calls
38 * invariant_exception if multiple constants have the same value.
40 * @return array($value => 'CONST_NAME', ....)
43 final public static function getNames(): darray<T, string> where T as arraykey;
46 * Returns whether or not the value is defined as a constant.
49 final public static function isValid(mixed $value): bool;
52 * Coerce to a valid value or null.
53 * This is useful for typing deserialized enum values.
56 final public static function coerce(mixed $value): ?T;
59 * Coerce to valid value or throw UnexpectedValueException
60 * This is useful for typing deserialized enum values.
63 final public static function assert(mixed $value): T;
66 * Coerce all the values in a traversable. If the value is not an
67 * array of valid items, an UnexpectedValueException is thrown
69 <<__Pure, __AtMostRxAsArgs>>
70 final public static function assertAll(
71 <<__OnlyRxIfImpl(\HH\Rx\Traversable::class), __MaybeMutable>> Traversable<mixed> $values,
75 type enumname<T> = classname<BuiltinEnum<T>>;
77 const enumname<arraykey> BUILTIN_ENUM = BuiltinEnum::class;
79 /* Experimental section for Enum Classes. This is bound to evolve with
84 * Base helper class for the enum class feature
86 final class Elt<-TPhantom, +T> {
87 /* TODO(T77095784) How to make it private ? */
88 public function __construct(private string $name, private T $data) {}
91 public function name(): string {
95 /* TODO(T77095784) ask if this can be inlined/optimized */
97 public function unwrap(): T {
103 * BuiltinEnumClass contains the utility methods provided by enum classes.
104 * Under the hood, an enum class Foo : Bar will extend
105 * BuiltinEnumClass<HH\Elt<this, Bar>>.
107 * HHVM provides a native implementation for this class. The PHP class
108 * definition below is not actually used at run time; it is simply
109 * provided for the typechecker and for developer reference.
112 abstract class BuiltinEnumClass<+T> {
114 * Get the values of the public consts defined on this class,
115 * indexed by the string name of those consts.
117 * @return array ('CONST_NAME' => $value, ....)
120 final public static function getValues(): darray<string, T>;