2020-12-19 08:17:35 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
2017-11-27 19:17:50 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Common.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class ContentBaseExtensions
|
2017-11-27 19:17:50 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set property values by alias with an anonymous object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Does not support variants.</remarks>
|
|
|
|
|
public static void PropertyValues(this IContentBase content, object value, string? culture = null, string? segment = null)
|
2017-11-27 19:17:50 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
if (value == null)
|
2017-11-27 19:17:50 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
throw new Exception("No properties has been passed in");
|
|
|
|
|
}
|
2017-11-27 19:17:50 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var propertyInfos = value.GetType().GetProperties();
|
|
|
|
|
foreach (var propertyInfo in propertyInfos)
|
|
|
|
|
{
|
|
|
|
|
if (!content.Properties.TryGetValue(propertyInfo.Name, out var property))
|
2017-11-27 19:17:50 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
throw new Exception(
|
|
|
|
|
$"The property alias {propertyInfo.Name} is not valid, because no PropertyType with this alias exists");
|
|
|
|
|
}
|
2019-02-07 12:43:11 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
property.SetValue(propertyInfo.GetValue(value, null), culture, segment);
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Update item with newly added value
|
|
|
|
|
content.Properties.Add(property);
|
2017-11-27 19:17:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|