Automattic\WooCommerce\Vendor\GraphQL\Type\Definition

PhpEnumType::serializepublicWC 1.0

Method of the class: PhpEnumType{}

No Hooks.

Returns

null. Nothing (null).

Usage

$PhpEnumType = new PhpEnumType();
$PhpEnumType->serialize( $value ): string;
$value(required)
.

PhpEnumType::serialize() code WC 10.9.1

public function serialize($value): string
{
    if ($value instanceof $this->enumClass) {
        return $value->name;
    }

    if (is_a($this->enumClass, \BackedEnum::class, true)) {
        try {
            $instance = $this->enumClass::from($value);
        } catch (\ValueError|\TypeError $error) {
            $notEnumInstanceOrValue = Utils::printSafe($value);
            throw new SerializationError("Cannot serialize value as enum: {$notEnumInstanceOrValue}, expected instance or valid value of {$this->enumClass}.", $error->getCode(), $error);
        }

        return $instance->name;
    }

    $notEnum = Utils::printSafe($value);
    throw new SerializationError("Cannot serialize value as enum: {$notEnum}, expected instance of {$this->enumClass}.");
}