mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-30 19:50:15 -07:00
Moved source code under src folder - massive change
This commit is contained in:
parent
2fc8123d6b
commit
5bf0e197ec
1499 changed files with 1054 additions and 1444 deletions
115
src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs
Normal file
115
src/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Common.EnsureThat.Resources;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
|
||||
namespace NzbDrone.Common.EnsureThat
|
||||
{
|
||||
[DebuggerStepThrough]
|
||||
public static class EnsureStringExtensions
|
||||
{
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> IsNotNullOrWhiteSpace(this Param<string> param)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> IsNotNullOrEmpty(this Param<string> param)
|
||||
{
|
||||
if (string.IsNullOrEmpty(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrEmpty);
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> HasLengthBetween(this Param<string> param, int minLength, int maxLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrEmpty);
|
||||
|
||||
var length = param.Value.Length;
|
||||
|
||||
if (length < minLength)
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotInRange_ToShort.Inject(minLength, maxLength, length));
|
||||
|
||||
if (length > maxLength)
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotInRange_ToLong.Inject(minLength, maxLength, length));
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> IsLongerThan(this Param<string> param, int minLength)
|
||||
{
|
||||
if (string.IsNullOrEmpty(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrEmpty);
|
||||
|
||||
var length = param.Value.Length;
|
||||
|
||||
if (length < minLength)
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, "The string is not long enough. Must be at least '{0}' but was '{1}' characters long.".Inject(minLength, length));
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> Matches(this Param<string> param, string match)
|
||||
{
|
||||
return Matches(param, new Regex(match));
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> Matches(this Param<string> param, Regex match)
|
||||
{
|
||||
if (!match.IsMatch(param.Value))
|
||||
{
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_NoMatch.Inject(param.Value, match));
|
||||
}
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> IsRelativePath(this Param<string> param)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
||||
|
||||
if (!param.Value.EndsWith("\\"))
|
||||
{
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid relative path. relative paths must end with \\", param.Value));
|
||||
}
|
||||
|
||||
if (param.Value.Length > 1 && param.Value.StartsWith("\\"))
|
||||
{
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid relative path. relative paths can not start with \\", param.Value));
|
||||
}
|
||||
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public static Param<string> IsValidPath(this Param<string> param)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(param.Value))
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
||||
|
||||
if (param.Value.IsPathValid()) return param;
|
||||
|
||||
if (OsInfo.IsLinux)
|
||||
{
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid *nix path. paths must start with /", param.Value));
|
||||
}
|
||||
|
||||
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid Windows path. paths must be a full path eg. C:\\Windows", param.Value));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue