*/ public const REGISTRY = [ 'array' => MultiSelectEnumSchemaFactory::class, 'string' => LegacyTitledEnumSchema::class, ]; /** * Creates an instance from an array. * * @param array $data * @return EnumSchemaInterface * @throws \InvalidArgumentException */ public static function fromArray(array $data): EnumSchemaInterface { if (!isset($data['type'])) { throw new \InvalidArgumentException('Missing discriminator field: type'); } switch ($data['type']) { case 'array': return MultiSelectEnumSchemaFactory::fromArray($data); case 'string': if (isset($data['oneOf'])) { return SingleSelectEnumSchemaFactory::fromArray($data); } elseif (isset($data['enumNames'])) { return LegacyTitledEnumSchema::fromArray($data); } else { return LegacyTitledEnumSchema::fromArray($data); } default: throw new \InvalidArgumentException(sprintf( "Unknown type value '%s'. Valid values: %s", is_scalar($data['type']) ? $data['type'] : gettype($data['type']), implode(', ', array_keys(self::REGISTRY)) )); } } /** * Checks if a type value is supported by this factory. * * @param string $type * @return bool */ public static function supports(string $type): bool { return isset(self::REGISTRY[$type]); } /** * Returns all supported type values. * * @return array */ public static function types(): array { return array_keys(self::REGISTRY); } }