// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System;
using Doozy.Runtime.Common.Extensions;
using UnityEngine;
namespace Doozy.Runtime.Common
{
/// Pair of two strings used as a single entry in Category Name databases
[Serializable]
public class CategoryNameItem
{
/// Default string value for Category
public const string k_DefaultCategory = "None";
/// Default string value for Name
public const string k_DefaultName = "None";
[SerializeField] private string Category;
/// Category string value
public string category => Category;
[SerializeField] private string Name;
/// Name string value
public string name => Name;
/// Construct a new with default values
public CategoryNameItem()
{
Category = k_DefaultCategory;
Name = k_DefaultName;
}
/// Construct a new with the given category value and the default name value
/// Category value
public CategoryNameItem(string category)
{
Category = category;
Name = k_DefaultName;
}
/// Construct a new with the given category and name values
/// Category value
/// Name value
/// Remove whitespaces from values
/// Remove special characters from values
public CategoryNameItem(string category, string name, bool removeWhitespaces = true, bool removeSpecialCharacters = true)
{
Category = CleanString(category, removeWhitespaces, removeSpecialCharacters);
Name = CleanString(name, removeWhitespaces, removeSpecialCharacters);
}
/// Set a new Category value
/// Target value
/// Remove all whitespaces from the target string
/// Remove all special characters from the target string
/// Operation result (True or False) and a success or failure reason message
public (bool, string) SetCategory(string newCategory, bool removeWhitespaces = true, bool removeSpecialCharacters = true)
{
if (newCategory.RemoveWhitespaces().RemoveAllSpecialCharacters().IsNullOrEmpty())
return (false, $"Invalid '{nameof(newCategory)}'. It cannot be null or empty or contain special characters");
Category = CleanString(newCategory, removeWhitespaces, removeSpecialCharacters);
return (true, $"'{nameof(Category)}' renamed to: {Category}");
}
/// Set a new Name value
/// Target value
/// Remove all whitespaces from the target string
/// Remove all special characters from the target string
/// Operation result (True or False) and a success or failure reason message
public (bool, string) SetName(string newName, bool removeWhitespaces = true, bool removeSpecialCharacters = true)
{
if (newName.RemoveWhitespaces().RemoveAllSpecialCharacters().IsNullOrEmpty())
return (false, $"Invalid '{nameof(newName)}'. It cannot be null or empty or contain special characters");
Name = CleanString(newName, removeWhitespaces, removeSpecialCharacters);
return (true, $"'{nameof(Name)}' renamed to: {Name}");
}
/// Cleans the string by removing any empty spaces (at the start of the string and/or the end of the string)
/// Target value
/// Remove all whitespaces from the target string
/// Remove all special characters from the target string
/// The cleaned string
public static string CleanString(string value, bool removeWhitespaces = true, bool removeSpecialCharacters = true)
{
if (removeWhitespaces) value = value.RemoveWhitespaces();
if (removeSpecialCharacters) value = value.RemoveAllSpecialCharacters();
return value.Trim();
}
}
}