Files
Umbraco-CMS/src/Umbraco.Examine/BaseValueSetBuilder.cs

73 lines
2.9 KiB
C#
Raw Normal View History

2018-11-26 14:27:44 +11:00
using System.Collections.Generic;
2018-11-26 17:20:15 +11:00
using System.Linq;
2018-11-28 14:46:45 +11:00
using Examine;
2018-11-26 14:27:44 +11:00
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
namespace Umbraco.Examine
{
2018-11-28 14:46:45 +11:00
/// <inheritdoc />
public abstract class BaseValueSetBuilder<TContent> : IValueSetBuilder<TContent>
where TContent : IContentBase
2018-11-26 14:27:44 +11:00
{
protected bool PublishedValuesOnly { get; }
2018-11-26 14:27:44 +11:00
private readonly PropertyEditorCollection _propertyEditors;
protected BaseValueSetBuilder(PropertyEditorCollection propertyEditors, bool publishedValuesOnly)
2018-11-26 14:27:44 +11:00
{
PublishedValuesOnly = publishedValuesOnly;
2018-11-26 14:27:44 +11:00
_propertyEditors = propertyEditors ?? throw new System.ArgumentNullException(nameof(propertyEditors));
}
2018-11-28 14:46:45 +11:00
/// <inheritdoc />
public abstract IEnumerable<ValueSet> GetValueSets(params TContent[] content);
protected void AddPropertyValue(Property property, string culture, string segment, IDictionary<string, IEnumerable<object>> values)
2018-11-26 14:27:44 +11:00
{
2018-11-26 17:20:15 +11:00
var editor = _propertyEditors[property.PropertyType.PropertyEditorAlias];
2018-11-26 14:27:44 +11:00
if (editor == null) return;
2018-12-05 12:57:23 +01:00
var indexVals = editor.PropertyIndexValueFactory.GetIndexValues(property, culture, segment, PublishedValuesOnly);
2018-11-26 14:27:44 +11:00
foreach (var keyVal in indexVals)
{
if (keyVal.Key.IsNullOrWhiteSpace()) continue;
var cultureSuffix = culture == null ? string.Empty : "_" + culture;
foreach (var val in keyVal.Value)
{
switch (val)
{
//only add the value if its not null or empty (we'll check for string explicitly here too)
case null:
continue;
case string strVal:
2018-11-26 17:20:15 +11:00
{
if (strVal.IsNullOrWhiteSpace()) continue;
2018-11-26 17:20:15 +11:00
var key = $"{keyVal.Key}{cultureSuffix}";
if (values.TryGetValue(key, out var v))
values[key] = new List<object>(v) { val }.ToArray();
else
values.Add($"{keyVal.Key}{cultureSuffix}", val.Yield());
2018-11-26 17:20:15 +11:00
}
2018-11-26 14:27:44 +11:00
break;
default:
2018-11-26 17:20:15 +11:00
{
var key = $"{keyVal.Key}{cultureSuffix}";
if (values.TryGetValue(key, out var v))
values[key] = new List<object>(v) { val }.ToArray();
else
values.Add($"{keyVal.Key}{cultureSuffix}", val.Yield());
2018-11-26 17:20:15 +11:00
}
2018-11-26 14:27:44 +11:00
break;
}
}
}
}
}
}