using System;
namespace Umbraco.Core.Models.PublishedContent
{
///
/// Provides strongly typed published content models services.
///
public static class PublishedContentExtensionsForModels
{
///
/// Creates a strongly typed published content model for an internal published content.
///
/// The internal published content.
/// The strongly typed published content model.
public static IPublishedContent CreateModel(this IPublishedContent content, IPublishedModelFactory publishedModelFactory)
{
if (publishedModelFactory == null) throw new ArgumentNullException(nameof(publishedModelFactory));
if (content == null)
return null;
// get model
// if factory returns nothing, throw
var model = publishedModelFactory.CreateModel(content);
if (model == null)
throw new InvalidOperationException("Factory returned null.");
// if factory returns a different type, throw
if (!(model is IPublishedContent publishedContent))
throw new InvalidOperationException($"Factory returned model of type {model.GetType().FullName} which does not implement IPublishedContent.");
return publishedContent;
}
}
}