Files
Umbraco-CMS/src/Umbraco.Core/PropertyEditors/IntegerPropertyEditor.cs
Kenn Jacobsen cc082e191c Clean up datatype configurations for V14 (#15718)
* Clean up datatype configurations for V14

* Fix merge

* Remove ParameterEditorCollection + revert accidental changes to core project file
2024-02-15 12:36:23 +01:00

55 lines
2.0 KiB
C#

using System.Globalization;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.PropertyEditors.Validators;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Strings;
namespace Umbraco.Cms.Core.PropertyEditors;
/// <summary>
/// Represents an integer property and parameter editor.
/// </summary>
[DataEditor(
Constants.PropertyEditors.Aliases.Integer,
ValueType = ValueTypes.Integer,
ValueEditorIsReusable = true)]
public class IntegerPropertyEditor : DataEditor
{
public IntegerPropertyEditor(IDataValueEditorFactory dataValueEditorFactory)
: base(dataValueEditorFactory)
=> SupportsReadOnly = true;
/// <inheritdoc />
protected override IDataValueEditor CreateValueEditor()
=> DataValueEditorFactory.Create<IntegerPropertyValueEditor>(Attribute!);
/// <inheritdoc />
protected override IConfigurationEditor CreateConfigurationEditor() => new IntegerConfigurationEditor();
internal class IntegerPropertyValueEditor : DataValueEditor
{
public IntegerPropertyValueEditor(
IShortStringHelper shortStringHelper,
IJsonSerializer jsonSerializer,
IIOHelper ioHelper,
DataEditorAttribute attribute)
: base(shortStringHelper, jsonSerializer, ioHelper, attribute)
=> Validators.Add(new IntegerValidator());
public override object? ToEditor(IProperty property, string? culture = null, string? segment = null)
=> TryParsePropertyValue(property.GetValue(culture, segment));
public override object? FromEditor(ContentPropertyData editorValue, object? currentValue)
=> TryParsePropertyValue(editorValue.Value);
private int? TryParsePropertyValue(object? value)
=> value is int integerValue
? integerValue
: int.TryParse(value?.ToString(), CultureInfo.InvariantCulture, out var parsedIntegerValue)
? parsedIntegerValue
: null;
}
}