<?php

declare(strict_types=1);

namespace SimpleSAML\SAML2\Type;

use SimpleSAML\SAML2\Assert\Assert;
use SimpleSAML\SAML2\XML\md\KeyTypesEnum;
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;

use function array_column;

/**
 * @package simplesaml/saml2
 */
class KeyTypesValue extends SAMLStringValue
{
    /**
     * Validate the content of the element.
     *
     * @throws \Exception on failure
     */
    protected function validateValue(string $content): void
    {
        Assert::oneOf(
            $this->sanitizeValue($content),
            array_column(KeyTypesEnum::cases(), 'value'),
            SchemaViolationException::class,
        );
    }


    /**
     * Convert enum to value type
     */
    public static function fromEnum(KeyTypesEnum $use): static
    {
        return static::fromString($use->value);
    }


    /**
     * Convert this value type to enum
     */
    public function toEnum(): KeyTypesEnum
    {
        return KeyTypesEnum::from($this->getValue());
    }
}
