Files
Umbraco-CMS/src/Umbraco.Core/Models/Mapping/MapperContextExtensions.cs

69 lines
2.2 KiB
C#
Raw Normal View History

using Umbraco.Cms.Core.Mapping;
2019-03-26 10:39:50 +01:00
namespace Umbraco.Extensions
2019-03-26 10:39:50 +01:00
{
/// <summary>
/// Provides extension methods for the <see cref="MapperContext"/> class.
/// </summary>
2020-01-20 15:48:03 +01:00
public static class MapperContextExtensions
2019-03-26 10:39:50 +01:00
{
private const string CultureKey = "Map.Culture";
private const string SegmentKey = "Map.Segment";
2019-03-26 10:39:50 +01:00
private const string IncludedPropertiesKey = "Map.IncludedProperties";
/// <summary>
/// Gets the context culture.
/// </summary>
2022-01-21 11:43:58 +01:00
public static string? GetCulture(this MapperContext context)
2019-03-26 10:39:50 +01:00
{
return context.HasItems && context.Items.TryGetValue(CultureKey, out var obj) && obj is string s ? s : null;
}
/// <summary>
/// Gets the context segment.
/// </summary>
2022-01-21 11:43:58 +01:00
public static string? GetSegment(this MapperContext context)
{
return context.HasItems && context.Items.TryGetValue(SegmentKey, out var obj) && obj is string s ? s : null;
}
2019-03-26 10:39:50 +01:00
/// <summary>
/// Sets a context culture.
/// </summary>
2022-01-21 11:43:58 +01:00
public static void SetCulture(this MapperContext context, string? culture)
2019-03-26 10:39:50 +01:00
{
2022-01-21 11:43:58 +01:00
if (culture is not null)
{
context.Items[CultureKey] = culture;
}
2019-03-26 10:39:50 +01:00
}
/// <summary>
/// Sets a context segment.
/// </summary>
2022-01-21 11:43:58 +01:00
public static void SetSegment(this MapperContext context, string? segment)
{
2022-01-21 11:43:58 +01:00
if (segment is not null)
{
context.Items[SegmentKey] = segment;
}
}
2019-03-26 10:39:50 +01:00
/// <summary>
/// Get included properties.
/// </summary>
2022-01-21 11:43:58 +01:00
public static string[]? GetIncludedProperties(this MapperContext context)
2019-03-26 10:39:50 +01:00
{
return context.HasItems && context.Items.TryGetValue(IncludedPropertiesKey, out var obj) && obj is string[] s ? s : null;
}
/// <summary>
/// Sets included properties.
/// </summary>
public static void SetIncludedProperties(this MapperContext context, string[] properties)
{
context.Items[IncludedPropertiesKey] = properties;
}
}
2020-01-20 15:48:03 +01:00
}