Skip to content

Commit

Permalink
Added "TryGetProperty" extension methods for "IContentBase"
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Jun 27, 2024
1 parent 73011c3 commit de99ac1
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Skybrud.Essentials.Umbraco/ContentBaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Umbraco.Cms.Core.Models;

namespace Skybrud.Essentials.Umbraco;

/// <summary>
/// Static class with various extension methods for <see cref="IContentBase"/>.
/// </summary>
public static class ContentBaseExtensions {

/// <summary>
/// Returns whether a property with the alias <paramref name="propertyAlias"/> exists on <paramref name="content"/>.
/// </summary>
/// <param name="content">An instance of <see cref="IContentBase"/> - e.g. <see cref="IContent"/> or <see cref="IMedia"/>.</param>
/// <param name="propertyAlias">The alias of the property.</param>
/// <returns><see langword="true"/> if the property exists; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// Umbraco doesn't offer a direct way for getting a property with a specific alias from a
/// <see cref="IContentBase"/> instance, so this method will iterate through all of the <paramref name="content"/>
/// item's properties until a match is found. This means this method is <c>O(n)</c> in terms of performance and
/// lookups, and not <c>O(1)</c>.
/// </remarks>
public static bool HasProperty(this IContentBase content, string propertyAlias) {
return content.Properties.FirstOrDefault(x => x.Alias == propertyAlias) != null;
}

/// <summary>
/// Attempts to get the property with the specified <paramref name="propertyAlias"/>.
/// </summary>
/// <param name="content">An instance of <see cref="IContentBase"/> - e.g. <see cref="IContent"/> or <see cref="IMedia"/>.</param>
/// <param name="propertyAlias">The alias of the property.</param>
/// <param name="result">When this method returns, holds an instance of <see cref="IProperty"/> if successful; otherwise, <see langword="null"/>.</param>
/// <returns><see langword="true"/> if the property exists; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// Umbraco doesn't offer a direct way for getting a property with a specific alias from a
/// <see cref="IContentBase"/> instance, so this method will iterate through all of the <paramref name="content"/>
/// item's properties until a match is found. This means this method is <c>O(n)</c> in terms of performance and
/// lookups, and not <c>O(1)</c>.
/// </remarks>
public static bool TryGetProperty(this IContentBase content, string propertyAlias, [NotNullWhen(true)] out IProperty? result) {
result = content.Properties.FirstOrDefault(x => x.Alias == propertyAlias);
return result != null;
}

/// <summary>
/// Attempts to get the property matching the specified <paramref name="predicate"/>.
/// </summary>
/// <param name="content">An instance of <see cref="IContentBase"/> - e.g. <see cref="IContent"/> or <see cref="IMedia"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="result">When this method returns, holds the first instance of <see cref="IProperty"/> element that passes the test specified by <paramref name="predicate"/>, or <see langword="null"/> if not match is found.</param>
/// <returns><see langword="true"/> if a matching property is found; otherwise, <see langword="false"/>.</returns>
public static bool TryGetProperty(this IContentBase content, Func<IProperty, bool> predicate, [NotNullWhen(true)] out IProperty? result) {
result = content.Properties.FirstOrDefault(predicate);
return result != null;
}

}

0 comments on commit de99ac1

Please sign in to comment.