using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Text; using zero.Core.Extensions; namespace zero.Core.Routing { /// /// Represents infos for a URL. /// [DataContract(Name = "urlInfo", Namespace = "")] public class UrlInfo : IEquatable { /// /// Creates a instance representing a true URL. /// public static UrlInfo Url(string text, string culture = null) => new UrlInfo(text, true, culture); /// /// Creates a instance representing a message. /// public static UrlInfo Message(string text, string culture = null) => new UrlInfo(text, false, culture); /// /// Initializes a new instance of the class. /// public UrlInfo(string text, bool isUrl, string culture) { if (text.IsNullOrEmpty()) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(text)); } IsUrl = isUrl; Text = text; Culture = culture; } /// /// Gets the culture. /// [DataMember(Name = "culture")] public string Culture { get; } /// /// Gets a value indicating whether the URL is a true URL. /// /// Otherwise, it is a message. [DataMember(Name = "isUrl")] public bool IsUrl { get; } /// /// Gets the text, which is either the URL, or a message. /// [DataMember(Name = "text")] public string Text { get; } /// /// Checks equality /// /// /// /// /// Compare both culture and Text as invariant strings since URLs are not case sensitive, nor are culture names within Umbraco /// public bool Equals(UrlInfo other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return string.Equals(Culture, other.Culture, StringComparison.InvariantCultureIgnoreCase) && IsUrl == other.IsUrl && string.Equals(Text, other.Text, StringComparison.InvariantCultureIgnoreCase); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals((UrlInfo)obj); } public override int GetHashCode() { unchecked { var hashCode = (Culture != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(Culture) : 0); hashCode = (hashCode * 397) ^ IsUrl.GetHashCode(); hashCode = (hashCode * 397) ^ (Text != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(Text) : 0); return hashCode; } } public static bool operator ==(UrlInfo left, UrlInfo right) { return Equals(left, right); } public static bool operator !=(UrlInfo left, UrlInfo right) { return !Equals(left, right); } public override string ToString() { return Text; } } }